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/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/GlobalAlias.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/InlineAsm.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CallSite.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/system_error.h"
38 void llvm::initializeCore(PassRegistry &Registry) {
39 initializeDominatorTreePass(Registry);
40 initializePrintModulePassPass(Registry);
41 initializePrintFunctionPassPass(Registry);
42 initializeVerifierPass(Registry);
43 initializePreVerifierPass(Registry);
44 initializeTargetTransformInfoAnalysisGroup(Registry);
47 void LLVMInitializeCore(LLVMPassRegistryRef R) {
48 initializeCore(*unwrap(R));
51 /*===-- Error handling ----------------------------------------------------===*/
53 void LLVMDisposeMessage(char *Message) {
58 /*===-- Operations on contexts --------------------------------------------===*/
60 LLVMContextRef LLVMContextCreate() {
61 return wrap(new LLVMContext());
64 LLVMContextRef LLVMGetGlobalContext() {
65 return wrap(&getGlobalContext());
68 void LLVMContextDispose(LLVMContextRef C) {
72 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
74 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
77 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
78 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
82 /*===-- Operations on modules ---------------------------------------------===*/
84 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
85 return wrap(new Module(ModuleID, getGlobalContext()));
88 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
90 return wrap(new Module(ModuleID, *unwrap(C)));
93 void LLVMDisposeModule(LLVMModuleRef M) {
97 /*--.. Data layout .........................................................--*/
98 const char * LLVMGetDataLayout(LLVMModuleRef M) {
99 return unwrap(M)->getDataLayout().c_str();
102 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
103 unwrap(M)->setDataLayout(Triple);
106 /*--.. Target triple .......................................................--*/
107 const char * LLVMGetTarget(LLVMModuleRef M) {
108 return unwrap(M)->getTargetTriple().c_str();
111 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
112 unwrap(M)->setTargetTriple(Triple);
115 void LLVMDumpModule(LLVMModuleRef M) {
119 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
120 char **ErrorMessage) {
122 raw_fd_ostream dest(Filename, error);
123 if (!error.empty()) {
124 *ErrorMessage = strdup(error.c_str());
128 unwrap(M)->print(dest, NULL);
130 if (!error.empty()) {
131 *ErrorMessage = strdup(error.c_str());
138 /*--.. Operations on inline assembler ......................................--*/
139 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
140 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
144 /*--.. Operations on module contexts ......................................--*/
145 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
146 return wrap(&unwrap(M)->getContext());
150 /*===-- Operations on types -----------------------------------------------===*/
152 /*--.. Operations on all types (mostly) ....................................--*/
154 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
155 switch (unwrap(Ty)->getTypeID()) {
156 default: llvm_unreachable("Unhandled TypeID.");
158 return LLVMVoidTypeKind;
160 return LLVMHalfTypeKind;
161 case Type::FloatTyID:
162 return LLVMFloatTypeKind;
163 case Type::DoubleTyID:
164 return LLVMDoubleTypeKind;
165 case Type::X86_FP80TyID:
166 return LLVMX86_FP80TypeKind;
167 case Type::FP128TyID:
168 return LLVMFP128TypeKind;
169 case Type::PPC_FP128TyID:
170 return LLVMPPC_FP128TypeKind;
171 case Type::LabelTyID:
172 return LLVMLabelTypeKind;
173 case Type::MetadataTyID:
174 return LLVMMetadataTypeKind;
175 case Type::IntegerTyID:
176 return LLVMIntegerTypeKind;
177 case Type::FunctionTyID:
178 return LLVMFunctionTypeKind;
179 case Type::StructTyID:
180 return LLVMStructTypeKind;
181 case Type::ArrayTyID:
182 return LLVMArrayTypeKind;
183 case Type::PointerTyID:
184 return LLVMPointerTypeKind;
185 case Type::VectorTyID:
186 return LLVMVectorTypeKind;
187 case Type::X86_MMXTyID:
188 return LLVMX86_MMXTypeKind;
192 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
194 return unwrap(Ty)->isSized();
197 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
198 return wrap(&unwrap(Ty)->getContext());
201 /*--.. Operations on integer types .........................................--*/
203 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
204 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
206 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
207 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
209 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
210 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
212 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
213 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
215 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
216 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
218 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
219 return wrap(IntegerType::get(*unwrap(C), NumBits));
222 LLVMTypeRef LLVMInt1Type(void) {
223 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
225 LLVMTypeRef LLVMInt8Type(void) {
226 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
228 LLVMTypeRef LLVMInt16Type(void) {
229 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
231 LLVMTypeRef LLVMInt32Type(void) {
232 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
234 LLVMTypeRef LLVMInt64Type(void) {
235 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
237 LLVMTypeRef LLVMIntType(unsigned NumBits) {
238 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
241 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
242 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
245 /*--.. Operations on real types ............................................--*/
247 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
248 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
250 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
251 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
253 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
254 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
256 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
257 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
259 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
260 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
262 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
263 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
265 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
266 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
269 LLVMTypeRef LLVMHalfType(void) {
270 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
272 LLVMTypeRef LLVMFloatType(void) {
273 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
275 LLVMTypeRef LLVMDoubleType(void) {
276 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
278 LLVMTypeRef LLVMX86FP80Type(void) {
279 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
281 LLVMTypeRef LLVMFP128Type(void) {
282 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
284 LLVMTypeRef LLVMPPCFP128Type(void) {
285 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
287 LLVMTypeRef LLVMX86MMXType(void) {
288 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
291 /*--.. Operations on function types ........................................--*/
293 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
294 LLVMTypeRef *ParamTypes, unsigned ParamCount,
296 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
297 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
300 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
301 return unwrap<FunctionType>(FunctionTy)->isVarArg();
304 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
305 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
308 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
309 return unwrap<FunctionType>(FunctionTy)->getNumParams();
312 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
313 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
314 for (FunctionType::param_iterator I = Ty->param_begin(),
315 E = Ty->param_end(); I != E; ++I)
319 /*--.. Operations on struct types ..........................................--*/
321 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
322 unsigned ElementCount, LLVMBool Packed) {
323 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
324 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
327 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
328 unsigned ElementCount, LLVMBool Packed) {
329 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
330 ElementCount, Packed);
333 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
335 return wrap(StructType::create(*unwrap(C), Name));
338 const char *LLVMGetStructName(LLVMTypeRef Ty)
340 StructType *Type = unwrap<StructType>(Ty);
341 if (!Type->hasName())
343 return Type->getName().data();
346 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
347 unsigned ElementCount, LLVMBool Packed) {
348 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
349 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
352 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
353 return unwrap<StructType>(StructTy)->getNumElements();
356 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
357 StructType *Ty = unwrap<StructType>(StructTy);
358 for (StructType::element_iterator I = Ty->element_begin(),
359 E = Ty->element_end(); I != E; ++I)
363 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
364 return unwrap<StructType>(StructTy)->isPacked();
367 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
368 return unwrap<StructType>(StructTy)->isOpaque();
371 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
372 return wrap(unwrap(M)->getTypeByName(Name));
375 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
377 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
378 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
381 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
382 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
385 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
386 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
389 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
390 return wrap(unwrap<SequentialType>(Ty)->getElementType());
393 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
394 return unwrap<ArrayType>(ArrayTy)->getNumElements();
397 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
398 return unwrap<PointerType>(PointerTy)->getAddressSpace();
401 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
402 return unwrap<VectorType>(VectorTy)->getNumElements();
405 /*--.. Operations on other types ...........................................--*/
407 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
408 return wrap(Type::getVoidTy(*unwrap(C)));
410 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
411 return wrap(Type::getLabelTy(*unwrap(C)));
414 LLVMTypeRef LLVMVoidType(void) {
415 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
417 LLVMTypeRef LLVMLabelType(void) {
418 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
421 /*===-- Operations on values ----------------------------------------------===*/
423 /*--.. Operations on all values ............................................--*/
425 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
426 return wrap(unwrap(Val)->getType());
429 const char *LLVMGetValueName(LLVMValueRef Val) {
430 return unwrap(Val)->getName().data();
433 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
434 unwrap(Val)->setName(Name);
437 void LLVMDumpValue(LLVMValueRef Val) {
441 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
442 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
445 int LLVMHasMetadata(LLVMValueRef Inst) {
446 return unwrap<Instruction>(Inst)->hasMetadata();
449 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
450 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
453 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
454 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
457 /*--.. Conversion functions ................................................--*/
459 #define LLVM_DEFINE_VALUE_CAST(name) \
460 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
461 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
464 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
466 /*--.. Operations on Uses ..................................................--*/
467 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
468 Value *V = unwrap(Val);
469 Value::use_iterator I = V->use_begin();
470 if (I == V->use_end())
472 return wrap(&(I.getUse()));
475 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
476 Use *Next = unwrap(U)->getNext();
482 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
483 return wrap(unwrap(U)->getUser());
486 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
487 return wrap(unwrap(U)->get());
490 /*--.. Operations on Users .................................................--*/
491 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
492 Value *V = unwrap(Val);
493 if (MDNode *MD = dyn_cast<MDNode>(V))
494 return wrap(MD->getOperand(Index));
495 return wrap(cast<User>(V)->getOperand(Index));
498 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
499 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
502 int LLVMGetNumOperands(LLVMValueRef Val) {
503 Value *V = unwrap(Val);
504 if (MDNode *MD = dyn_cast<MDNode>(V))
505 return MD->getNumOperands();
506 return cast<User>(V)->getNumOperands();
509 /*--.. Operations on constants of any type .................................--*/
511 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
512 return wrap(Constant::getNullValue(unwrap(Ty)));
515 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
516 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
519 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
520 return wrap(UndefValue::get(unwrap(Ty)));
523 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
524 return isa<Constant>(unwrap(Ty));
527 LLVMBool LLVMIsNull(LLVMValueRef Val) {
528 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
529 return C->isNullValue();
533 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
534 return isa<UndefValue>(unwrap(Val));
537 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
539 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
542 /*--.. Operations on metadata nodes ........................................--*/
544 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
546 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
549 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
550 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
553 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
555 return wrap(MDNode::get(*unwrap(C),
556 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
559 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
560 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
563 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
564 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
565 *Len = S->getString().size();
566 return S->getString().data();
572 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
574 return cast<MDNode>(unwrap(V))->getNumOperands();
577 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
579 const MDNode *N = cast<MDNode>(unwrap(V));
580 const unsigned numOperands = N->getNumOperands();
581 for (unsigned i = 0; i < numOperands; i++)
582 Dest[i] = wrap(N->getOperand(i));
585 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
587 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
588 return N->getNumOperands();
593 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
595 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
598 for (unsigned i=0;i<N->getNumOperands();i++)
599 Dest[i] = wrap(N->getOperand(i));
602 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
605 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
608 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
613 /*--.. Operations on scalar constants ......................................--*/
615 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
616 LLVMBool SignExtend) {
617 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
620 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
622 const uint64_t Words[]) {
623 IntegerType *Ty = unwrap<IntegerType>(IntTy);
624 return wrap(ConstantInt::get(Ty->getContext(),
625 APInt(Ty->getBitWidth(),
626 makeArrayRef(Words, NumWords))));
629 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
631 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
635 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
636 unsigned SLen, uint8_t Radix) {
637 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
641 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
642 return wrap(ConstantFP::get(unwrap(RealTy), N));
645 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
646 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
649 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
651 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
654 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
655 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
658 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
659 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
662 /*--.. Operations on composite constants ...................................--*/
664 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
666 LLVMBool DontNullTerminate) {
667 /* Inverted the sense of AddNull because ', 0)' is a
668 better mnemonic for null termination than ', 1)'. */
669 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
670 DontNullTerminate == 0));
672 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
673 LLVMValueRef *ConstantVals,
674 unsigned Count, LLVMBool Packed) {
675 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
676 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
680 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
681 LLVMBool DontNullTerminate) {
682 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
685 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
686 LLVMValueRef *ConstantVals, unsigned Length) {
687 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
688 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
690 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
692 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
696 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
697 LLVMValueRef *ConstantVals,
699 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
700 StructType *Ty = cast<StructType>(unwrap(StructTy));
702 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
705 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
706 return wrap(ConstantVector::get(makeArrayRef(
707 unwrap<Constant>(ScalarConstantVals, Size), Size)));
710 /*-- Opcode mapping */
712 static LLVMOpcode map_to_llvmopcode(int opcode)
715 default: llvm_unreachable("Unhandled Opcode.");
716 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
717 #include "llvm/IR/Instruction.def"
722 static int map_from_llvmopcode(LLVMOpcode code)
725 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
726 #include "llvm/IR/Instruction.def"
729 llvm_unreachable("Unhandled Opcode.");
732 /*--.. Constant expressions ................................................--*/
734 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
735 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
738 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
739 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
742 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
743 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
746 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
747 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
750 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
751 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
754 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
755 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
759 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
760 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
763 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
764 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
767 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
768 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
769 unwrap<Constant>(RHSConstant)));
772 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
773 LLVMValueRef RHSConstant) {
774 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
775 unwrap<Constant>(RHSConstant)));
778 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
779 LLVMValueRef RHSConstant) {
780 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
781 unwrap<Constant>(RHSConstant)));
784 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
785 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
786 unwrap<Constant>(RHSConstant)));
789 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
790 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
791 unwrap<Constant>(RHSConstant)));
794 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
795 LLVMValueRef RHSConstant) {
796 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
797 unwrap<Constant>(RHSConstant)));
800 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
801 LLVMValueRef RHSConstant) {
802 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
803 unwrap<Constant>(RHSConstant)));
806 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
807 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
808 unwrap<Constant>(RHSConstant)));
811 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
812 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
813 unwrap<Constant>(RHSConstant)));
816 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
817 LLVMValueRef RHSConstant) {
818 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
819 unwrap<Constant>(RHSConstant)));
822 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
823 LLVMValueRef RHSConstant) {
824 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
825 unwrap<Constant>(RHSConstant)));
828 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
829 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
830 unwrap<Constant>(RHSConstant)));
833 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
834 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
835 unwrap<Constant>(RHSConstant)));
838 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
839 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
840 unwrap<Constant>(RHSConstant)));
843 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
844 LLVMValueRef RHSConstant) {
845 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
846 unwrap<Constant>(RHSConstant)));
849 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
850 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
851 unwrap<Constant>(RHSConstant)));
854 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
855 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
856 unwrap<Constant>(RHSConstant)));
859 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
860 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
861 unwrap<Constant>(RHSConstant)));
864 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
865 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
866 unwrap<Constant>(RHSConstant)));
869 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
870 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
871 unwrap<Constant>(RHSConstant)));
874 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
875 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
876 unwrap<Constant>(RHSConstant)));
879 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
880 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
881 unwrap<Constant>(RHSConstant)));
884 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
885 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
886 return wrap(ConstantExpr::getICmp(Predicate,
887 unwrap<Constant>(LHSConstant),
888 unwrap<Constant>(RHSConstant)));
891 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
892 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
893 return wrap(ConstantExpr::getFCmp(Predicate,
894 unwrap<Constant>(LHSConstant),
895 unwrap<Constant>(RHSConstant)));
898 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
899 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
900 unwrap<Constant>(RHSConstant)));
903 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
904 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
905 unwrap<Constant>(RHSConstant)));
908 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
909 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
910 unwrap<Constant>(RHSConstant)));
913 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
914 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
915 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
917 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
921 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
922 LLVMValueRef *ConstantIndices,
923 unsigned NumIndices) {
924 Constant* Val = unwrap<Constant>(ConstantVal);
925 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
927 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
930 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
931 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
935 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
936 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
940 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
941 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
945 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
946 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
950 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
951 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
955 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
956 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
960 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
961 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
965 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
966 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
970 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
971 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
975 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
976 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
980 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
981 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
985 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
986 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
990 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
991 LLVMTypeRef ToType) {
992 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
996 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
997 LLVMTypeRef ToType) {
998 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1002 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1003 LLVMTypeRef ToType) {
1004 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1008 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1009 LLVMTypeRef ToType) {
1010 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1014 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1015 LLVMBool isSigned) {
1016 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1017 unwrap(ToType), isSigned));
1020 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1021 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1025 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1026 LLVMValueRef ConstantIfTrue,
1027 LLVMValueRef ConstantIfFalse) {
1028 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1029 unwrap<Constant>(ConstantIfTrue),
1030 unwrap<Constant>(ConstantIfFalse)));
1033 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1034 LLVMValueRef IndexConstant) {
1035 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1036 unwrap<Constant>(IndexConstant)));
1039 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1040 LLVMValueRef ElementValueConstant,
1041 LLVMValueRef IndexConstant) {
1042 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1043 unwrap<Constant>(ElementValueConstant),
1044 unwrap<Constant>(IndexConstant)));
1047 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1048 LLVMValueRef VectorBConstant,
1049 LLVMValueRef MaskConstant) {
1050 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1051 unwrap<Constant>(VectorBConstant),
1052 unwrap<Constant>(MaskConstant)));
1055 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1057 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1058 makeArrayRef(IdxList, NumIdx)));
1061 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1062 LLVMValueRef ElementValueConstant,
1063 unsigned *IdxList, unsigned NumIdx) {
1064 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1065 unwrap<Constant>(ElementValueConstant),
1066 makeArrayRef(IdxList, NumIdx)));
1069 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1070 const char *Constraints,
1071 LLVMBool HasSideEffects,
1072 LLVMBool IsAlignStack) {
1073 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1074 Constraints, HasSideEffects, IsAlignStack));
1077 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1078 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1081 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1083 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1084 return wrap(unwrap<GlobalValue>(Global)->getParent());
1087 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1088 return unwrap<GlobalValue>(Global)->isDeclaration();
1091 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1092 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1093 case GlobalValue::ExternalLinkage:
1094 return LLVMExternalLinkage;
1095 case GlobalValue::AvailableExternallyLinkage:
1096 return LLVMAvailableExternallyLinkage;
1097 case GlobalValue::LinkOnceAnyLinkage:
1098 return LLVMLinkOnceAnyLinkage;
1099 case GlobalValue::LinkOnceODRLinkage:
1100 return LLVMLinkOnceODRLinkage;
1101 case GlobalValue::LinkOnceODRAutoHideLinkage:
1102 return LLVMLinkOnceODRAutoHideLinkage;
1103 case GlobalValue::WeakAnyLinkage:
1104 return LLVMWeakAnyLinkage;
1105 case GlobalValue::WeakODRLinkage:
1106 return LLVMWeakODRLinkage;
1107 case GlobalValue::AppendingLinkage:
1108 return LLVMAppendingLinkage;
1109 case GlobalValue::InternalLinkage:
1110 return LLVMInternalLinkage;
1111 case GlobalValue::PrivateLinkage:
1112 return LLVMPrivateLinkage;
1113 case GlobalValue::LinkerPrivateLinkage:
1114 return LLVMLinkerPrivateLinkage;
1115 case GlobalValue::LinkerPrivateWeakLinkage:
1116 return LLVMLinkerPrivateWeakLinkage;
1117 case GlobalValue::DLLImportLinkage:
1118 return LLVMDLLImportLinkage;
1119 case GlobalValue::DLLExportLinkage:
1120 return LLVMDLLExportLinkage;
1121 case GlobalValue::ExternalWeakLinkage:
1122 return LLVMExternalWeakLinkage;
1123 case GlobalValue::CommonLinkage:
1124 return LLVMCommonLinkage;
1127 llvm_unreachable("Invalid GlobalValue linkage!");
1130 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1131 GlobalValue *GV = unwrap<GlobalValue>(Global);
1134 case LLVMExternalLinkage:
1135 GV->setLinkage(GlobalValue::ExternalLinkage);
1137 case LLVMAvailableExternallyLinkage:
1138 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1140 case LLVMLinkOnceAnyLinkage:
1141 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1143 case LLVMLinkOnceODRLinkage:
1144 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1146 case LLVMLinkOnceODRAutoHideLinkage:
1147 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1149 case LLVMWeakAnyLinkage:
1150 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1152 case LLVMWeakODRLinkage:
1153 GV->setLinkage(GlobalValue::WeakODRLinkage);
1155 case LLVMAppendingLinkage:
1156 GV->setLinkage(GlobalValue::AppendingLinkage);
1158 case LLVMInternalLinkage:
1159 GV->setLinkage(GlobalValue::InternalLinkage);
1161 case LLVMPrivateLinkage:
1162 GV->setLinkage(GlobalValue::PrivateLinkage);
1164 case LLVMLinkerPrivateLinkage:
1165 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1167 case LLVMLinkerPrivateWeakLinkage:
1168 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1170 case LLVMDLLImportLinkage:
1171 GV->setLinkage(GlobalValue::DLLImportLinkage);
1173 case LLVMDLLExportLinkage:
1174 GV->setLinkage(GlobalValue::DLLExportLinkage);
1176 case LLVMExternalWeakLinkage:
1177 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1179 case LLVMGhostLinkage:
1181 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1183 case LLVMCommonLinkage:
1184 GV->setLinkage(GlobalValue::CommonLinkage);
1189 const char *LLVMGetSection(LLVMValueRef Global) {
1190 return unwrap<GlobalValue>(Global)->getSection().c_str();
1193 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1194 unwrap<GlobalValue>(Global)->setSection(Section);
1197 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1198 return static_cast<LLVMVisibility>(
1199 unwrap<GlobalValue>(Global)->getVisibility());
1202 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1203 unwrap<GlobalValue>(Global)
1204 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1207 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1208 return unwrap<GlobalValue>(Global)->getAlignment();
1211 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1212 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1215 /*--.. Operations on global variables ......................................--*/
1217 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1218 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1219 GlobalValue::ExternalLinkage, 0, Name));
1222 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1224 unsigned AddressSpace) {
1225 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1226 GlobalValue::ExternalLinkage, 0, Name, 0,
1227 GlobalVariable::NotThreadLocal, AddressSpace));
1230 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1231 return wrap(unwrap(M)->getNamedGlobal(Name));
1234 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1235 Module *Mod = unwrap(M);
1236 Module::global_iterator I = Mod->global_begin();
1237 if (I == Mod->global_end())
1242 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1243 Module *Mod = unwrap(M);
1244 Module::global_iterator I = Mod->global_end();
1245 if (I == Mod->global_begin())
1250 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1251 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1252 Module::global_iterator I = GV;
1253 if (++I == GV->getParent()->global_end())
1258 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1259 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1260 Module::global_iterator I = GV;
1261 if (I == GV->getParent()->global_begin())
1266 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1267 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1270 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1271 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1272 if ( !GV->hasInitializer() )
1274 return wrap(GV->getInitializer());
1277 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1278 unwrap<GlobalVariable>(GlobalVar)
1279 ->setInitializer(unwrap<Constant>(ConstantVal));
1282 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1283 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1286 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1287 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1290 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1291 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1294 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1295 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1298 /*--.. Operations on aliases ......................................--*/
1300 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1302 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1303 unwrap<Constant>(Aliasee), unwrap (M)));
1306 /*--.. Operations on functions .............................................--*/
1308 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1309 LLVMTypeRef FunctionTy) {
1310 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1311 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1314 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1315 return wrap(unwrap(M)->getFunction(Name));
1318 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1319 Module *Mod = unwrap(M);
1320 Module::iterator I = Mod->begin();
1321 if (I == Mod->end())
1326 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1327 Module *Mod = unwrap(M);
1328 Module::iterator I = Mod->end();
1329 if (I == Mod->begin())
1334 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1335 Function *Func = unwrap<Function>(Fn);
1336 Module::iterator I = Func;
1337 if (++I == Func->getParent()->end())
1342 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1343 Function *Func = unwrap<Function>(Fn);
1344 Module::iterator I = Func;
1345 if (I == Func->getParent()->begin())
1350 void LLVMDeleteFunction(LLVMValueRef Fn) {
1351 unwrap<Function>(Fn)->eraseFromParent();
1354 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1355 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1356 return F->getIntrinsicID();
1360 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1361 return unwrap<Function>(Fn)->getCallingConv();
1364 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1365 return unwrap<Function>(Fn)->setCallingConv(
1366 static_cast<CallingConv::ID>(CC));
1369 const char *LLVMGetGC(LLVMValueRef Fn) {
1370 Function *F = unwrap<Function>(Fn);
1371 return F->hasGC()? F->getGC() : 0;
1374 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1375 Function *F = unwrap<Function>(Fn);
1382 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1383 Function *Func = unwrap<Function>(Fn);
1384 const AttributeSet PAL = Func->getAttributes();
1386 const AttributeSet PALnew =
1387 PAL.addAttr(Func->getContext(), AttributeSet::FunctionIndex,
1388 Attribute::get(Func->getContext(), B));
1389 Func->setAttributes(PALnew);
1392 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1393 Function *Func = unwrap<Function>(Fn);
1394 const AttributeSet PAL = Func->getAttributes();
1396 const AttributeSet PALnew =
1397 PAL.removeAttr(Func->getContext(), AttributeSet::FunctionIndex,
1398 Attribute::get(Func->getContext(), B));
1399 Func->setAttributes(PALnew);
1402 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1403 Function *Func = unwrap<Function>(Fn);
1404 const AttributeSet PAL = Func->getAttributes();
1405 return (LLVMAttribute)PAL.getBitMask(AttributeSet::FunctionIndex);
1408 /*--.. Operations on parameters ............................................--*/
1410 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1411 // This function is strictly redundant to
1412 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1413 return unwrap<Function>(FnRef)->arg_size();
1416 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1417 Function *Fn = unwrap<Function>(FnRef);
1418 for (Function::arg_iterator I = Fn->arg_begin(),
1419 E = Fn->arg_end(); I != E; I++)
1420 *ParamRefs++ = wrap(I);
1423 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1424 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1430 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1431 return wrap(unwrap<Argument>(V)->getParent());
1434 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1435 Function *Func = unwrap<Function>(Fn);
1436 Function::arg_iterator I = Func->arg_begin();
1437 if (I == Func->arg_end())
1442 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1443 Function *Func = unwrap<Function>(Fn);
1444 Function::arg_iterator I = Func->arg_end();
1445 if (I == Func->arg_begin())
1450 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1451 Argument *A = unwrap<Argument>(Arg);
1452 Function::arg_iterator I = A;
1453 if (++I == A->getParent()->arg_end())
1458 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1459 Argument *A = unwrap<Argument>(Arg);
1460 Function::arg_iterator I = A;
1461 if (I == A->getParent()->arg_begin())
1466 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1467 Argument *A = unwrap<Argument>(Arg);
1469 A->addAttr(Attribute::get(A->getContext(), B));
1472 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1473 Argument *A = unwrap<Argument>(Arg);
1475 A->removeAttr(Attribute::get(A->getContext(), B));
1478 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1479 Argument *A = unwrap<Argument>(Arg);
1480 return (LLVMAttribute)A->getParent()->getAttributes().
1481 getBitMask(A->getArgNo()+1);
1485 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1487 B.addAlignmentAttr(align);
1488 unwrap<Argument>(Arg)->addAttr(Attribute::
1489 get(unwrap<Argument>(Arg)->getContext(), B));
1492 /*--.. Operations on basic blocks ..........................................--*/
1494 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1495 return wrap(static_cast<Value*>(unwrap(BB)));
1498 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1499 return isa<BasicBlock>(unwrap(Val));
1502 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1503 return wrap(unwrap<BasicBlock>(Val));
1506 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1507 return wrap(unwrap(BB)->getParent());
1510 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1511 return wrap(unwrap(BB)->getTerminator());
1514 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1515 return unwrap<Function>(FnRef)->size();
1518 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1519 Function *Fn = unwrap<Function>(FnRef);
1520 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1521 *BasicBlocksRefs++ = wrap(I);
1524 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1525 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1528 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1529 Function *Func = unwrap<Function>(Fn);
1530 Function::iterator I = Func->begin();
1531 if (I == Func->end())
1536 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1537 Function *Func = unwrap<Function>(Fn);
1538 Function::iterator I = Func->end();
1539 if (I == Func->begin())
1544 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1545 BasicBlock *Block = unwrap(BB);
1546 Function::iterator I = Block;
1547 if (++I == Block->getParent()->end())
1552 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1553 BasicBlock *Block = unwrap(BB);
1554 Function::iterator I = Block;
1555 if (I == Block->getParent()->begin())
1560 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1563 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1566 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1567 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1570 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1571 LLVMBasicBlockRef BBRef,
1573 BasicBlock *BB = unwrap(BBRef);
1574 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1577 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1579 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1582 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1583 unwrap(BBRef)->eraseFromParent();
1586 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1587 unwrap(BBRef)->removeFromParent();
1590 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1591 unwrap(BB)->moveBefore(unwrap(MovePos));
1594 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1595 unwrap(BB)->moveAfter(unwrap(MovePos));
1598 /*--.. Operations on instructions ..........................................--*/
1600 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1601 return wrap(unwrap<Instruction>(Inst)->getParent());
1604 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1605 BasicBlock *Block = unwrap(BB);
1606 BasicBlock::iterator I = Block->begin();
1607 if (I == Block->end())
1612 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1613 BasicBlock *Block = unwrap(BB);
1614 BasicBlock::iterator I = Block->end();
1615 if (I == Block->begin())
1620 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1621 Instruction *Instr = unwrap<Instruction>(Inst);
1622 BasicBlock::iterator I = Instr;
1623 if (++I == Instr->getParent()->end())
1628 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1629 Instruction *Instr = unwrap<Instruction>(Inst);
1630 BasicBlock::iterator I = Instr;
1631 if (I == Instr->getParent()->begin())
1636 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1637 unwrap<Instruction>(Inst)->eraseFromParent();
1640 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1641 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1642 return (LLVMIntPredicate)I->getPredicate();
1643 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1644 if (CE->getOpcode() == Instruction::ICmp)
1645 return (LLVMIntPredicate)CE->getPredicate();
1646 return (LLVMIntPredicate)0;
1649 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1650 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1651 return map_to_llvmopcode(C->getOpcode());
1652 return (LLVMOpcode)0;
1655 /*--.. Call and invoke instructions ........................................--*/
1657 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1658 Value *V = unwrap(Instr);
1659 if (CallInst *CI = dyn_cast<CallInst>(V))
1660 return CI->getCallingConv();
1661 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1662 return II->getCallingConv();
1663 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1666 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1667 Value *V = unwrap(Instr);
1668 if (CallInst *CI = dyn_cast<CallInst>(V))
1669 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1670 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1671 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1672 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1675 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1677 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1680 Call.getAttributes().addAttr(Call->getContext(), index,
1681 Attribute::get(Call->getContext(), B)));
1684 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1686 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1689 Call.getAttributes().removeAttr(Call->getContext(), index,
1690 Attribute::get(Call->getContext(), B)));
1693 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1695 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1697 B.addAlignmentAttr(align);
1698 Call.setAttributes(Call.getAttributes().addAttr(Call->getContext(), index,
1699 Attribute::get(Call->getContext(), B)));
1702 /*--.. Operations on call instructions (only) ..............................--*/
1704 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1705 return unwrap<CallInst>(Call)->isTailCall();
1708 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1709 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1712 /*--.. Operations on switch instructions (only) ............................--*/
1714 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1715 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1718 /*--.. Operations on phi nodes .............................................--*/
1720 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1721 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1722 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1723 for (unsigned I = 0; I != Count; ++I)
1724 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1727 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1728 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1731 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1732 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1735 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1736 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1740 /*===-- Instruction builders ----------------------------------------------===*/
1742 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1743 return wrap(new IRBuilder<>(*unwrap(C)));
1746 LLVMBuilderRef LLVMCreateBuilder(void) {
1747 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1750 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1751 LLVMValueRef Instr) {
1752 BasicBlock *BB = unwrap(Block);
1753 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1754 unwrap(Builder)->SetInsertPoint(BB, I);
1757 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1758 Instruction *I = unwrap<Instruction>(Instr);
1759 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1762 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1763 BasicBlock *BB = unwrap(Block);
1764 unwrap(Builder)->SetInsertPoint(BB);
1767 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1768 return wrap(unwrap(Builder)->GetInsertBlock());
1771 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1772 unwrap(Builder)->ClearInsertionPoint();
1775 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1776 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1779 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1781 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1784 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1785 delete unwrap(Builder);
1788 /*--.. Metadata builders ...................................................--*/
1790 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1791 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1792 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1795 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1796 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1797 .getAsMDNode(unwrap(Builder)->getContext()));
1800 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1801 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1805 /*--.. Instruction builders ................................................--*/
1807 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1808 return wrap(unwrap(B)->CreateRetVoid());
1811 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1812 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1815 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1817 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1820 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1821 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1824 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1825 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1826 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1829 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1830 LLVMBasicBlockRef Else, unsigned NumCases) {
1831 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1834 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1835 unsigned NumDests) {
1836 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1839 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1840 LLVMValueRef *Args, unsigned NumArgs,
1841 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1843 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1844 makeArrayRef(unwrap(Args), NumArgs),
1848 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1849 LLVMValueRef PersFn, unsigned NumClauses,
1851 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1852 cast<Function>(unwrap(PersFn)),
1856 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1857 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1860 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1861 return wrap(unwrap(B)->CreateUnreachable());
1864 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1865 LLVMBasicBlockRef Dest) {
1866 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1869 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1870 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1873 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1874 unwrap<LandingPadInst>(LandingPad)->
1875 addClause(cast<Constant>(unwrap(ClauseVal)));
1878 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1879 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1882 /*--.. Arithmetic ..........................................................--*/
1884 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1886 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1889 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1891 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1894 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1896 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1899 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1901 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1904 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1906 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1909 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1911 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1914 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1916 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1919 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1921 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1924 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1926 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1929 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1931 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1934 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1936 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1939 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1941 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1944 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1946 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1949 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1951 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1954 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1955 LLVMValueRef RHS, const char *Name) {
1956 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1959 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1961 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1964 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1966 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1969 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1971 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1974 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1976 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1979 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1981 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1984 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1986 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1989 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1991 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1994 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1996 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1999 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2001 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2004 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2006 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2009 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2010 LLVMValueRef LHS, LLVMValueRef RHS,
2012 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2013 unwrap(RHS), Name));
2016 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2017 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2020 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2022 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2025 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2027 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2030 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2031 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2034 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2035 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2038 /*--.. Memory ..............................................................--*/
2040 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2042 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2043 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2044 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2045 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2046 ITy, unwrap(Ty), AllocSize,
2048 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2051 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2052 LLVMValueRef Val, const char *Name) {
2053 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2054 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2055 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2056 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2057 ITy, unwrap(Ty), AllocSize,
2058 unwrap(Val), 0, "");
2059 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2062 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2064 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2067 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2068 LLVMValueRef Val, const char *Name) {
2069 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2072 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2073 return wrap(unwrap(B)->Insert(
2074 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2078 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2080 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2083 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2084 LLVMValueRef PointerVal) {
2085 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2088 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2089 LLVMValueRef *Indices, unsigned NumIndices,
2091 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2092 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2095 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2096 LLVMValueRef *Indices, unsigned NumIndices,
2098 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2099 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2102 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2103 unsigned Idx, const char *Name) {
2104 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2107 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2109 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2112 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2114 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2117 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2118 Value *P = unwrap<Value>(MemAccessInst);
2119 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2120 return LI->isVolatile();
2121 return cast<StoreInst>(P)->isVolatile();
2124 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2125 Value *P = unwrap<Value>(MemAccessInst);
2126 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2127 return LI->setVolatile(isVolatile);
2128 return cast<StoreInst>(P)->setVolatile(isVolatile);
2131 /*--.. Casts ...............................................................--*/
2133 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2134 LLVMTypeRef DestTy, const char *Name) {
2135 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2138 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2139 LLVMTypeRef DestTy, const char *Name) {
2140 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2143 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2144 LLVMTypeRef DestTy, const char *Name) {
2145 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2148 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2149 LLVMTypeRef DestTy, const char *Name) {
2150 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2153 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2154 LLVMTypeRef DestTy, const char *Name) {
2155 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2158 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2159 LLVMTypeRef DestTy, const char *Name) {
2160 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2163 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2164 LLVMTypeRef DestTy, const char *Name) {
2165 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2168 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2169 LLVMTypeRef DestTy, const char *Name) {
2170 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2173 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2174 LLVMTypeRef DestTy, const char *Name) {
2175 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2178 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2179 LLVMTypeRef DestTy, const char *Name) {
2180 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2183 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2184 LLVMTypeRef DestTy, const char *Name) {
2185 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2188 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2189 LLVMTypeRef DestTy, const char *Name) {
2190 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2193 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2194 LLVMTypeRef DestTy, const char *Name) {
2195 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2199 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2200 LLVMTypeRef DestTy, const char *Name) {
2201 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2205 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2206 LLVMTypeRef DestTy, const char *Name) {
2207 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2211 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2212 LLVMTypeRef DestTy, const char *Name) {
2213 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2214 unwrap(DestTy), Name));
2217 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2218 LLVMTypeRef DestTy, const char *Name) {
2219 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2222 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2223 LLVMTypeRef DestTy, const char *Name) {
2224 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2225 /*isSigned*/true, Name));
2228 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2229 LLVMTypeRef DestTy, const char *Name) {
2230 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2233 /*--.. Comparisons .........................................................--*/
2235 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2236 LLVMValueRef LHS, LLVMValueRef RHS,
2238 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2239 unwrap(LHS), unwrap(RHS), Name));
2242 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2243 LLVMValueRef LHS, LLVMValueRef RHS,
2245 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2246 unwrap(LHS), unwrap(RHS), Name));
2249 /*--.. Miscellaneous instructions ..........................................--*/
2251 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2252 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2255 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2256 LLVMValueRef *Args, unsigned NumArgs,
2258 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2259 makeArrayRef(unwrap(Args), NumArgs),
2263 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2264 LLVMValueRef Then, LLVMValueRef Else,
2266 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2270 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2271 LLVMTypeRef Ty, const char *Name) {
2272 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2275 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2276 LLVMValueRef Index, const char *Name) {
2277 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2281 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2282 LLVMValueRef EltVal, LLVMValueRef Index,
2284 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2285 unwrap(Index), Name));
2288 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2289 LLVMValueRef V2, LLVMValueRef Mask,
2291 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2292 unwrap(Mask), Name));
2295 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2296 unsigned Index, const char *Name) {
2297 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2300 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2301 LLVMValueRef EltVal, unsigned Index,
2303 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2307 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2309 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2312 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2314 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2317 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2318 LLVMValueRef RHS, const char *Name) {
2319 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2323 /*===-- Module providers --------------------------------------------------===*/
2325 LLVMModuleProviderRef
2326 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2327 return reinterpret_cast<LLVMModuleProviderRef>(M);
2330 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2335 /*===-- Memory buffers ----------------------------------------------------===*/
2337 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2339 LLVMMemoryBufferRef *OutMemBuf,
2340 char **OutMessage) {
2342 OwningPtr<MemoryBuffer> MB;
2344 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2345 *OutMemBuf = wrap(MB.take());
2349 *OutMessage = strdup(ec.message().c_str());
2353 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2354 char **OutMessage) {
2355 OwningPtr<MemoryBuffer> MB;
2357 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2358 *OutMemBuf = wrap(MB.take());
2362 *OutMessage = strdup(ec.message().c_str());
2366 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2367 delete unwrap(MemBuf);
2370 /*===-- Pass Registry -----------------------------------------------------===*/
2372 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2373 return wrap(PassRegistry::getPassRegistry());
2376 /*===-- Pass Manager ------------------------------------------------------===*/
2378 LLVMPassManagerRef LLVMCreatePassManager() {
2379 return wrap(new PassManager());
2382 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2383 return wrap(new FunctionPassManager(unwrap(M)));
2386 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2387 return LLVMCreateFunctionPassManagerForModule(
2388 reinterpret_cast<LLVMModuleRef>(P));
2391 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2392 return unwrap<PassManager>(PM)->run(*unwrap(M));
2395 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2396 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2399 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2400 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2403 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2404 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2407 void LLVMDisposePassManager(LLVMPassManagerRef PM) {