1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/GlobalAlias.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/PassManager.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ManagedStatic.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Threading.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/system_error.h"
42 void llvm::initializeCore(PassRegistry &Registry) {
43 initializeDominatorTreeWrapperPassPass(Registry);
44 initializePrintModulePassWrapperPass(Registry);
45 initializePrintFunctionPassWrapperPass(Registry);
46 initializePrintBasicBlockPassPass(Registry);
47 initializeVerifierLegacyPassPass(Registry);
50 void LLVMInitializeCore(LLVMPassRegistryRef R) {
51 initializeCore(*unwrap(R));
58 /*===-- Error handling ----------------------------------------------------===*/
60 char *LLVMCreateMessage(const char *Message) {
61 return strdup(Message);
64 void LLVMDisposeMessage(char *Message) {
69 /*===-- Operations on contexts --------------------------------------------===*/
71 LLVMContextRef LLVMContextCreate() {
72 return wrap(new LLVMContext());
75 LLVMContextRef LLVMGetGlobalContext() {
76 return wrap(&getGlobalContext());
79 void LLVMContextDispose(LLVMContextRef C) {
83 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
85 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
88 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
89 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
93 /*===-- Operations on modules ---------------------------------------------===*/
95 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
96 return wrap(new Module(ModuleID, getGlobalContext()));
99 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
101 return wrap(new Module(ModuleID, *unwrap(C)));
104 void LLVMDisposeModule(LLVMModuleRef M) {
108 /*--.. Data layout .........................................................--*/
109 const char * LLVMGetDataLayout(LLVMModuleRef M) {
110 return unwrap(M)->getDataLayoutStr().c_str();
113 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
114 unwrap(M)->setDataLayout(Triple);
117 /*--.. Target triple .......................................................--*/
118 const char * LLVMGetTarget(LLVMModuleRef M) {
119 return unwrap(M)->getTargetTriple().c_str();
122 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
123 unwrap(M)->setTargetTriple(Triple);
126 void LLVMDumpModule(LLVMModuleRef M) {
130 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
131 char **ErrorMessage) {
133 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
134 if (!error.empty()) {
135 *ErrorMessage = strdup(error.c_str());
139 unwrap(M)->print(dest, NULL);
141 if (!error.empty()) {
142 *ErrorMessage = strdup(error.c_str());
149 char *LLVMPrintModuleToString(LLVMModuleRef M) {
151 raw_string_ostream os(buf);
153 unwrap(M)->print(os, NULL);
156 return strdup(buf.c_str());
159 /*--.. Operations on inline assembler ......................................--*/
160 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
161 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
165 /*--.. Operations on module contexts ......................................--*/
166 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
167 return wrap(&unwrap(M)->getContext());
171 /*===-- Operations on types -----------------------------------------------===*/
173 /*--.. Operations on all types (mostly) ....................................--*/
175 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
176 switch (unwrap(Ty)->getTypeID()) {
178 return LLVMVoidTypeKind;
180 return LLVMHalfTypeKind;
181 case Type::FloatTyID:
182 return LLVMFloatTypeKind;
183 case Type::DoubleTyID:
184 return LLVMDoubleTypeKind;
185 case Type::X86_FP80TyID:
186 return LLVMX86_FP80TypeKind;
187 case Type::FP128TyID:
188 return LLVMFP128TypeKind;
189 case Type::PPC_FP128TyID:
190 return LLVMPPC_FP128TypeKind;
191 case Type::LabelTyID:
192 return LLVMLabelTypeKind;
193 case Type::MetadataTyID:
194 return LLVMMetadataTypeKind;
195 case Type::IntegerTyID:
196 return LLVMIntegerTypeKind;
197 case Type::FunctionTyID:
198 return LLVMFunctionTypeKind;
199 case Type::StructTyID:
200 return LLVMStructTypeKind;
201 case Type::ArrayTyID:
202 return LLVMArrayTypeKind;
203 case Type::PointerTyID:
204 return LLVMPointerTypeKind;
205 case Type::VectorTyID:
206 return LLVMVectorTypeKind;
207 case Type::X86_MMXTyID:
208 return LLVMX86_MMXTypeKind;
210 llvm_unreachable("Unhandled TypeID.");
213 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
215 return unwrap(Ty)->isSized();
218 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
219 return wrap(&unwrap(Ty)->getContext());
222 void LLVMDumpType(LLVMTypeRef Ty) {
223 return unwrap(Ty)->dump();
226 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
228 raw_string_ostream os(buf);
230 unwrap(Ty)->print(os);
233 return strdup(buf.c_str());
236 /*--.. Operations on integer types .........................................--*/
238 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
239 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
241 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
242 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
244 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
245 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
247 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
248 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
250 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
251 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
253 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
254 return wrap(IntegerType::get(*unwrap(C), NumBits));
257 LLVMTypeRef LLVMInt1Type(void) {
258 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
260 LLVMTypeRef LLVMInt8Type(void) {
261 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
263 LLVMTypeRef LLVMInt16Type(void) {
264 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
266 LLVMTypeRef LLVMInt32Type(void) {
267 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
269 LLVMTypeRef LLVMInt64Type(void) {
270 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
272 LLVMTypeRef LLVMIntType(unsigned NumBits) {
273 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
276 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
277 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
280 /*--.. Operations on real types ............................................--*/
282 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
283 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
285 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
286 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
288 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
289 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
291 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
292 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
294 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
295 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
297 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
298 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
300 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
301 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
304 LLVMTypeRef LLVMHalfType(void) {
305 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
307 LLVMTypeRef LLVMFloatType(void) {
308 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
310 LLVMTypeRef LLVMDoubleType(void) {
311 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
313 LLVMTypeRef LLVMX86FP80Type(void) {
314 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
316 LLVMTypeRef LLVMFP128Type(void) {
317 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
319 LLVMTypeRef LLVMPPCFP128Type(void) {
320 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
322 LLVMTypeRef LLVMX86MMXType(void) {
323 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
326 /*--.. Operations on function types ........................................--*/
328 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
329 LLVMTypeRef *ParamTypes, unsigned ParamCount,
331 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
332 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
335 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
336 return unwrap<FunctionType>(FunctionTy)->isVarArg();
339 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
340 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
343 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
344 return unwrap<FunctionType>(FunctionTy)->getNumParams();
347 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
348 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
349 for (FunctionType::param_iterator I = Ty->param_begin(),
350 E = Ty->param_end(); I != E; ++I)
354 /*--.. Operations on struct types ..........................................--*/
356 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
357 unsigned ElementCount, LLVMBool Packed) {
358 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
359 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
362 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
363 unsigned ElementCount, LLVMBool Packed) {
364 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
365 ElementCount, Packed);
368 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
370 return wrap(StructType::create(*unwrap(C), Name));
373 const char *LLVMGetStructName(LLVMTypeRef Ty)
375 StructType *Type = unwrap<StructType>(Ty);
376 if (!Type->hasName())
378 return Type->getName().data();
381 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
382 unsigned ElementCount, LLVMBool Packed) {
383 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
384 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
387 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
388 return unwrap<StructType>(StructTy)->getNumElements();
391 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
392 StructType *Ty = unwrap<StructType>(StructTy);
393 for (StructType::element_iterator I = Ty->element_begin(),
394 E = Ty->element_end(); I != E; ++I)
398 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
399 return unwrap<StructType>(StructTy)->isPacked();
402 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
403 return unwrap<StructType>(StructTy)->isOpaque();
406 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
407 return wrap(unwrap(M)->getTypeByName(Name));
410 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
412 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
413 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
416 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
417 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
420 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
421 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
424 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
425 return wrap(unwrap<SequentialType>(Ty)->getElementType());
428 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
429 return unwrap<ArrayType>(ArrayTy)->getNumElements();
432 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
433 return unwrap<PointerType>(PointerTy)->getAddressSpace();
436 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
437 return unwrap<VectorType>(VectorTy)->getNumElements();
440 /*--.. Operations on other types ...........................................--*/
442 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
443 return wrap(Type::getVoidTy(*unwrap(C)));
445 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
446 return wrap(Type::getLabelTy(*unwrap(C)));
449 LLVMTypeRef LLVMVoidType(void) {
450 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
452 LLVMTypeRef LLVMLabelType(void) {
453 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
456 /*===-- Operations on values ----------------------------------------------===*/
458 /*--.. Operations on all values ............................................--*/
460 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
461 return wrap(unwrap(Val)->getType());
464 const char *LLVMGetValueName(LLVMValueRef Val) {
465 return unwrap(Val)->getName().data();
468 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
469 unwrap(Val)->setName(Name);
472 void LLVMDumpValue(LLVMValueRef Val) {
476 char* LLVMPrintValueToString(LLVMValueRef Val) {
478 raw_string_ostream os(buf);
480 unwrap(Val)->print(os);
483 return strdup(buf.c_str());
486 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
487 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
490 int LLVMHasMetadata(LLVMValueRef Inst) {
491 return unwrap<Instruction>(Inst)->hasMetadata();
494 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
495 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
498 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
499 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
502 /*--.. Conversion functions ................................................--*/
504 #define LLVM_DEFINE_VALUE_CAST(name) \
505 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
506 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
509 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
511 /*--.. Operations on Uses ..................................................--*/
512 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
513 Value *V = unwrap(Val);
514 Value::use_iterator I = V->use_begin();
515 if (I == V->use_end())
520 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
521 Use *Next = unwrap(U)->getNext();
527 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
528 return wrap(unwrap(U)->getUser());
531 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
532 return wrap(unwrap(U)->get());
535 /*--.. Operations on Users .................................................--*/
536 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
537 Value *V = unwrap(Val);
538 if (MDNode *MD = dyn_cast<MDNode>(V))
539 return wrap(MD->getOperand(Index));
540 return wrap(cast<User>(V)->getOperand(Index));
543 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
544 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
547 int LLVMGetNumOperands(LLVMValueRef Val) {
548 Value *V = unwrap(Val);
549 if (MDNode *MD = dyn_cast<MDNode>(V))
550 return MD->getNumOperands();
551 return cast<User>(V)->getNumOperands();
554 /*--.. Operations on constants of any type .................................--*/
556 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
557 return wrap(Constant::getNullValue(unwrap(Ty)));
560 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
561 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
564 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
565 return wrap(UndefValue::get(unwrap(Ty)));
568 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
569 return isa<Constant>(unwrap(Ty));
572 LLVMBool LLVMIsNull(LLVMValueRef Val) {
573 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
574 return C->isNullValue();
578 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
579 return isa<UndefValue>(unwrap(Val));
582 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
584 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
587 /*--.. Operations on metadata nodes ........................................--*/
589 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
591 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
594 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
595 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
598 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
600 return wrap(MDNode::get(*unwrap(C),
601 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
604 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
605 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
608 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
609 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
610 *Len = S->getString().size();
611 return S->getString().data();
617 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
619 return cast<MDNode>(unwrap(V))->getNumOperands();
622 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
624 const MDNode *N = cast<MDNode>(unwrap(V));
625 const unsigned numOperands = N->getNumOperands();
626 for (unsigned i = 0; i < numOperands; i++)
627 Dest[i] = wrap(N->getOperand(i));
630 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
632 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
633 return N->getNumOperands();
638 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
640 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
643 for (unsigned i=0;i<N->getNumOperands();i++)
644 Dest[i] = wrap(N->getOperand(i));
647 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
650 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
653 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
658 /*--.. Operations on scalar constants ......................................--*/
660 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
661 LLVMBool SignExtend) {
662 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
665 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
667 const uint64_t Words[]) {
668 IntegerType *Ty = unwrap<IntegerType>(IntTy);
669 return wrap(ConstantInt::get(Ty->getContext(),
670 APInt(Ty->getBitWidth(),
671 makeArrayRef(Words, NumWords))));
674 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
676 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
680 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
681 unsigned SLen, uint8_t Radix) {
682 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
686 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
687 return wrap(ConstantFP::get(unwrap(RealTy), N));
690 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
691 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
694 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
696 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
699 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
700 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
703 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
704 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
707 /*--.. Operations on composite constants ...................................--*/
709 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
711 LLVMBool DontNullTerminate) {
712 /* Inverted the sense of AddNull because ', 0)' is a
713 better mnemonic for null termination than ', 1)'. */
714 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
715 DontNullTerminate == 0));
717 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
718 LLVMValueRef *ConstantVals,
719 unsigned Count, LLVMBool Packed) {
720 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
721 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
725 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
726 LLVMBool DontNullTerminate) {
727 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
730 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
731 LLVMValueRef *ConstantVals, unsigned Length) {
732 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
733 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
735 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
737 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
741 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
742 LLVMValueRef *ConstantVals,
744 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
745 StructType *Ty = cast<StructType>(unwrap(StructTy));
747 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
750 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
751 return wrap(ConstantVector::get(makeArrayRef(
752 unwrap<Constant>(ScalarConstantVals, Size), Size)));
755 /*-- Opcode mapping */
757 static LLVMOpcode map_to_llvmopcode(int opcode)
760 default: llvm_unreachable("Unhandled Opcode.");
761 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
762 #include "llvm/IR/Instruction.def"
767 static int map_from_llvmopcode(LLVMOpcode code)
770 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
771 #include "llvm/IR/Instruction.def"
774 llvm_unreachable("Unhandled Opcode.");
777 /*--.. Constant expressions ................................................--*/
779 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
780 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
783 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
784 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
787 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
788 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
791 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
792 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
795 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
796 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
799 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
800 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
804 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
805 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
808 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
809 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
812 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
813 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
814 unwrap<Constant>(RHSConstant)));
817 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
818 LLVMValueRef RHSConstant) {
819 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
820 unwrap<Constant>(RHSConstant)));
823 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
824 LLVMValueRef RHSConstant) {
825 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
826 unwrap<Constant>(RHSConstant)));
829 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
830 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
831 unwrap<Constant>(RHSConstant)));
834 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
835 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
836 unwrap<Constant>(RHSConstant)));
839 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
840 LLVMValueRef RHSConstant) {
841 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
842 unwrap<Constant>(RHSConstant)));
845 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
846 LLVMValueRef RHSConstant) {
847 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
848 unwrap<Constant>(RHSConstant)));
851 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
852 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
853 unwrap<Constant>(RHSConstant)));
856 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
857 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
858 unwrap<Constant>(RHSConstant)));
861 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
862 LLVMValueRef RHSConstant) {
863 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
864 unwrap<Constant>(RHSConstant)));
867 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
868 LLVMValueRef RHSConstant) {
869 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
870 unwrap<Constant>(RHSConstant)));
873 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
874 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
875 unwrap<Constant>(RHSConstant)));
878 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
879 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
880 unwrap<Constant>(RHSConstant)));
883 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
884 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
885 unwrap<Constant>(RHSConstant)));
888 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
889 LLVMValueRef RHSConstant) {
890 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
891 unwrap<Constant>(RHSConstant)));
894 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
895 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
896 unwrap<Constant>(RHSConstant)));
899 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
900 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
901 unwrap<Constant>(RHSConstant)));
904 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
905 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
906 unwrap<Constant>(RHSConstant)));
909 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
910 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
911 unwrap<Constant>(RHSConstant)));
914 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
915 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
916 unwrap<Constant>(RHSConstant)));
919 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
920 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
921 unwrap<Constant>(RHSConstant)));
924 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
925 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
926 unwrap<Constant>(RHSConstant)));
929 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
930 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
931 return wrap(ConstantExpr::getICmp(Predicate,
932 unwrap<Constant>(LHSConstant),
933 unwrap<Constant>(RHSConstant)));
936 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
937 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
938 return wrap(ConstantExpr::getFCmp(Predicate,
939 unwrap<Constant>(LHSConstant),
940 unwrap<Constant>(RHSConstant)));
943 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
944 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
945 unwrap<Constant>(RHSConstant)));
948 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
949 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
950 unwrap<Constant>(RHSConstant)));
953 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
954 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
955 unwrap<Constant>(RHSConstant)));
958 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
959 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
960 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
962 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
966 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
967 LLVMValueRef *ConstantIndices,
968 unsigned NumIndices) {
969 Constant* Val = unwrap<Constant>(ConstantVal);
970 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
972 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
975 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
976 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
980 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
981 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
985 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
986 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
990 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
991 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
995 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
996 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1000 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1001 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1005 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1006 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1010 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1011 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1015 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1016 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1020 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1021 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1025 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1026 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1030 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1031 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1035 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1036 LLVMTypeRef ToType) {
1037 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1041 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1042 LLVMTypeRef ToType) {
1043 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1047 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1048 LLVMTypeRef ToType) {
1049 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1053 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1054 LLVMTypeRef ToType) {
1055 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1059 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1060 LLVMTypeRef ToType) {
1061 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1065 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1066 LLVMBool isSigned) {
1067 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1068 unwrap(ToType), isSigned));
1071 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1072 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1076 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1077 LLVMValueRef ConstantIfTrue,
1078 LLVMValueRef ConstantIfFalse) {
1079 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1080 unwrap<Constant>(ConstantIfTrue),
1081 unwrap<Constant>(ConstantIfFalse)));
1084 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1085 LLVMValueRef IndexConstant) {
1086 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1087 unwrap<Constant>(IndexConstant)));
1090 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1091 LLVMValueRef ElementValueConstant,
1092 LLVMValueRef IndexConstant) {
1093 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1094 unwrap<Constant>(ElementValueConstant),
1095 unwrap<Constant>(IndexConstant)));
1098 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1099 LLVMValueRef VectorBConstant,
1100 LLVMValueRef MaskConstant) {
1101 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1102 unwrap<Constant>(VectorBConstant),
1103 unwrap<Constant>(MaskConstant)));
1106 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1108 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1109 makeArrayRef(IdxList, NumIdx)));
1112 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1113 LLVMValueRef ElementValueConstant,
1114 unsigned *IdxList, unsigned NumIdx) {
1115 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1116 unwrap<Constant>(ElementValueConstant),
1117 makeArrayRef(IdxList, NumIdx)));
1120 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1121 const char *Constraints,
1122 LLVMBool HasSideEffects,
1123 LLVMBool IsAlignStack) {
1124 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1125 Constraints, HasSideEffects, IsAlignStack));
1128 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1129 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1132 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1134 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1135 return wrap(unwrap<GlobalValue>(Global)->getParent());
1138 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1139 return unwrap<GlobalValue>(Global)->isDeclaration();
1142 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1143 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1144 case GlobalValue::ExternalLinkage:
1145 return LLVMExternalLinkage;
1146 case GlobalValue::AvailableExternallyLinkage:
1147 return LLVMAvailableExternallyLinkage;
1148 case GlobalValue::LinkOnceAnyLinkage:
1149 return LLVMLinkOnceAnyLinkage;
1150 case GlobalValue::LinkOnceODRLinkage:
1151 return LLVMLinkOnceODRLinkage;
1152 case GlobalValue::WeakAnyLinkage:
1153 return LLVMWeakAnyLinkage;
1154 case GlobalValue::WeakODRLinkage:
1155 return LLVMWeakODRLinkage;
1156 case GlobalValue::AppendingLinkage:
1157 return LLVMAppendingLinkage;
1158 case GlobalValue::InternalLinkage:
1159 return LLVMInternalLinkage;
1160 case GlobalValue::PrivateLinkage:
1161 return LLVMPrivateLinkage;
1162 case GlobalValue::ExternalWeakLinkage:
1163 return LLVMExternalWeakLinkage;
1164 case GlobalValue::CommonLinkage:
1165 return LLVMCommonLinkage;
1168 llvm_unreachable("Invalid GlobalValue linkage!");
1171 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1172 GlobalValue *GV = unwrap<GlobalValue>(Global);
1175 case LLVMExternalLinkage:
1176 GV->setLinkage(GlobalValue::ExternalLinkage);
1178 case LLVMAvailableExternallyLinkage:
1179 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1181 case LLVMLinkOnceAnyLinkage:
1182 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1184 case LLVMLinkOnceODRLinkage:
1185 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1187 case LLVMLinkOnceODRAutoHideLinkage:
1188 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1189 "longer supported.");
1191 case LLVMWeakAnyLinkage:
1192 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1194 case LLVMWeakODRLinkage:
1195 GV->setLinkage(GlobalValue::WeakODRLinkage);
1197 case LLVMAppendingLinkage:
1198 GV->setLinkage(GlobalValue::AppendingLinkage);
1200 case LLVMInternalLinkage:
1201 GV->setLinkage(GlobalValue::InternalLinkage);
1203 case LLVMPrivateLinkage:
1204 GV->setLinkage(GlobalValue::PrivateLinkage);
1206 case LLVMLinkerPrivateLinkage:
1207 GV->setLinkage(GlobalValue::PrivateLinkage);
1209 case LLVMLinkerPrivateWeakLinkage:
1210 GV->setLinkage(GlobalValue::PrivateLinkage);
1212 case LLVMDLLImportLinkage:
1214 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1216 case LLVMDLLExportLinkage:
1218 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1220 case LLVMExternalWeakLinkage:
1221 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1223 case LLVMGhostLinkage:
1225 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1227 case LLVMCommonLinkage:
1228 GV->setLinkage(GlobalValue::CommonLinkage);
1233 const char *LLVMGetSection(LLVMValueRef Global) {
1234 return unwrap<GlobalValue>(Global)->getSection().c_str();
1237 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1238 unwrap<GlobalValue>(Global)->setSection(Section);
1241 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1242 return static_cast<LLVMVisibility>(
1243 unwrap<GlobalValue>(Global)->getVisibility());
1246 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1247 unwrap<GlobalValue>(Global)
1248 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1251 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1252 return static_cast<LLVMDLLStorageClass>(
1253 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1256 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1257 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1258 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1261 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1262 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1265 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1266 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1269 /*--.. Operations on global variables, load and store instructions .........--*/
1271 unsigned LLVMGetAlignment(LLVMValueRef V) {
1272 Value *P = unwrap<Value>(V);
1273 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1274 return GV->getAlignment();
1275 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1276 return AI->getAlignment();
1277 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1278 return LI->getAlignment();
1279 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1280 return SI->getAlignment();
1283 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1286 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1287 Value *P = unwrap<Value>(V);
1288 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1289 GV->setAlignment(Bytes);
1290 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1291 AI->setAlignment(Bytes);
1292 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1293 LI->setAlignment(Bytes);
1294 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1295 SI->setAlignment(Bytes);
1298 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1301 /*--.. Operations on global variables ......................................--*/
1303 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1304 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1305 GlobalValue::ExternalLinkage, 0, Name));
1308 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1310 unsigned AddressSpace) {
1311 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1312 GlobalValue::ExternalLinkage, 0, Name, 0,
1313 GlobalVariable::NotThreadLocal, AddressSpace));
1316 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1317 return wrap(unwrap(M)->getNamedGlobal(Name));
1320 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1321 Module *Mod = unwrap(M);
1322 Module::global_iterator I = Mod->global_begin();
1323 if (I == Mod->global_end())
1328 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1329 Module *Mod = unwrap(M);
1330 Module::global_iterator I = Mod->global_end();
1331 if (I == Mod->global_begin())
1336 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1337 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1338 Module::global_iterator I = GV;
1339 if (++I == GV->getParent()->global_end())
1344 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1345 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1346 Module::global_iterator I = GV;
1347 if (I == GV->getParent()->global_begin())
1352 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1353 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1356 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1357 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1358 if ( !GV->hasInitializer() )
1360 return wrap(GV->getInitializer());
1363 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1364 unwrap<GlobalVariable>(GlobalVar)
1365 ->setInitializer(unwrap<Constant>(ConstantVal));
1368 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1369 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1372 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1373 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1376 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1377 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1380 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1381 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1384 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1385 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1386 case GlobalVariable::NotThreadLocal:
1387 return LLVMNotThreadLocal;
1388 case GlobalVariable::GeneralDynamicTLSModel:
1389 return LLVMGeneralDynamicTLSModel;
1390 case GlobalVariable::LocalDynamicTLSModel:
1391 return LLVMLocalDynamicTLSModel;
1392 case GlobalVariable::InitialExecTLSModel:
1393 return LLVMInitialExecTLSModel;
1394 case GlobalVariable::LocalExecTLSModel:
1395 return LLVMLocalExecTLSModel;
1398 llvm_unreachable("Invalid GlobalVariable thread local mode");
1401 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1402 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1405 case LLVMNotThreadLocal:
1406 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1408 case LLVMGeneralDynamicTLSModel:
1409 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1411 case LLVMLocalDynamicTLSModel:
1412 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1414 case LLVMInitialExecTLSModel:
1415 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1417 case LLVMLocalExecTLSModel:
1418 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1423 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1424 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1427 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1428 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1431 /*--.. Operations on aliases ......................................--*/
1433 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1435 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1436 unwrap<Constant>(Aliasee), unwrap (M)));
1439 /*--.. Operations on functions .............................................--*/
1441 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1442 LLVMTypeRef FunctionTy) {
1443 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1444 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1447 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1448 return wrap(unwrap(M)->getFunction(Name));
1451 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1452 Module *Mod = unwrap(M);
1453 Module::iterator I = Mod->begin();
1454 if (I == Mod->end())
1459 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1460 Module *Mod = unwrap(M);
1461 Module::iterator I = Mod->end();
1462 if (I == Mod->begin())
1467 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1468 Function *Func = unwrap<Function>(Fn);
1469 Module::iterator I = Func;
1470 if (++I == Func->getParent()->end())
1475 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1476 Function *Func = unwrap<Function>(Fn);
1477 Module::iterator I = Func;
1478 if (I == Func->getParent()->begin())
1483 void LLVMDeleteFunction(LLVMValueRef Fn) {
1484 unwrap<Function>(Fn)->eraseFromParent();
1487 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1488 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1489 return F->getIntrinsicID();
1493 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1494 return unwrap<Function>(Fn)->getCallingConv();
1497 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1498 return unwrap<Function>(Fn)->setCallingConv(
1499 static_cast<CallingConv::ID>(CC));
1502 const char *LLVMGetGC(LLVMValueRef Fn) {
1503 Function *F = unwrap<Function>(Fn);
1504 return F->hasGC()? F->getGC() : 0;
1507 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1508 Function *F = unwrap<Function>(Fn);
1515 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1516 Function *Func = unwrap<Function>(Fn);
1517 const AttributeSet PAL = Func->getAttributes();
1519 const AttributeSet PALnew =
1520 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1521 AttributeSet::get(Func->getContext(),
1522 AttributeSet::FunctionIndex, B));
1523 Func->setAttributes(PALnew);
1526 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1528 Function *Func = unwrap<Function>(Fn);
1529 AttributeSet::AttrIndex Idx =
1530 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1533 B.addAttribute(A, V);
1534 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1535 Func->addAttributes(Idx, Set);
1538 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1539 Function *Func = unwrap<Function>(Fn);
1540 const AttributeSet PAL = Func->getAttributes();
1542 const AttributeSet PALnew =
1543 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1544 AttributeSet::get(Func->getContext(),
1545 AttributeSet::FunctionIndex, B));
1546 Func->setAttributes(PALnew);
1549 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1550 Function *Func = unwrap<Function>(Fn);
1551 const AttributeSet PAL = Func->getAttributes();
1552 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1555 /*--.. Operations on parameters ............................................--*/
1557 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1558 // This function is strictly redundant to
1559 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1560 return unwrap<Function>(FnRef)->arg_size();
1563 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1564 Function *Fn = unwrap<Function>(FnRef);
1565 for (Function::arg_iterator I = Fn->arg_begin(),
1566 E = Fn->arg_end(); I != E; I++)
1567 *ParamRefs++ = wrap(I);
1570 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1571 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1577 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1578 return wrap(unwrap<Argument>(V)->getParent());
1581 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1582 Function *Func = unwrap<Function>(Fn);
1583 Function::arg_iterator I = Func->arg_begin();
1584 if (I == Func->arg_end())
1589 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1590 Function *Func = unwrap<Function>(Fn);
1591 Function::arg_iterator I = Func->arg_end();
1592 if (I == Func->arg_begin())
1597 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1598 Argument *A = unwrap<Argument>(Arg);
1599 Function::arg_iterator I = A;
1600 if (++I == A->getParent()->arg_end())
1605 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1606 Argument *A = unwrap<Argument>(Arg);
1607 Function::arg_iterator I = A;
1608 if (I == A->getParent()->arg_begin())
1613 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1614 Argument *A = unwrap<Argument>(Arg);
1616 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1619 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1620 Argument *A = unwrap<Argument>(Arg);
1622 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1625 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1626 Argument *A = unwrap<Argument>(Arg);
1627 return (LLVMAttribute)A->getParent()->getAttributes().
1628 Raw(A->getArgNo()+1);
1632 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1633 Argument *A = unwrap<Argument>(Arg);
1635 B.addAlignmentAttr(align);
1636 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1639 /*--.. Operations on basic blocks ..........................................--*/
1641 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1642 return wrap(static_cast<Value*>(unwrap(BB)));
1645 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1646 return isa<BasicBlock>(unwrap(Val));
1649 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1650 return wrap(unwrap<BasicBlock>(Val));
1653 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1654 return wrap(unwrap(BB)->getParent());
1657 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1658 return wrap(unwrap(BB)->getTerminator());
1661 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1662 return unwrap<Function>(FnRef)->size();
1665 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1666 Function *Fn = unwrap<Function>(FnRef);
1667 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1668 *BasicBlocksRefs++ = wrap(I);
1671 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1672 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1675 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1676 Function *Func = unwrap<Function>(Fn);
1677 Function::iterator I = Func->begin();
1678 if (I == Func->end())
1683 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1684 Function *Func = unwrap<Function>(Fn);
1685 Function::iterator I = Func->end();
1686 if (I == Func->begin())
1691 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1692 BasicBlock *Block = unwrap(BB);
1693 Function::iterator I = Block;
1694 if (++I == Block->getParent()->end())
1699 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1700 BasicBlock *Block = unwrap(BB);
1701 Function::iterator I = Block;
1702 if (I == Block->getParent()->begin())
1707 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1710 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1713 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1714 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1717 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1718 LLVMBasicBlockRef BBRef,
1720 BasicBlock *BB = unwrap(BBRef);
1721 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1724 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1726 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1729 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1730 unwrap(BBRef)->eraseFromParent();
1733 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1734 unwrap(BBRef)->removeFromParent();
1737 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1738 unwrap(BB)->moveBefore(unwrap(MovePos));
1741 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1742 unwrap(BB)->moveAfter(unwrap(MovePos));
1745 /*--.. Operations on instructions ..........................................--*/
1747 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1748 return wrap(unwrap<Instruction>(Inst)->getParent());
1751 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1752 BasicBlock *Block = unwrap(BB);
1753 BasicBlock::iterator I = Block->begin();
1754 if (I == Block->end())
1759 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1760 BasicBlock *Block = unwrap(BB);
1761 BasicBlock::iterator I = Block->end();
1762 if (I == Block->begin())
1767 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1768 Instruction *Instr = unwrap<Instruction>(Inst);
1769 BasicBlock::iterator I = Instr;
1770 if (++I == Instr->getParent()->end())
1775 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1776 Instruction *Instr = unwrap<Instruction>(Inst);
1777 BasicBlock::iterator I = Instr;
1778 if (I == Instr->getParent()->begin())
1783 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1784 unwrap<Instruction>(Inst)->eraseFromParent();
1787 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1788 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1789 return (LLVMIntPredicate)I->getPredicate();
1790 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1791 if (CE->getOpcode() == Instruction::ICmp)
1792 return (LLVMIntPredicate)CE->getPredicate();
1793 return (LLVMIntPredicate)0;
1796 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1797 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1798 return map_to_llvmopcode(C->getOpcode());
1799 return (LLVMOpcode)0;
1802 /*--.. Call and invoke instructions ........................................--*/
1804 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1805 Value *V = unwrap(Instr);
1806 if (CallInst *CI = dyn_cast<CallInst>(V))
1807 return CI->getCallingConv();
1808 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1809 return II->getCallingConv();
1810 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1813 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1814 Value *V = unwrap(Instr);
1815 if (CallInst *CI = dyn_cast<CallInst>(V))
1816 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1817 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1818 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1819 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1822 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1824 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1827 Call.getAttributes().addAttributes(Call->getContext(), index,
1828 AttributeSet::get(Call->getContext(),
1832 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1834 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1836 Call.setAttributes(Call.getAttributes()
1837 .removeAttributes(Call->getContext(), index,
1838 AttributeSet::get(Call->getContext(),
1842 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1844 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1846 B.addAlignmentAttr(align);
1847 Call.setAttributes(Call.getAttributes()
1848 .addAttributes(Call->getContext(), index,
1849 AttributeSet::get(Call->getContext(),
1853 /*--.. Operations on call instructions (only) ..............................--*/
1855 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1856 return unwrap<CallInst>(Call)->isTailCall();
1859 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1860 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1863 /*--.. Operations on switch instructions (only) ............................--*/
1865 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1866 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1869 /*--.. Operations on phi nodes .............................................--*/
1871 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1872 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1873 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1874 for (unsigned I = 0; I != Count; ++I)
1875 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1878 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1879 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1882 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1883 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1886 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1887 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1891 /*===-- Instruction builders ----------------------------------------------===*/
1893 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1894 return wrap(new IRBuilder<>(*unwrap(C)));
1897 LLVMBuilderRef LLVMCreateBuilder(void) {
1898 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1901 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1902 LLVMValueRef Instr) {
1903 BasicBlock *BB = unwrap(Block);
1904 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1905 unwrap(Builder)->SetInsertPoint(BB, I);
1908 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1909 Instruction *I = unwrap<Instruction>(Instr);
1910 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1913 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1914 BasicBlock *BB = unwrap(Block);
1915 unwrap(Builder)->SetInsertPoint(BB);
1918 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1919 return wrap(unwrap(Builder)->GetInsertBlock());
1922 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1923 unwrap(Builder)->ClearInsertionPoint();
1926 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1927 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1930 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1932 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1935 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1936 delete unwrap(Builder);
1939 /*--.. Metadata builders ...................................................--*/
1941 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1942 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1943 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1946 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1947 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1948 .getAsMDNode(unwrap(Builder)->getContext()));
1951 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1952 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1956 /*--.. Instruction builders ................................................--*/
1958 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1959 return wrap(unwrap(B)->CreateRetVoid());
1962 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1963 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1966 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1968 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1971 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1972 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1975 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1976 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1977 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1980 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1981 LLVMBasicBlockRef Else, unsigned NumCases) {
1982 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1985 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1986 unsigned NumDests) {
1987 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1990 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1991 LLVMValueRef *Args, unsigned NumArgs,
1992 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1994 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1995 makeArrayRef(unwrap(Args), NumArgs),
1999 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2000 LLVMValueRef PersFn, unsigned NumClauses,
2002 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2003 cast<Function>(unwrap(PersFn)),
2007 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2008 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2011 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2012 return wrap(unwrap(B)->CreateUnreachable());
2015 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2016 LLVMBasicBlockRef Dest) {
2017 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2020 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2021 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2024 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2025 unwrap<LandingPadInst>(LandingPad)->
2026 addClause(cast<Constant>(unwrap(ClauseVal)));
2029 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2030 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2033 /*--.. Arithmetic ..........................................................--*/
2035 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2037 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2040 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2042 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2045 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2047 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2050 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2052 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2055 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2057 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2060 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2062 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2065 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2067 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2070 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2072 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2075 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2077 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2080 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2082 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2085 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2087 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2090 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2092 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2095 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2097 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2100 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2102 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2105 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2106 LLVMValueRef RHS, const char *Name) {
2107 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2110 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2112 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2115 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2117 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2120 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2122 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2125 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2127 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2130 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2132 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2135 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2137 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2140 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2142 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2145 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2147 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2150 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2152 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2155 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2157 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2160 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2161 LLVMValueRef LHS, LLVMValueRef RHS,
2163 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2164 unwrap(RHS), Name));
2167 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2168 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2171 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2173 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2176 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2178 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2181 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2182 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2185 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2186 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2189 /*--.. Memory ..............................................................--*/
2191 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2193 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2194 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2195 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2196 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2197 ITy, unwrap(Ty), AllocSize,
2199 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2202 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2203 LLVMValueRef Val, const char *Name) {
2204 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2205 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2206 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2207 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2208 ITy, unwrap(Ty), AllocSize,
2209 unwrap(Val), 0, "");
2210 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2213 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2215 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2218 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2219 LLVMValueRef Val, const char *Name) {
2220 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2223 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2224 return wrap(unwrap(B)->Insert(
2225 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2229 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2231 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2234 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2235 LLVMValueRef PointerVal) {
2236 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2239 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2241 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2242 case LLVMAtomicOrderingUnordered: return Unordered;
2243 case LLVMAtomicOrderingMonotonic: return Monotonic;
2244 case LLVMAtomicOrderingAcquire: return Acquire;
2245 case LLVMAtomicOrderingRelease: return Release;
2246 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2247 case LLVMAtomicOrderingSequentiallyConsistent:
2248 return SequentiallyConsistent;
2251 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2254 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2255 LLVMBool isSingleThread, const char *Name) {
2257 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2258 isSingleThread ? SingleThread : CrossThread,
2262 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2263 LLVMValueRef *Indices, unsigned NumIndices,
2265 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2266 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2269 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2270 LLVMValueRef *Indices, unsigned NumIndices,
2272 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2273 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2276 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2277 unsigned Idx, const char *Name) {
2278 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2281 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2283 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2286 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2288 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2291 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2292 Value *P = unwrap<Value>(MemAccessInst);
2293 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2294 return LI->isVolatile();
2295 return cast<StoreInst>(P)->isVolatile();
2298 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2299 Value *P = unwrap<Value>(MemAccessInst);
2300 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2301 return LI->setVolatile(isVolatile);
2302 return cast<StoreInst>(P)->setVolatile(isVolatile);
2305 /*--.. Casts ...............................................................--*/
2307 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2308 LLVMTypeRef DestTy, const char *Name) {
2309 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2312 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2313 LLVMTypeRef DestTy, const char *Name) {
2314 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2317 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2318 LLVMTypeRef DestTy, const char *Name) {
2319 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2322 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2323 LLVMTypeRef DestTy, const char *Name) {
2324 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2327 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2328 LLVMTypeRef DestTy, const char *Name) {
2329 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2332 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2333 LLVMTypeRef DestTy, const char *Name) {
2334 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2337 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2338 LLVMTypeRef DestTy, const char *Name) {
2339 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2342 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2343 LLVMTypeRef DestTy, const char *Name) {
2344 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2347 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2348 LLVMTypeRef DestTy, const char *Name) {
2349 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2352 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2353 LLVMTypeRef DestTy, const char *Name) {
2354 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2357 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2358 LLVMTypeRef DestTy, const char *Name) {
2359 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2362 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2363 LLVMTypeRef DestTy, const char *Name) {
2364 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2367 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2368 LLVMTypeRef DestTy, const char *Name) {
2369 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2372 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2373 LLVMTypeRef DestTy, const char *Name) {
2374 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2378 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2379 LLVMTypeRef DestTy, const char *Name) {
2380 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2384 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2385 LLVMTypeRef DestTy, const char *Name) {
2386 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2390 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2391 LLVMTypeRef DestTy, const char *Name) {
2392 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2393 unwrap(DestTy), Name));
2396 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2397 LLVMTypeRef DestTy, const char *Name) {
2398 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2401 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2402 LLVMTypeRef DestTy, const char *Name) {
2403 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2404 /*isSigned*/true, Name));
2407 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2408 LLVMTypeRef DestTy, const char *Name) {
2409 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2412 /*--.. Comparisons .........................................................--*/
2414 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2415 LLVMValueRef LHS, LLVMValueRef RHS,
2417 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2418 unwrap(LHS), unwrap(RHS), Name));
2421 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2422 LLVMValueRef LHS, LLVMValueRef RHS,
2424 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2425 unwrap(LHS), unwrap(RHS), Name));
2428 /*--.. Miscellaneous instructions ..........................................--*/
2430 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2431 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2434 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2435 LLVMValueRef *Args, unsigned NumArgs,
2437 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2438 makeArrayRef(unwrap(Args), NumArgs),
2442 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2443 LLVMValueRef Then, LLVMValueRef Else,
2445 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2449 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2450 LLVMTypeRef Ty, const char *Name) {
2451 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2454 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2455 LLVMValueRef Index, const char *Name) {
2456 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2460 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2461 LLVMValueRef EltVal, LLVMValueRef Index,
2463 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2464 unwrap(Index), Name));
2467 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2468 LLVMValueRef V2, LLVMValueRef Mask,
2470 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2471 unwrap(Mask), Name));
2474 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2475 unsigned Index, const char *Name) {
2476 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2479 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2480 LLVMValueRef EltVal, unsigned Index,
2482 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2486 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2488 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2491 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2493 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2496 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2497 LLVMValueRef RHS, const char *Name) {
2498 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2501 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2502 LLVMValueRef PTR, LLVMValueRef Val,
2503 LLVMAtomicOrdering ordering,
2504 LLVMBool singleThread) {
2505 AtomicRMWInst::BinOp intop;
2507 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2508 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2509 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2510 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2511 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2512 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2513 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2514 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2515 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2516 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2517 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2519 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2520 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2524 /*===-- Module providers --------------------------------------------------===*/
2526 LLVMModuleProviderRef
2527 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2528 return reinterpret_cast<LLVMModuleProviderRef>(M);
2531 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2536 /*===-- Memory buffers ----------------------------------------------------===*/
2538 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2540 LLVMMemoryBufferRef *OutMemBuf,
2541 char **OutMessage) {
2543 std::unique_ptr<MemoryBuffer> MB;
2545 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2546 *OutMemBuf = wrap(MB.release());
2550 *OutMessage = strdup(ec.message().c_str());
2554 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2555 char **OutMessage) {
2556 std::unique_ptr<MemoryBuffer> MB;
2558 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2559 *OutMemBuf = wrap(MB.release());
2563 *OutMessage = strdup(ec.message().c_str());
2567 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2568 const char *InputData,
2569 size_t InputDataLength,
2570 const char *BufferName,
2571 LLVMBool RequiresNullTerminator) {
2573 return wrap(MemoryBuffer::getMemBuffer(
2574 StringRef(InputData, InputDataLength),
2575 StringRef(BufferName),
2576 RequiresNullTerminator));
2579 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2580 const char *InputData,
2581 size_t InputDataLength,
2582 const char *BufferName) {
2584 return wrap(MemoryBuffer::getMemBufferCopy(
2585 StringRef(InputData, InputDataLength),
2586 StringRef(BufferName)));
2589 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2590 return unwrap(MemBuf)->getBufferStart();
2593 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2594 return unwrap(MemBuf)->getBufferSize();
2597 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2598 delete unwrap(MemBuf);
2601 /*===-- Pass Registry -----------------------------------------------------===*/
2603 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2604 return wrap(PassRegistry::getPassRegistry());
2607 /*===-- Pass Manager ------------------------------------------------------===*/
2609 LLVMPassManagerRef LLVMCreatePassManager() {
2610 return wrap(new PassManager());
2613 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2614 return wrap(new FunctionPassManager(unwrap(M)));
2617 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2618 return LLVMCreateFunctionPassManagerForModule(
2619 reinterpret_cast<LLVMModuleRef>(P));
2622 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2623 return unwrap<PassManager>(PM)->run(*unwrap(M));
2626 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2627 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2630 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2631 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2634 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2635 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2638 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2642 /*===-- Threading ------------------------------------------------------===*/
2644 LLVMBool LLVMStartMultithreaded() {
2645 return llvm_start_multithreaded();
2648 void LLVMStopMultithreaded() {
2649 llvm_stop_multithreaded();
2652 LLVMBool LLVMIsMultithreaded() {
2653 return llvm_is_multithreaded();