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/Wrap.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/Attributes.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/InlineAsm.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/PassManager.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Support/system_error.h"
34 #include "llvm/Support/Threading.h"
41 void llvm::initializeCore(PassRegistry &Registry) {
42 initializeDominatorTreePass(Registry);
43 initializePrintModulePassPass(Registry);
44 initializePrintFunctionPassPass(Registry);
45 initializePrintBasicBlockPassPass(Registry);
46 initializeVerifierPass(Registry);
47 initializePreVerifierPass(Registry);
50 void LLVMInitializeCore(LLVMPassRegistryRef R) {
51 initializeCore(*unwrap(R));
58 /*===-- Error handling ----------------------------------------------------===*/
60 void LLVMDisposeMessage(char *Message) {
65 /*===-- Operations on contexts --------------------------------------------===*/
67 LLVMContextRef LLVMContextCreate() {
68 return wrap(new LLVMContext());
71 LLVMContextRef LLVMGetGlobalContext() {
72 return wrap(&getGlobalContext());
75 void LLVMContextDispose(LLVMContextRef C) {
79 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
81 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
84 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
85 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
89 /*===-- Operations on modules ---------------------------------------------===*/
91 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
92 return wrap(new Module(ModuleID, getGlobalContext()));
95 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
97 return wrap(new Module(ModuleID, *unwrap(C)));
100 void LLVMDisposeModule(LLVMModuleRef M) {
104 /*--.. Data layout .........................................................--*/
105 const char * LLVMGetDataLayout(LLVMModuleRef M) {
106 return unwrap(M)->getDataLayout().c_str();
109 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
110 unwrap(M)->setDataLayout(Triple);
113 /*--.. Target triple .......................................................--*/
114 const char * LLVMGetTarget(LLVMModuleRef M) {
115 return unwrap(M)->getTargetTriple().c_str();
118 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
119 unwrap(M)->setTargetTriple(Triple);
122 void LLVMDumpModule(LLVMModuleRef M) {
126 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
127 char **ErrorMessage) {
129 raw_fd_ostream dest(Filename, error);
130 if (!error.empty()) {
131 *ErrorMessage = strdup(error.c_str());
135 unwrap(M)->print(dest, NULL);
137 if (!error.empty()) {
138 *ErrorMessage = strdup(error.c_str());
145 /*--.. Operations on inline assembler ......................................--*/
146 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
147 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
151 /*--.. Operations on module contexts ......................................--*/
152 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
153 return wrap(&unwrap(M)->getContext());
157 /*===-- Operations on types -----------------------------------------------===*/
159 /*--.. Operations on all types (mostly) ....................................--*/
161 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
162 switch (unwrap(Ty)->getTypeID()) {
163 default: llvm_unreachable("Unhandled TypeID.");
165 return LLVMVoidTypeKind;
167 return LLVMHalfTypeKind;
168 case Type::FloatTyID:
169 return LLVMFloatTypeKind;
170 case Type::DoubleTyID:
171 return LLVMDoubleTypeKind;
172 case Type::X86_FP80TyID:
173 return LLVMX86_FP80TypeKind;
174 case Type::FP128TyID:
175 return LLVMFP128TypeKind;
176 case Type::PPC_FP128TyID:
177 return LLVMPPC_FP128TypeKind;
178 case Type::LabelTyID:
179 return LLVMLabelTypeKind;
180 case Type::MetadataTyID:
181 return LLVMMetadataTypeKind;
182 case Type::IntegerTyID:
183 return LLVMIntegerTypeKind;
184 case Type::FunctionTyID:
185 return LLVMFunctionTypeKind;
186 case Type::StructTyID:
187 return LLVMStructTypeKind;
188 case Type::ArrayTyID:
189 return LLVMArrayTypeKind;
190 case Type::PointerTyID:
191 return LLVMPointerTypeKind;
192 case Type::VectorTyID:
193 return LLVMVectorTypeKind;
194 case Type::X86_MMXTyID:
195 return LLVMX86_MMXTypeKind;
199 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
201 return unwrap(Ty)->isSized();
204 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
205 return wrap(&unwrap(Ty)->getContext());
208 /*--.. Operations on integer types .........................................--*/
210 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
211 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
213 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
214 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
216 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
217 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
219 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
220 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
222 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
223 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
225 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
226 return wrap(IntegerType::get(*unwrap(C), NumBits));
229 LLVMTypeRef LLVMInt1Type(void) {
230 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
232 LLVMTypeRef LLVMInt8Type(void) {
233 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
235 LLVMTypeRef LLVMInt16Type(void) {
236 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
238 LLVMTypeRef LLVMInt32Type(void) {
239 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
241 LLVMTypeRef LLVMInt64Type(void) {
242 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
244 LLVMTypeRef LLVMIntType(unsigned NumBits) {
245 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
248 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
249 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
252 /*--.. Operations on real types ............................................--*/
254 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
255 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
257 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
258 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
260 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
261 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
263 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
264 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
266 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
267 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
269 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
270 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
272 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
273 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
276 LLVMTypeRef LLVMHalfType(void) {
277 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
279 LLVMTypeRef LLVMFloatType(void) {
280 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
282 LLVMTypeRef LLVMDoubleType(void) {
283 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
285 LLVMTypeRef LLVMX86FP80Type(void) {
286 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
288 LLVMTypeRef LLVMFP128Type(void) {
289 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
291 LLVMTypeRef LLVMPPCFP128Type(void) {
292 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
294 LLVMTypeRef LLVMX86MMXType(void) {
295 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
298 /*--.. Operations on function types ........................................--*/
300 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
301 LLVMTypeRef *ParamTypes, unsigned ParamCount,
303 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
304 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
307 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
308 return unwrap<FunctionType>(FunctionTy)->isVarArg();
311 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
312 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
315 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
316 return unwrap<FunctionType>(FunctionTy)->getNumParams();
319 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
320 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
321 for (FunctionType::param_iterator I = Ty->param_begin(),
322 E = Ty->param_end(); I != E; ++I)
326 /*--.. Operations on struct types ..........................................--*/
328 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
329 unsigned ElementCount, LLVMBool Packed) {
330 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
331 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
334 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
335 unsigned ElementCount, LLVMBool Packed) {
336 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
337 ElementCount, Packed);
340 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
342 return wrap(StructType::create(*unwrap(C), Name));
345 const char *LLVMGetStructName(LLVMTypeRef Ty)
347 StructType *Type = unwrap<StructType>(Ty);
348 if (!Type->hasName())
350 return Type->getName().data();
353 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
354 unsigned ElementCount, LLVMBool Packed) {
355 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
356 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
359 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
360 return unwrap<StructType>(StructTy)->getNumElements();
363 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
364 StructType *Ty = unwrap<StructType>(StructTy);
365 for (StructType::element_iterator I = Ty->element_begin(),
366 E = Ty->element_end(); I != E; ++I)
370 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
371 return unwrap<StructType>(StructTy)->isPacked();
374 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
375 return unwrap<StructType>(StructTy)->isOpaque();
378 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
379 return wrap(unwrap(M)->getTypeByName(Name));
382 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
384 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
385 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
388 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
389 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
392 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
393 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
396 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
397 return wrap(unwrap<SequentialType>(Ty)->getElementType());
400 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
401 return unwrap<ArrayType>(ArrayTy)->getNumElements();
404 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
405 return unwrap<PointerType>(PointerTy)->getAddressSpace();
408 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
409 return unwrap<VectorType>(VectorTy)->getNumElements();
412 /*--.. Operations on other types ...........................................--*/
414 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
415 return wrap(Type::getVoidTy(*unwrap(C)));
417 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
418 return wrap(Type::getLabelTy(*unwrap(C)));
421 LLVMTypeRef LLVMVoidType(void) {
422 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
424 LLVMTypeRef LLVMLabelType(void) {
425 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
428 /*===-- Operations on values ----------------------------------------------===*/
430 /*--.. Operations on all values ............................................--*/
432 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
433 return wrap(unwrap(Val)->getType());
436 const char *LLVMGetValueName(LLVMValueRef Val) {
437 return unwrap(Val)->getName().data();
440 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
441 unwrap(Val)->setName(Name);
444 void LLVMDumpValue(LLVMValueRef Val) {
448 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
449 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
452 int LLVMHasMetadata(LLVMValueRef Inst) {
453 return unwrap<Instruction>(Inst)->hasMetadata();
456 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
457 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
460 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
461 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
464 /*--.. Conversion functions ................................................--*/
466 #define LLVM_DEFINE_VALUE_CAST(name) \
467 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
468 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
471 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
473 /*--.. Operations on Uses ..................................................--*/
474 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
475 Value *V = unwrap(Val);
476 Value::use_iterator I = V->use_begin();
477 if (I == V->use_end())
479 return wrap(&(I.getUse()));
482 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
483 Use *Next = unwrap(U)->getNext();
489 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
490 return wrap(unwrap(U)->getUser());
493 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
494 return wrap(unwrap(U)->get());
497 /*--.. Operations on Users .................................................--*/
498 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
499 Value *V = unwrap(Val);
500 if (MDNode *MD = dyn_cast<MDNode>(V))
501 return wrap(MD->getOperand(Index));
502 return wrap(cast<User>(V)->getOperand(Index));
505 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
506 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
509 int LLVMGetNumOperands(LLVMValueRef Val) {
510 Value *V = unwrap(Val);
511 if (MDNode *MD = dyn_cast<MDNode>(V))
512 return MD->getNumOperands();
513 return cast<User>(V)->getNumOperands();
516 /*--.. Operations on constants of any type .................................--*/
518 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
519 return wrap(Constant::getNullValue(unwrap(Ty)));
522 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
523 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
526 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
527 return wrap(UndefValue::get(unwrap(Ty)));
530 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
531 return isa<Constant>(unwrap(Ty));
534 LLVMBool LLVMIsNull(LLVMValueRef Val) {
535 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
536 return C->isNullValue();
540 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
541 return isa<UndefValue>(unwrap(Val));
544 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
546 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
549 /*--.. Operations on metadata nodes ........................................--*/
551 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
553 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
556 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
557 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
560 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
562 return wrap(MDNode::get(*unwrap(C),
563 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
566 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
567 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
570 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
571 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
572 *Len = S->getString().size();
573 return S->getString().data();
579 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
581 return cast<MDNode>(unwrap(V))->getNumOperands();
584 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
586 const MDNode *N = cast<MDNode>(unwrap(V));
587 const unsigned numOperands = N->getNumOperands();
588 for (unsigned i = 0; i < numOperands; i++)
589 Dest[i] = wrap(N->getOperand(i));
592 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
594 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
595 return N->getNumOperands();
600 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
602 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
605 for (unsigned i=0;i<N->getNumOperands();i++)
606 Dest[i] = wrap(N->getOperand(i));
609 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
612 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
615 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
620 /*--.. Operations on scalar constants ......................................--*/
622 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
623 LLVMBool SignExtend) {
624 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
627 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
629 const uint64_t Words[]) {
630 IntegerType *Ty = unwrap<IntegerType>(IntTy);
631 return wrap(ConstantInt::get(Ty->getContext(),
632 APInt(Ty->getBitWidth(),
633 makeArrayRef(Words, NumWords))));
636 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
638 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
642 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
643 unsigned SLen, uint8_t Radix) {
644 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
648 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
649 return wrap(ConstantFP::get(unwrap(RealTy), N));
652 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
653 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
656 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
658 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
661 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
662 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
665 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
666 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
669 /*--.. Operations on composite constants ...................................--*/
671 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
673 LLVMBool DontNullTerminate) {
674 /* Inverted the sense of AddNull because ', 0)' is a
675 better mnemonic for null termination than ', 1)'. */
676 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
677 DontNullTerminate == 0));
679 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
680 LLVMValueRef *ConstantVals,
681 unsigned Count, LLVMBool Packed) {
682 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
683 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
687 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
688 LLVMBool DontNullTerminate) {
689 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
692 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
693 LLVMValueRef *ConstantVals, unsigned Length) {
694 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
695 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
697 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
699 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
703 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
704 LLVMValueRef *ConstantVals,
706 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
707 StructType *Ty = cast<StructType>(unwrap(StructTy));
709 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
712 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
713 return wrap(ConstantVector::get(makeArrayRef(
714 unwrap<Constant>(ScalarConstantVals, Size), Size)));
717 /*-- Opcode mapping */
719 static LLVMOpcode map_to_llvmopcode(int opcode)
722 default: llvm_unreachable("Unhandled Opcode.");
723 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
724 #include "llvm/IR/Instruction.def"
729 static int map_from_llvmopcode(LLVMOpcode code)
732 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
733 #include "llvm/IR/Instruction.def"
736 llvm_unreachable("Unhandled Opcode.");
739 /*--.. Constant expressions ................................................--*/
741 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
742 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
745 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
746 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
749 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
750 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
753 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
754 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
757 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
758 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
761 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
762 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
766 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
767 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
770 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
771 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
774 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
775 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
776 unwrap<Constant>(RHSConstant)));
779 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
780 LLVMValueRef RHSConstant) {
781 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
782 unwrap<Constant>(RHSConstant)));
785 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
786 LLVMValueRef RHSConstant) {
787 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
788 unwrap<Constant>(RHSConstant)));
791 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
792 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
793 unwrap<Constant>(RHSConstant)));
796 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
797 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
798 unwrap<Constant>(RHSConstant)));
801 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
802 LLVMValueRef RHSConstant) {
803 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
804 unwrap<Constant>(RHSConstant)));
807 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
808 LLVMValueRef RHSConstant) {
809 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
810 unwrap<Constant>(RHSConstant)));
813 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
814 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
815 unwrap<Constant>(RHSConstant)));
818 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
819 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
820 unwrap<Constant>(RHSConstant)));
823 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
824 LLVMValueRef RHSConstant) {
825 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
826 unwrap<Constant>(RHSConstant)));
829 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
830 LLVMValueRef RHSConstant) {
831 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
832 unwrap<Constant>(RHSConstant)));
835 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
836 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
837 unwrap<Constant>(RHSConstant)));
840 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
841 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
842 unwrap<Constant>(RHSConstant)));
845 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
846 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
847 unwrap<Constant>(RHSConstant)));
850 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
851 LLVMValueRef RHSConstant) {
852 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
853 unwrap<Constant>(RHSConstant)));
856 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
857 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
858 unwrap<Constant>(RHSConstant)));
861 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
862 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
863 unwrap<Constant>(RHSConstant)));
866 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
867 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
868 unwrap<Constant>(RHSConstant)));
871 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
872 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
873 unwrap<Constant>(RHSConstant)));
876 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
877 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
878 unwrap<Constant>(RHSConstant)));
881 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
882 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
883 unwrap<Constant>(RHSConstant)));
886 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
887 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
888 unwrap<Constant>(RHSConstant)));
891 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
892 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
893 return wrap(ConstantExpr::getICmp(Predicate,
894 unwrap<Constant>(LHSConstant),
895 unwrap<Constant>(RHSConstant)));
898 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
899 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
900 return wrap(ConstantExpr::getFCmp(Predicate,
901 unwrap<Constant>(LHSConstant),
902 unwrap<Constant>(RHSConstant)));
905 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
906 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
907 unwrap<Constant>(RHSConstant)));
910 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
911 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
912 unwrap<Constant>(RHSConstant)));
915 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
916 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
921 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
922 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
924 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
928 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
929 LLVMValueRef *ConstantIndices,
930 unsigned NumIndices) {
931 Constant* Val = unwrap<Constant>(ConstantVal);
932 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
934 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
937 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
938 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
942 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
943 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
947 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
948 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
952 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
953 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
957 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
958 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
962 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
963 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
967 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
968 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
972 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
973 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
977 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
978 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
982 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
983 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
987 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
988 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
992 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
993 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
997 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
998 LLVMTypeRef ToType) {
999 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1003 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1004 LLVMTypeRef ToType) {
1005 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1009 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1010 LLVMTypeRef ToType) {
1011 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1015 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1016 LLVMTypeRef ToType) {
1017 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1021 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1022 LLVMBool isSigned) {
1023 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1024 unwrap(ToType), isSigned));
1027 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1028 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1032 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1033 LLVMValueRef ConstantIfTrue,
1034 LLVMValueRef ConstantIfFalse) {
1035 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1036 unwrap<Constant>(ConstantIfTrue),
1037 unwrap<Constant>(ConstantIfFalse)));
1040 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1041 LLVMValueRef IndexConstant) {
1042 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1043 unwrap<Constant>(IndexConstant)));
1046 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1047 LLVMValueRef ElementValueConstant,
1048 LLVMValueRef IndexConstant) {
1049 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1050 unwrap<Constant>(ElementValueConstant),
1051 unwrap<Constant>(IndexConstant)));
1054 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1055 LLVMValueRef VectorBConstant,
1056 LLVMValueRef MaskConstant) {
1057 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1058 unwrap<Constant>(VectorBConstant),
1059 unwrap<Constant>(MaskConstant)));
1062 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1064 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1065 makeArrayRef(IdxList, NumIdx)));
1068 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1069 LLVMValueRef ElementValueConstant,
1070 unsigned *IdxList, unsigned NumIdx) {
1071 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1072 unwrap<Constant>(ElementValueConstant),
1073 makeArrayRef(IdxList, NumIdx)));
1076 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1077 const char *Constraints,
1078 LLVMBool HasSideEffects,
1079 LLVMBool IsAlignStack) {
1080 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1081 Constraints, HasSideEffects, IsAlignStack));
1084 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1085 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1088 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1090 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1091 return wrap(unwrap<GlobalValue>(Global)->getParent());
1094 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1095 return unwrap<GlobalValue>(Global)->isDeclaration();
1098 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1099 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1100 case GlobalValue::ExternalLinkage:
1101 return LLVMExternalLinkage;
1102 case GlobalValue::AvailableExternallyLinkage:
1103 return LLVMAvailableExternallyLinkage;
1104 case GlobalValue::LinkOnceAnyLinkage:
1105 return LLVMLinkOnceAnyLinkage;
1106 case GlobalValue::LinkOnceODRLinkage:
1107 return LLVMLinkOnceODRLinkage;
1108 case GlobalValue::LinkOnceODRAutoHideLinkage:
1109 return LLVMLinkOnceODRAutoHideLinkage;
1110 case GlobalValue::WeakAnyLinkage:
1111 return LLVMWeakAnyLinkage;
1112 case GlobalValue::WeakODRLinkage:
1113 return LLVMWeakODRLinkage;
1114 case GlobalValue::AppendingLinkage:
1115 return LLVMAppendingLinkage;
1116 case GlobalValue::InternalLinkage:
1117 return LLVMInternalLinkage;
1118 case GlobalValue::PrivateLinkage:
1119 return LLVMPrivateLinkage;
1120 case GlobalValue::LinkerPrivateLinkage:
1121 return LLVMLinkerPrivateLinkage;
1122 case GlobalValue::LinkerPrivateWeakLinkage:
1123 return LLVMLinkerPrivateWeakLinkage;
1124 case GlobalValue::DLLImportLinkage:
1125 return LLVMDLLImportLinkage;
1126 case GlobalValue::DLLExportLinkage:
1127 return LLVMDLLExportLinkage;
1128 case GlobalValue::ExternalWeakLinkage:
1129 return LLVMExternalWeakLinkage;
1130 case GlobalValue::CommonLinkage:
1131 return LLVMCommonLinkage;
1134 llvm_unreachable("Invalid GlobalValue linkage!");
1137 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1138 GlobalValue *GV = unwrap<GlobalValue>(Global);
1141 case LLVMExternalLinkage:
1142 GV->setLinkage(GlobalValue::ExternalLinkage);
1144 case LLVMAvailableExternallyLinkage:
1145 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1147 case LLVMLinkOnceAnyLinkage:
1148 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1150 case LLVMLinkOnceODRLinkage:
1151 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1153 case LLVMLinkOnceODRAutoHideLinkage:
1154 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1156 case LLVMWeakAnyLinkage:
1157 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1159 case LLVMWeakODRLinkage:
1160 GV->setLinkage(GlobalValue::WeakODRLinkage);
1162 case LLVMAppendingLinkage:
1163 GV->setLinkage(GlobalValue::AppendingLinkage);
1165 case LLVMInternalLinkage:
1166 GV->setLinkage(GlobalValue::InternalLinkage);
1168 case LLVMPrivateLinkage:
1169 GV->setLinkage(GlobalValue::PrivateLinkage);
1171 case LLVMLinkerPrivateLinkage:
1172 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1174 case LLVMLinkerPrivateWeakLinkage:
1175 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1177 case LLVMDLLImportLinkage:
1178 GV->setLinkage(GlobalValue::DLLImportLinkage);
1180 case LLVMDLLExportLinkage:
1181 GV->setLinkage(GlobalValue::DLLExportLinkage);
1183 case LLVMExternalWeakLinkage:
1184 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1186 case LLVMGhostLinkage:
1188 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1190 case LLVMCommonLinkage:
1191 GV->setLinkage(GlobalValue::CommonLinkage);
1196 const char *LLVMGetSection(LLVMValueRef Global) {
1197 return unwrap<GlobalValue>(Global)->getSection().c_str();
1200 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1201 unwrap<GlobalValue>(Global)->setSection(Section);
1204 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1205 return static_cast<LLVMVisibility>(
1206 unwrap<GlobalValue>(Global)->getVisibility());
1209 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1210 unwrap<GlobalValue>(Global)
1211 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1214 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1215 return unwrap<GlobalValue>(Global)->getAlignment();
1218 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1219 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1222 /*--.. Operations on global variables ......................................--*/
1224 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1225 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1226 GlobalValue::ExternalLinkage, 0, Name));
1229 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1231 unsigned AddressSpace) {
1232 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1233 GlobalValue::ExternalLinkage, 0, Name, 0,
1234 GlobalVariable::NotThreadLocal, AddressSpace));
1237 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1238 return wrap(unwrap(M)->getNamedGlobal(Name));
1241 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1242 Module *Mod = unwrap(M);
1243 Module::global_iterator I = Mod->global_begin();
1244 if (I == Mod->global_end())
1249 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1250 Module *Mod = unwrap(M);
1251 Module::global_iterator I = Mod->global_end();
1252 if (I == Mod->global_begin())
1257 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1258 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1259 Module::global_iterator I = GV;
1260 if (++I == GV->getParent()->global_end())
1265 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1266 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1267 Module::global_iterator I = GV;
1268 if (I == GV->getParent()->global_begin())
1273 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1274 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1277 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1278 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1279 if ( !GV->hasInitializer() )
1281 return wrap(GV->getInitializer());
1284 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1285 unwrap<GlobalVariable>(GlobalVar)
1286 ->setInitializer(unwrap<Constant>(ConstantVal));
1289 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1290 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1293 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1294 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1297 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1298 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1301 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1302 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1305 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1306 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1307 case GlobalVariable::NotThreadLocal:
1308 return LLVMNotThreadLocal;
1309 case GlobalVariable::GeneralDynamicTLSModel:
1310 return LLVMGeneralDynamicTLSModel;
1311 case GlobalVariable::LocalDynamicTLSModel:
1312 return LLVMLocalDynamicTLSModel;
1313 case GlobalVariable::InitialExecTLSModel:
1314 return LLVMInitialExecTLSModel;
1315 case GlobalVariable::LocalExecTLSModel:
1316 return LLVMLocalExecTLSModel;
1319 llvm_unreachable("Invalid GlobalVariable thread local mode");
1322 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1323 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1326 case LLVMNotThreadLocal:
1327 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1329 case LLVMGeneralDynamicTLSModel:
1330 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1332 case LLVMLocalDynamicTLSModel:
1333 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1335 case LLVMInitialExecTLSModel:
1336 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1338 case LLVMLocalExecTLSModel:
1339 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1344 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1345 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1348 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1349 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1352 /*--.. Operations on aliases ......................................--*/
1354 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1356 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1357 unwrap<Constant>(Aliasee), unwrap (M)));
1360 /*--.. Operations on functions .............................................--*/
1362 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1363 LLVMTypeRef FunctionTy) {
1364 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1365 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1368 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1369 return wrap(unwrap(M)->getFunction(Name));
1372 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1373 Module *Mod = unwrap(M);
1374 Module::iterator I = Mod->begin();
1375 if (I == Mod->end())
1380 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1381 Module *Mod = unwrap(M);
1382 Module::iterator I = Mod->end();
1383 if (I == Mod->begin())
1388 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1389 Function *Func = unwrap<Function>(Fn);
1390 Module::iterator I = Func;
1391 if (++I == Func->getParent()->end())
1396 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1397 Function *Func = unwrap<Function>(Fn);
1398 Module::iterator I = Func;
1399 if (I == Func->getParent()->begin())
1404 void LLVMDeleteFunction(LLVMValueRef Fn) {
1405 unwrap<Function>(Fn)->eraseFromParent();
1408 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1409 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1410 return F->getIntrinsicID();
1414 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1415 return unwrap<Function>(Fn)->getCallingConv();
1418 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1419 return unwrap<Function>(Fn)->setCallingConv(
1420 static_cast<CallingConv::ID>(CC));
1423 const char *LLVMGetGC(LLVMValueRef Fn) {
1424 Function *F = unwrap<Function>(Fn);
1425 return F->hasGC()? F->getGC() : 0;
1428 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1429 Function *F = unwrap<Function>(Fn);
1436 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1437 Function *Func = unwrap<Function>(Fn);
1438 const AttributeSet PAL = Func->getAttributes();
1440 const AttributeSet PALnew =
1441 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1442 AttributeSet::get(Func->getContext(),
1443 AttributeSet::FunctionIndex, B));
1444 Func->setAttributes(PALnew);
1447 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1449 Function *Func = unwrap<Function>(Fn);
1450 AttributeSet::AttrIndex Idx =
1451 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1454 B.addAttribute(A, V);
1455 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1456 Func->addAttributes(Idx, Set);
1459 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1460 Function *Func = unwrap<Function>(Fn);
1461 const AttributeSet PAL = Func->getAttributes();
1463 const AttributeSet PALnew =
1464 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1465 AttributeSet::get(Func->getContext(),
1466 AttributeSet::FunctionIndex, B));
1467 Func->setAttributes(PALnew);
1470 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1471 Function *Func = unwrap<Function>(Fn);
1472 const AttributeSet PAL = Func->getAttributes();
1473 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1476 /*--.. Operations on parameters ............................................--*/
1478 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1479 // This function is strictly redundant to
1480 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1481 return unwrap<Function>(FnRef)->arg_size();
1484 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1485 Function *Fn = unwrap<Function>(FnRef);
1486 for (Function::arg_iterator I = Fn->arg_begin(),
1487 E = Fn->arg_end(); I != E; I++)
1488 *ParamRefs++ = wrap(I);
1491 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1492 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1498 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1499 return wrap(unwrap<Argument>(V)->getParent());
1502 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1503 Function *Func = unwrap<Function>(Fn);
1504 Function::arg_iterator I = Func->arg_begin();
1505 if (I == Func->arg_end())
1510 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1511 Function *Func = unwrap<Function>(Fn);
1512 Function::arg_iterator I = Func->arg_end();
1513 if (I == Func->arg_begin())
1518 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1519 Argument *A = unwrap<Argument>(Arg);
1520 Function::arg_iterator I = A;
1521 if (++I == A->getParent()->arg_end())
1526 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1527 Argument *A = unwrap<Argument>(Arg);
1528 Function::arg_iterator I = A;
1529 if (I == A->getParent()->arg_begin())
1534 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1535 Argument *A = unwrap<Argument>(Arg);
1537 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1540 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1541 Argument *A = unwrap<Argument>(Arg);
1543 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1546 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1547 Argument *A = unwrap<Argument>(Arg);
1548 return (LLVMAttribute)A->getParent()->getAttributes().
1549 Raw(A->getArgNo()+1);
1553 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1554 Argument *A = unwrap<Argument>(Arg);
1556 B.addAlignmentAttr(align);
1557 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1560 /*--.. Operations on basic blocks ..........................................--*/
1562 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1563 return wrap(static_cast<Value*>(unwrap(BB)));
1566 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1567 return isa<BasicBlock>(unwrap(Val));
1570 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1571 return wrap(unwrap<BasicBlock>(Val));
1574 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1575 return wrap(unwrap(BB)->getParent());
1578 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1579 return wrap(unwrap(BB)->getTerminator());
1582 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1583 return unwrap<Function>(FnRef)->size();
1586 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1587 Function *Fn = unwrap<Function>(FnRef);
1588 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1589 *BasicBlocksRefs++ = wrap(I);
1592 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1593 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1596 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1597 Function *Func = unwrap<Function>(Fn);
1598 Function::iterator I = Func->begin();
1599 if (I == Func->end())
1604 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1605 Function *Func = unwrap<Function>(Fn);
1606 Function::iterator I = Func->end();
1607 if (I == Func->begin())
1612 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1613 BasicBlock *Block = unwrap(BB);
1614 Function::iterator I = Block;
1615 if (++I == Block->getParent()->end())
1620 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1621 BasicBlock *Block = unwrap(BB);
1622 Function::iterator I = Block;
1623 if (I == Block->getParent()->begin())
1628 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1631 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1634 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1635 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1638 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1639 LLVMBasicBlockRef BBRef,
1641 BasicBlock *BB = unwrap(BBRef);
1642 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1645 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1647 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1650 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1651 unwrap(BBRef)->eraseFromParent();
1654 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1655 unwrap(BBRef)->removeFromParent();
1658 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1659 unwrap(BB)->moveBefore(unwrap(MovePos));
1662 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1663 unwrap(BB)->moveAfter(unwrap(MovePos));
1666 /*--.. Operations on instructions ..........................................--*/
1668 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1669 return wrap(unwrap<Instruction>(Inst)->getParent());
1672 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1673 BasicBlock *Block = unwrap(BB);
1674 BasicBlock::iterator I = Block->begin();
1675 if (I == Block->end())
1680 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1681 BasicBlock *Block = unwrap(BB);
1682 BasicBlock::iterator I = Block->end();
1683 if (I == Block->begin())
1688 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1689 Instruction *Instr = unwrap<Instruction>(Inst);
1690 BasicBlock::iterator I = Instr;
1691 if (++I == Instr->getParent()->end())
1696 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1697 Instruction *Instr = unwrap<Instruction>(Inst);
1698 BasicBlock::iterator I = Instr;
1699 if (I == Instr->getParent()->begin())
1704 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1705 unwrap<Instruction>(Inst)->eraseFromParent();
1708 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1709 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1710 return (LLVMIntPredicate)I->getPredicate();
1711 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1712 if (CE->getOpcode() == Instruction::ICmp)
1713 return (LLVMIntPredicate)CE->getPredicate();
1714 return (LLVMIntPredicate)0;
1717 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1718 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1719 return map_to_llvmopcode(C->getOpcode());
1720 return (LLVMOpcode)0;
1723 /*--.. Call and invoke instructions ........................................--*/
1725 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1726 Value *V = unwrap(Instr);
1727 if (CallInst *CI = dyn_cast<CallInst>(V))
1728 return CI->getCallingConv();
1729 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1730 return II->getCallingConv();
1731 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1734 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1735 Value *V = unwrap(Instr);
1736 if (CallInst *CI = dyn_cast<CallInst>(V))
1737 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1738 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1739 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1740 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1743 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1745 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1748 Call.getAttributes().addAttributes(Call->getContext(), index,
1749 AttributeSet::get(Call->getContext(),
1753 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1755 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1757 Call.setAttributes(Call.getAttributes()
1758 .removeAttributes(Call->getContext(), index,
1759 AttributeSet::get(Call->getContext(),
1763 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1765 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1767 B.addAlignmentAttr(align);
1768 Call.setAttributes(Call.getAttributes()
1769 .addAttributes(Call->getContext(), index,
1770 AttributeSet::get(Call->getContext(),
1774 /*--.. Operations on call instructions (only) ..............................--*/
1776 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1777 return unwrap<CallInst>(Call)->isTailCall();
1780 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1781 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1784 /*--.. Operations on switch instructions (only) ............................--*/
1786 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1787 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1790 /*--.. Operations on phi nodes .............................................--*/
1792 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1793 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1794 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1795 for (unsigned I = 0; I != Count; ++I)
1796 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1799 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1800 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1803 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1804 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1807 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1808 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1812 /*===-- Instruction builders ----------------------------------------------===*/
1814 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1815 return wrap(new IRBuilder<>(*unwrap(C)));
1818 LLVMBuilderRef LLVMCreateBuilder(void) {
1819 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1822 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1823 LLVMValueRef Instr) {
1824 BasicBlock *BB = unwrap(Block);
1825 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1826 unwrap(Builder)->SetInsertPoint(BB, I);
1829 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1830 Instruction *I = unwrap<Instruction>(Instr);
1831 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1834 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1835 BasicBlock *BB = unwrap(Block);
1836 unwrap(Builder)->SetInsertPoint(BB);
1839 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1840 return wrap(unwrap(Builder)->GetInsertBlock());
1843 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1844 unwrap(Builder)->ClearInsertionPoint();
1847 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1848 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1851 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1853 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1856 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1857 delete unwrap(Builder);
1860 /*--.. Metadata builders ...................................................--*/
1862 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1863 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1864 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1867 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1868 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1869 .getAsMDNode(unwrap(Builder)->getContext()));
1872 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1873 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1877 /*--.. Instruction builders ................................................--*/
1879 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1880 return wrap(unwrap(B)->CreateRetVoid());
1883 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1884 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1887 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1889 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1892 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1893 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1896 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1897 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1898 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1901 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1902 LLVMBasicBlockRef Else, unsigned NumCases) {
1903 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1906 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1907 unsigned NumDests) {
1908 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1911 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1912 LLVMValueRef *Args, unsigned NumArgs,
1913 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1915 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1916 makeArrayRef(unwrap(Args), NumArgs),
1920 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1921 LLVMValueRef PersFn, unsigned NumClauses,
1923 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1924 cast<Function>(unwrap(PersFn)),
1928 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1929 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1932 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1933 return wrap(unwrap(B)->CreateUnreachable());
1936 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1937 LLVMBasicBlockRef Dest) {
1938 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1941 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1942 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1945 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1946 unwrap<LandingPadInst>(LandingPad)->
1947 addClause(cast<Constant>(unwrap(ClauseVal)));
1950 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1951 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1954 /*--.. Arithmetic ..........................................................--*/
1956 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1958 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1961 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1963 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1966 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1968 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1971 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1973 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1976 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1978 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1981 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1983 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1986 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1988 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1991 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1993 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1996 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1998 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2001 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2003 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2006 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2008 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2011 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2013 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2016 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2018 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2021 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2023 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2026 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2027 LLVMValueRef RHS, const char *Name) {
2028 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2031 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2033 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2036 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2038 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2041 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2043 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2046 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2048 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2051 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2053 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2056 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2058 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2061 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2063 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2066 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2068 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2071 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2073 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2076 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2078 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2081 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2082 LLVMValueRef LHS, LLVMValueRef RHS,
2084 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2085 unwrap(RHS), Name));
2088 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2089 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2092 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2094 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2097 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2099 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2102 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2103 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2106 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2107 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2110 /*--.. Memory ..............................................................--*/
2112 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2114 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2115 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2116 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2117 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2118 ITy, unwrap(Ty), AllocSize,
2120 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2123 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2124 LLVMValueRef Val, const char *Name) {
2125 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2126 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2127 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2128 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2129 ITy, unwrap(Ty), AllocSize,
2130 unwrap(Val), 0, "");
2131 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2134 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2136 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2139 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2140 LLVMValueRef Val, const char *Name) {
2141 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2144 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2145 return wrap(unwrap(B)->Insert(
2146 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2150 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2152 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2155 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2156 LLVMValueRef PointerVal) {
2157 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2160 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2161 LLVMValueRef *Indices, unsigned NumIndices,
2163 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2164 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2167 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2168 LLVMValueRef *Indices, unsigned NumIndices,
2170 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2171 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2174 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2175 unsigned Idx, const char *Name) {
2176 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2179 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2181 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2184 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2186 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2189 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2190 Value *P = unwrap<Value>(MemAccessInst);
2191 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2192 return LI->isVolatile();
2193 return cast<StoreInst>(P)->isVolatile();
2196 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2197 Value *P = unwrap<Value>(MemAccessInst);
2198 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2199 return LI->setVolatile(isVolatile);
2200 return cast<StoreInst>(P)->setVolatile(isVolatile);
2203 /*--.. Casts ...............................................................--*/
2205 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2206 LLVMTypeRef DestTy, const char *Name) {
2207 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2210 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2211 LLVMTypeRef DestTy, const char *Name) {
2212 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2215 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2216 LLVMTypeRef DestTy, const char *Name) {
2217 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2220 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2221 LLVMTypeRef DestTy, const char *Name) {
2222 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2225 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2226 LLVMTypeRef DestTy, const char *Name) {
2227 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2230 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2231 LLVMTypeRef DestTy, const char *Name) {
2232 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2235 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2236 LLVMTypeRef DestTy, const char *Name) {
2237 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2240 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2241 LLVMTypeRef DestTy, const char *Name) {
2242 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2245 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2246 LLVMTypeRef DestTy, const char *Name) {
2247 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2250 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2251 LLVMTypeRef DestTy, const char *Name) {
2252 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2255 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2256 LLVMTypeRef DestTy, const char *Name) {
2257 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2260 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2261 LLVMTypeRef DestTy, const char *Name) {
2262 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2265 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2266 LLVMTypeRef DestTy, const char *Name) {
2267 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2271 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2272 LLVMTypeRef DestTy, const char *Name) {
2273 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2277 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2278 LLVMTypeRef DestTy, const char *Name) {
2279 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2283 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2284 LLVMTypeRef DestTy, const char *Name) {
2285 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2286 unwrap(DestTy), Name));
2289 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2290 LLVMTypeRef DestTy, const char *Name) {
2291 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2294 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2295 LLVMTypeRef DestTy, const char *Name) {
2296 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2297 /*isSigned*/true, Name));
2300 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2301 LLVMTypeRef DestTy, const char *Name) {
2302 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2305 /*--.. Comparisons .........................................................--*/
2307 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2308 LLVMValueRef LHS, LLVMValueRef RHS,
2310 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2311 unwrap(LHS), unwrap(RHS), Name));
2314 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2315 LLVMValueRef LHS, LLVMValueRef RHS,
2317 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2318 unwrap(LHS), unwrap(RHS), Name));
2321 /*--.. Miscellaneous instructions ..........................................--*/
2323 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2324 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2327 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2328 LLVMValueRef *Args, unsigned NumArgs,
2330 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2331 makeArrayRef(unwrap(Args), NumArgs),
2335 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2336 LLVMValueRef Then, LLVMValueRef Else,
2338 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2342 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2343 LLVMTypeRef Ty, const char *Name) {
2344 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2347 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2348 LLVMValueRef Index, const char *Name) {
2349 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2353 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2354 LLVMValueRef EltVal, LLVMValueRef Index,
2356 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2357 unwrap(Index), Name));
2360 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2361 LLVMValueRef V2, LLVMValueRef Mask,
2363 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2364 unwrap(Mask), Name));
2367 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2368 unsigned Index, const char *Name) {
2369 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2372 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2373 LLVMValueRef EltVal, unsigned Index,
2375 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2379 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2381 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2384 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2386 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2389 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2390 LLVMValueRef RHS, const char *Name) {
2391 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2395 /*===-- Module providers --------------------------------------------------===*/
2397 LLVMModuleProviderRef
2398 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2399 return reinterpret_cast<LLVMModuleProviderRef>(M);
2402 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2407 /*===-- Memory buffers ----------------------------------------------------===*/
2409 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2411 LLVMMemoryBufferRef *OutMemBuf,
2412 char **OutMessage) {
2414 OwningPtr<MemoryBuffer> MB;
2416 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2417 *OutMemBuf = wrap(MB.take());
2421 *OutMessage = strdup(ec.message().c_str());
2425 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2426 char **OutMessage) {
2427 OwningPtr<MemoryBuffer> MB;
2429 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2430 *OutMemBuf = wrap(MB.take());
2434 *OutMessage = strdup(ec.message().c_str());
2438 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2439 const char *InputData,
2440 size_t InputDataLength,
2441 const char *BufferName,
2442 LLVMBool RequiresNullTerminator) {
2444 return wrap(MemoryBuffer::getMemBuffer(
2445 StringRef(InputData, InputDataLength),
2446 StringRef(BufferName),
2447 RequiresNullTerminator));
2450 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2451 const char *InputData,
2452 size_t InputDataLength,
2453 const char *BufferName) {
2455 return wrap(MemoryBuffer::getMemBufferCopy(
2456 StringRef(InputData, InputDataLength),
2457 StringRef(BufferName)));
2460 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2461 return unwrap(MemBuf)->getBufferStart();
2464 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2465 return unwrap(MemBuf)->getBufferSize();
2468 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2469 delete unwrap(MemBuf);
2472 /*===-- Pass Registry -----------------------------------------------------===*/
2474 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2475 return wrap(PassRegistry::getPassRegistry());
2478 /*===-- Pass Manager ------------------------------------------------------===*/
2480 LLVMPassManagerRef LLVMCreatePassManager() {
2481 return wrap(new PassManager());
2484 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2485 return wrap(new FunctionPassManager(unwrap(M)));
2488 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2489 return LLVMCreateFunctionPassManagerForModule(
2490 reinterpret_cast<LLVMModuleRef>(P));
2493 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2494 return unwrap<PassManager>(PM)->run(*unwrap(M));
2497 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2498 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2501 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2502 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2505 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2506 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2509 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2513 /*===-- Threading ------------------------------------------------------===*/
2515 LLVMBool LLVMStartMultithreaded() {
2516 return llvm_start_multithreaded();
2519 void LLVMStopMultithreaded() {
2520 llvm_stop_multithreaded();
2523 LLVMBool LLVMIsMultithreaded() {
2524 return llvm_is_multithreaded();