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 initializePrintBasicBlockPassPass(Registry);
43 initializeVerifierPass(Registry);
44 initializePreVerifierPass(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.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1388 AttributeSet::get(Func->getContext(),
1389 AttributeSet::FunctionIndex, B));
1390 Func->setAttributes(PALnew);
1393 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1394 Function *Func = unwrap<Function>(Fn);
1395 const AttributeSet PAL = Func->getAttributes();
1397 const AttributeSet PALnew =
1398 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1399 AttributeSet::get(Func->getContext(),
1400 AttributeSet::FunctionIndex, B));
1401 Func->setAttributes(PALnew);
1404 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1405 Function *Func = unwrap<Function>(Fn);
1406 const AttributeSet PAL = Func->getAttributes();
1407 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1410 /*--.. Operations on parameters ............................................--*/
1412 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1413 // This function is strictly redundant to
1414 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1415 return unwrap<Function>(FnRef)->arg_size();
1418 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1419 Function *Fn = unwrap<Function>(FnRef);
1420 for (Function::arg_iterator I = Fn->arg_begin(),
1421 E = Fn->arg_end(); I != E; I++)
1422 *ParamRefs++ = wrap(I);
1425 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1426 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1432 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1433 return wrap(unwrap<Argument>(V)->getParent());
1436 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1437 Function *Func = unwrap<Function>(Fn);
1438 Function::arg_iterator I = Func->arg_begin();
1439 if (I == Func->arg_end())
1444 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1445 Function *Func = unwrap<Function>(Fn);
1446 Function::arg_iterator I = Func->arg_end();
1447 if (I == Func->arg_begin())
1452 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1453 Argument *A = unwrap<Argument>(Arg);
1454 Function::arg_iterator I = A;
1455 if (++I == A->getParent()->arg_end())
1460 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1461 Argument *A = unwrap<Argument>(Arg);
1462 Function::arg_iterator I = A;
1463 if (I == A->getParent()->arg_begin())
1468 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1469 Argument *A = unwrap<Argument>(Arg);
1471 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1474 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1475 Argument *A = unwrap<Argument>(Arg);
1477 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1480 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1481 Argument *A = unwrap<Argument>(Arg);
1482 return (LLVMAttribute)A->getParent()->getAttributes().
1483 Raw(A->getArgNo()+1);
1487 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1488 Argument *A = unwrap<Argument>(Arg);
1490 B.addAlignmentAttr(align);
1491 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1494 /*--.. Operations on basic blocks ..........................................--*/
1496 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1497 return wrap(static_cast<Value*>(unwrap(BB)));
1500 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1501 return isa<BasicBlock>(unwrap(Val));
1504 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1505 return wrap(unwrap<BasicBlock>(Val));
1508 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1509 return wrap(unwrap(BB)->getParent());
1512 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1513 return wrap(unwrap(BB)->getTerminator());
1516 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1517 return unwrap<Function>(FnRef)->size();
1520 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1521 Function *Fn = unwrap<Function>(FnRef);
1522 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1523 *BasicBlocksRefs++ = wrap(I);
1526 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1527 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1530 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1531 Function *Func = unwrap<Function>(Fn);
1532 Function::iterator I = Func->begin();
1533 if (I == Func->end())
1538 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1539 Function *Func = unwrap<Function>(Fn);
1540 Function::iterator I = Func->end();
1541 if (I == Func->begin())
1546 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1547 BasicBlock *Block = unwrap(BB);
1548 Function::iterator I = Block;
1549 if (++I == Block->getParent()->end())
1554 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1555 BasicBlock *Block = unwrap(BB);
1556 Function::iterator I = Block;
1557 if (I == Block->getParent()->begin())
1562 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1565 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1568 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1569 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1572 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1573 LLVMBasicBlockRef BBRef,
1575 BasicBlock *BB = unwrap(BBRef);
1576 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1579 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1581 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1584 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1585 unwrap(BBRef)->eraseFromParent();
1588 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1589 unwrap(BBRef)->removeFromParent();
1592 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1593 unwrap(BB)->moveBefore(unwrap(MovePos));
1596 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1597 unwrap(BB)->moveAfter(unwrap(MovePos));
1600 /*--.. Operations on instructions ..........................................--*/
1602 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1603 return wrap(unwrap<Instruction>(Inst)->getParent());
1606 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1607 BasicBlock *Block = unwrap(BB);
1608 BasicBlock::iterator I = Block->begin();
1609 if (I == Block->end())
1614 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1615 BasicBlock *Block = unwrap(BB);
1616 BasicBlock::iterator I = Block->end();
1617 if (I == Block->begin())
1622 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1623 Instruction *Instr = unwrap<Instruction>(Inst);
1624 BasicBlock::iterator I = Instr;
1625 if (++I == Instr->getParent()->end())
1630 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1631 Instruction *Instr = unwrap<Instruction>(Inst);
1632 BasicBlock::iterator I = Instr;
1633 if (I == Instr->getParent()->begin())
1638 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1639 unwrap<Instruction>(Inst)->eraseFromParent();
1642 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1643 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1644 return (LLVMIntPredicate)I->getPredicate();
1645 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1646 if (CE->getOpcode() == Instruction::ICmp)
1647 return (LLVMIntPredicate)CE->getPredicate();
1648 return (LLVMIntPredicate)0;
1651 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1652 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1653 return map_to_llvmopcode(C->getOpcode());
1654 return (LLVMOpcode)0;
1657 /*--.. Call and invoke instructions ........................................--*/
1659 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1660 Value *V = unwrap(Instr);
1661 if (CallInst *CI = dyn_cast<CallInst>(V))
1662 return CI->getCallingConv();
1663 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1664 return II->getCallingConv();
1665 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1668 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1669 Value *V = unwrap(Instr);
1670 if (CallInst *CI = dyn_cast<CallInst>(V))
1671 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1672 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1673 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1674 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1677 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1679 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1682 Call.getAttributes().addAttributes(Call->getContext(), index,
1683 AttributeSet::get(Call->getContext(),
1687 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1689 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1691 Call.setAttributes(Call.getAttributes()
1692 .removeAttributes(Call->getContext(), index,
1693 AttributeSet::get(Call->getContext(),
1697 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1699 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1701 B.addAlignmentAttr(align);
1702 Call.setAttributes(Call.getAttributes()
1703 .addAttributes(Call->getContext(), index,
1704 AttributeSet::get(Call->getContext(),
1708 /*--.. Operations on call instructions (only) ..............................--*/
1710 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1711 return unwrap<CallInst>(Call)->isTailCall();
1714 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1715 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1718 /*--.. Operations on switch instructions (only) ............................--*/
1720 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1721 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1724 /*--.. Operations on phi nodes .............................................--*/
1726 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1727 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1728 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1729 for (unsigned I = 0; I != Count; ++I)
1730 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1733 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1734 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1737 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1738 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1741 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1742 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1746 /*===-- Instruction builders ----------------------------------------------===*/
1748 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1749 return wrap(new IRBuilder<>(*unwrap(C)));
1752 LLVMBuilderRef LLVMCreateBuilder(void) {
1753 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1756 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1757 LLVMValueRef Instr) {
1758 BasicBlock *BB = unwrap(Block);
1759 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1760 unwrap(Builder)->SetInsertPoint(BB, I);
1763 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1764 Instruction *I = unwrap<Instruction>(Instr);
1765 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1768 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1769 BasicBlock *BB = unwrap(Block);
1770 unwrap(Builder)->SetInsertPoint(BB);
1773 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1774 return wrap(unwrap(Builder)->GetInsertBlock());
1777 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1778 unwrap(Builder)->ClearInsertionPoint();
1781 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1782 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1785 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1787 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1790 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1791 delete unwrap(Builder);
1794 /*--.. Metadata builders ...................................................--*/
1796 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1797 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1798 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1801 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1802 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1803 .getAsMDNode(unwrap(Builder)->getContext()));
1806 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1807 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1811 /*--.. Instruction builders ................................................--*/
1813 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1814 return wrap(unwrap(B)->CreateRetVoid());
1817 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1818 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1821 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1823 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1826 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1827 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1830 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1831 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1832 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1835 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1836 LLVMBasicBlockRef Else, unsigned NumCases) {
1837 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1840 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1841 unsigned NumDests) {
1842 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1845 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1846 LLVMValueRef *Args, unsigned NumArgs,
1847 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1849 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1850 makeArrayRef(unwrap(Args), NumArgs),
1854 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1855 LLVMValueRef PersFn, unsigned NumClauses,
1857 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1858 cast<Function>(unwrap(PersFn)),
1862 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1863 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1866 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1867 return wrap(unwrap(B)->CreateUnreachable());
1870 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1871 LLVMBasicBlockRef Dest) {
1872 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1875 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1876 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1879 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1880 unwrap<LandingPadInst>(LandingPad)->
1881 addClause(cast<Constant>(unwrap(ClauseVal)));
1884 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1885 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1888 /*--.. Arithmetic ..........................................................--*/
1890 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1892 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1895 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1897 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1900 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1902 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1905 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1907 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1910 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1912 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1915 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1917 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1920 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1922 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1925 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1927 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1930 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1932 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1935 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1937 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1940 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1942 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1945 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1947 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1950 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1952 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1955 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1957 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1960 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1961 LLVMValueRef RHS, const char *Name) {
1962 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1965 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1967 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1970 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1972 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1975 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1977 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1980 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1982 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1985 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1987 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1990 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1992 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1995 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1997 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2000 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2002 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2005 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2007 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2010 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2012 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2015 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2016 LLVMValueRef LHS, LLVMValueRef RHS,
2018 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2019 unwrap(RHS), Name));
2022 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2023 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2026 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2028 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2031 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2033 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2036 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2037 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2040 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2041 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2044 /*--.. Memory ..............................................................--*/
2046 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2048 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2049 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2050 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2051 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2052 ITy, unwrap(Ty), AllocSize,
2054 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2057 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2058 LLVMValueRef Val, const char *Name) {
2059 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2060 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2061 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2062 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2063 ITy, unwrap(Ty), AllocSize,
2064 unwrap(Val), 0, "");
2065 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2068 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2070 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2073 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2074 LLVMValueRef Val, const char *Name) {
2075 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2078 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2079 return wrap(unwrap(B)->Insert(
2080 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2084 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2086 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2089 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2090 LLVMValueRef PointerVal) {
2091 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2094 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2095 LLVMValueRef *Indices, unsigned NumIndices,
2097 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2098 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2101 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2102 LLVMValueRef *Indices, unsigned NumIndices,
2104 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2105 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2108 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2109 unsigned Idx, const char *Name) {
2110 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2113 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2115 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2118 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2120 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2123 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2124 Value *P = unwrap<Value>(MemAccessInst);
2125 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2126 return LI->isVolatile();
2127 return cast<StoreInst>(P)->isVolatile();
2130 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2131 Value *P = unwrap<Value>(MemAccessInst);
2132 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2133 return LI->setVolatile(isVolatile);
2134 return cast<StoreInst>(P)->setVolatile(isVolatile);
2137 /*--.. Casts ...............................................................--*/
2139 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2140 LLVMTypeRef DestTy, const char *Name) {
2141 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2144 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2145 LLVMTypeRef DestTy, const char *Name) {
2146 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2149 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2150 LLVMTypeRef DestTy, const char *Name) {
2151 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2154 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2155 LLVMTypeRef DestTy, const char *Name) {
2156 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2159 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2160 LLVMTypeRef DestTy, const char *Name) {
2161 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2164 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2165 LLVMTypeRef DestTy, const char *Name) {
2166 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2169 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2170 LLVMTypeRef DestTy, const char *Name) {
2171 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2174 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2175 LLVMTypeRef DestTy, const char *Name) {
2176 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2179 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2180 LLVMTypeRef DestTy, const char *Name) {
2181 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2184 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2185 LLVMTypeRef DestTy, const char *Name) {
2186 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2189 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2190 LLVMTypeRef DestTy, const char *Name) {
2191 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2194 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2195 LLVMTypeRef DestTy, const char *Name) {
2196 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2199 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2200 LLVMTypeRef DestTy, const char *Name) {
2201 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2205 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2206 LLVMTypeRef DestTy, const char *Name) {
2207 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2211 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2212 LLVMTypeRef DestTy, const char *Name) {
2213 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2217 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2218 LLVMTypeRef DestTy, const char *Name) {
2219 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2220 unwrap(DestTy), Name));
2223 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2224 LLVMTypeRef DestTy, const char *Name) {
2225 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2228 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2229 LLVMTypeRef DestTy, const char *Name) {
2230 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2231 /*isSigned*/true, Name));
2234 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2235 LLVMTypeRef DestTy, const char *Name) {
2236 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2239 /*--.. Comparisons .........................................................--*/
2241 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2242 LLVMValueRef LHS, LLVMValueRef RHS,
2244 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2245 unwrap(LHS), unwrap(RHS), Name));
2248 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2249 LLVMValueRef LHS, LLVMValueRef RHS,
2251 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2252 unwrap(LHS), unwrap(RHS), Name));
2255 /*--.. Miscellaneous instructions ..........................................--*/
2257 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2258 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2261 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2262 LLVMValueRef *Args, unsigned NumArgs,
2264 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2265 makeArrayRef(unwrap(Args), NumArgs),
2269 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2270 LLVMValueRef Then, LLVMValueRef Else,
2272 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2276 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2277 LLVMTypeRef Ty, const char *Name) {
2278 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2281 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2282 LLVMValueRef Index, const char *Name) {
2283 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2287 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2288 LLVMValueRef EltVal, LLVMValueRef Index,
2290 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2291 unwrap(Index), Name));
2294 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2295 LLVMValueRef V2, LLVMValueRef Mask,
2297 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2298 unwrap(Mask), Name));
2301 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2302 unsigned Index, const char *Name) {
2303 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2306 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2307 LLVMValueRef EltVal, unsigned Index,
2309 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2313 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2315 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2318 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2320 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2323 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2324 LLVMValueRef RHS, const char *Name) {
2325 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2329 /*===-- Module providers --------------------------------------------------===*/
2331 LLVMModuleProviderRef
2332 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2333 return reinterpret_cast<LLVMModuleProviderRef>(M);
2336 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2341 /*===-- Memory buffers ----------------------------------------------------===*/
2343 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2345 LLVMMemoryBufferRef *OutMemBuf,
2346 char **OutMessage) {
2348 OwningPtr<MemoryBuffer> MB;
2350 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2351 *OutMemBuf = wrap(MB.take());
2355 *OutMessage = strdup(ec.message().c_str());
2359 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2360 char **OutMessage) {
2361 OwningPtr<MemoryBuffer> MB;
2363 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2364 *OutMemBuf = wrap(MB.take());
2368 *OutMessage = strdup(ec.message().c_str());
2372 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2373 delete unwrap(MemBuf);
2376 /*===-- Pass Registry -----------------------------------------------------===*/
2378 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2379 return wrap(PassRegistry::getPassRegistry());
2382 /*===-- Pass Manager ------------------------------------------------------===*/
2384 LLVMPassManagerRef LLVMCreatePassManager() {
2385 return wrap(new PassManager());
2388 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2389 return wrap(new FunctionPassManager(unwrap(M)));
2392 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2393 return LLVMCreateFunctionPassManagerForModule(
2394 reinterpret_cast<LLVMModuleRef>(P));
2397 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2398 return unwrap<PassManager>(PM)->run(*unwrap(M));
2401 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2402 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2405 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2406 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2409 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2410 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2413 void LLVMDisposePassManager(LLVMPassManagerRef PM) {