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/ManagedStatic.h"
30 #include "llvm/Support/MemoryBuffer.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/system_error.h"
33 #include "llvm/Support/Threading.h"
40 void llvm::initializeCore(PassRegistry &Registry) {
41 initializeDominatorTreePass(Registry);
42 initializePrintModulePassPass(Registry);
43 initializePrintFunctionPassPass(Registry);
44 initializePrintBasicBlockPassPass(Registry);
45 initializeVerifierPass(Registry);
46 initializePreVerifierPass(Registry);
49 void LLVMInitializeCore(LLVMPassRegistryRef R) {
50 initializeCore(*unwrap(R));
57 /*===-- Error handling ----------------------------------------------------===*/
59 void LLVMDisposeMessage(char *Message) {
64 /*===-- Operations on contexts --------------------------------------------===*/
66 LLVMContextRef LLVMContextCreate() {
67 return wrap(new LLVMContext());
70 LLVMContextRef LLVMGetGlobalContext() {
71 return wrap(&getGlobalContext());
74 void LLVMContextDispose(LLVMContextRef C) {
78 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
80 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
83 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
84 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
88 /*===-- Operations on modules ---------------------------------------------===*/
90 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
91 return wrap(new Module(ModuleID, getGlobalContext()));
94 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
96 return wrap(new Module(ModuleID, *unwrap(C)));
99 void LLVMDisposeModule(LLVMModuleRef M) {
103 /*--.. Data layout .........................................................--*/
104 const char * LLVMGetDataLayout(LLVMModuleRef M) {
105 return unwrap(M)->getDataLayout().c_str();
108 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
109 unwrap(M)->setDataLayout(Triple);
112 /*--.. Target triple .......................................................--*/
113 const char * LLVMGetTarget(LLVMModuleRef M) {
114 return unwrap(M)->getTargetTriple().c_str();
117 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
118 unwrap(M)->setTargetTriple(Triple);
121 void LLVMDumpModule(LLVMModuleRef M) {
125 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
126 char **ErrorMessage) {
128 raw_fd_ostream dest(Filename, error);
129 if (!error.empty()) {
130 *ErrorMessage = strdup(error.c_str());
134 unwrap(M)->print(dest, NULL);
136 if (!error.empty()) {
137 *ErrorMessage = strdup(error.c_str());
144 /*--.. Operations on inline assembler ......................................--*/
145 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
146 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
150 /*--.. Operations on module contexts ......................................--*/
151 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
152 return wrap(&unwrap(M)->getContext());
156 /*===-- Operations on types -----------------------------------------------===*/
158 /*--.. Operations on all types (mostly) ....................................--*/
160 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
161 switch (unwrap(Ty)->getTypeID()) {
162 default: llvm_unreachable("Unhandled TypeID.");
164 return LLVMVoidTypeKind;
166 return LLVMHalfTypeKind;
167 case Type::FloatTyID:
168 return LLVMFloatTypeKind;
169 case Type::DoubleTyID:
170 return LLVMDoubleTypeKind;
171 case Type::X86_FP80TyID:
172 return LLVMX86_FP80TypeKind;
173 case Type::FP128TyID:
174 return LLVMFP128TypeKind;
175 case Type::PPC_FP128TyID:
176 return LLVMPPC_FP128TypeKind;
177 case Type::LabelTyID:
178 return LLVMLabelTypeKind;
179 case Type::MetadataTyID:
180 return LLVMMetadataTypeKind;
181 case Type::IntegerTyID:
182 return LLVMIntegerTypeKind;
183 case Type::FunctionTyID:
184 return LLVMFunctionTypeKind;
185 case Type::StructTyID:
186 return LLVMStructTypeKind;
187 case Type::ArrayTyID:
188 return LLVMArrayTypeKind;
189 case Type::PointerTyID:
190 return LLVMPointerTypeKind;
191 case Type::VectorTyID:
192 return LLVMVectorTypeKind;
193 case Type::X86_MMXTyID:
194 return LLVMX86_MMXTypeKind;
198 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
200 return unwrap(Ty)->isSized();
203 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
204 return wrap(&unwrap(Ty)->getContext());
207 /*--.. Operations on integer types .........................................--*/
209 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
210 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
212 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
213 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
215 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
216 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
218 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
219 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
221 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
222 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
224 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
225 return wrap(IntegerType::get(*unwrap(C), NumBits));
228 LLVMTypeRef LLVMInt1Type(void) {
229 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
231 LLVMTypeRef LLVMInt8Type(void) {
232 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
234 LLVMTypeRef LLVMInt16Type(void) {
235 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
237 LLVMTypeRef LLVMInt32Type(void) {
238 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
240 LLVMTypeRef LLVMInt64Type(void) {
241 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
243 LLVMTypeRef LLVMIntType(unsigned NumBits) {
244 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
247 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
248 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
251 /*--.. Operations on real types ............................................--*/
253 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
254 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
256 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
257 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
259 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
260 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
262 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
263 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
265 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
266 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
268 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
269 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
271 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
272 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
275 LLVMTypeRef LLVMHalfType(void) {
276 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
278 LLVMTypeRef LLVMFloatType(void) {
279 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
281 LLVMTypeRef LLVMDoubleType(void) {
282 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
284 LLVMTypeRef LLVMX86FP80Type(void) {
285 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
287 LLVMTypeRef LLVMFP128Type(void) {
288 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
290 LLVMTypeRef LLVMPPCFP128Type(void) {
291 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
293 LLVMTypeRef LLVMX86MMXType(void) {
294 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
297 /*--.. Operations on function types ........................................--*/
299 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
300 LLVMTypeRef *ParamTypes, unsigned ParamCount,
302 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
303 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
306 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
307 return unwrap<FunctionType>(FunctionTy)->isVarArg();
310 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
311 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
314 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
315 return unwrap<FunctionType>(FunctionTy)->getNumParams();
318 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
319 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
320 for (FunctionType::param_iterator I = Ty->param_begin(),
321 E = Ty->param_end(); I != E; ++I)
325 /*--.. Operations on struct types ..........................................--*/
327 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
328 unsigned ElementCount, LLVMBool Packed) {
329 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
330 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
333 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
334 unsigned ElementCount, LLVMBool Packed) {
335 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
336 ElementCount, Packed);
339 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
341 return wrap(StructType::create(*unwrap(C), Name));
344 const char *LLVMGetStructName(LLVMTypeRef Ty)
346 StructType *Type = unwrap<StructType>(Ty);
347 if (!Type->hasName())
349 return Type->getName().data();
352 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
353 unsigned ElementCount, LLVMBool Packed) {
354 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
355 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
358 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
359 return unwrap<StructType>(StructTy)->getNumElements();
362 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
363 StructType *Ty = unwrap<StructType>(StructTy);
364 for (StructType::element_iterator I = Ty->element_begin(),
365 E = Ty->element_end(); I != E; ++I)
369 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
370 return unwrap<StructType>(StructTy)->isPacked();
373 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
374 return unwrap<StructType>(StructTy)->isOpaque();
377 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
378 return wrap(unwrap(M)->getTypeByName(Name));
381 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
383 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
384 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
387 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
388 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
391 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
392 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
395 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
396 return wrap(unwrap<SequentialType>(Ty)->getElementType());
399 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
400 return unwrap<ArrayType>(ArrayTy)->getNumElements();
403 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
404 return unwrap<PointerType>(PointerTy)->getAddressSpace();
407 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
408 return unwrap<VectorType>(VectorTy)->getNumElements();
411 /*--.. Operations on other types ...........................................--*/
413 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
414 return wrap(Type::getVoidTy(*unwrap(C)));
416 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
417 return wrap(Type::getLabelTy(*unwrap(C)));
420 LLVMTypeRef LLVMVoidType(void) {
421 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
423 LLVMTypeRef LLVMLabelType(void) {
424 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
427 /*===-- Operations on values ----------------------------------------------===*/
429 /*--.. Operations on all values ............................................--*/
431 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
432 return wrap(unwrap(Val)->getType());
435 const char *LLVMGetValueName(LLVMValueRef Val) {
436 return unwrap(Val)->getName().data();
439 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
440 unwrap(Val)->setName(Name);
443 void LLVMDumpValue(LLVMValueRef Val) {
447 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
448 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
451 int LLVMHasMetadata(LLVMValueRef Inst) {
452 return unwrap<Instruction>(Inst)->hasMetadata();
455 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
456 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
459 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
460 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
463 /*--.. Conversion functions ................................................--*/
465 #define LLVM_DEFINE_VALUE_CAST(name) \
466 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
467 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
470 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
472 /*--.. Operations on Uses ..................................................--*/
473 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
474 Value *V = unwrap(Val);
475 Value::use_iterator I = V->use_begin();
476 if (I == V->use_end())
478 return wrap(&(I.getUse()));
481 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
482 Use *Next = unwrap(U)->getNext();
488 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
489 return wrap(unwrap(U)->getUser());
492 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
493 return wrap(unwrap(U)->get());
496 /*--.. Operations on Users .................................................--*/
497 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
498 Value *V = unwrap(Val);
499 if (MDNode *MD = dyn_cast<MDNode>(V))
500 return wrap(MD->getOperand(Index));
501 return wrap(cast<User>(V)->getOperand(Index));
504 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
505 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
508 int LLVMGetNumOperands(LLVMValueRef Val) {
509 Value *V = unwrap(Val);
510 if (MDNode *MD = dyn_cast<MDNode>(V))
511 return MD->getNumOperands();
512 return cast<User>(V)->getNumOperands();
515 /*--.. Operations on constants of any type .................................--*/
517 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
518 return wrap(Constant::getNullValue(unwrap(Ty)));
521 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
522 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
525 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
526 return wrap(UndefValue::get(unwrap(Ty)));
529 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
530 return isa<Constant>(unwrap(Ty));
533 LLVMBool LLVMIsNull(LLVMValueRef Val) {
534 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
535 return C->isNullValue();
539 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
540 return isa<UndefValue>(unwrap(Val));
543 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
545 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
548 /*--.. Operations on metadata nodes ........................................--*/
550 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
552 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
555 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
556 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
559 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
561 return wrap(MDNode::get(*unwrap(C),
562 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
565 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
566 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
569 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
570 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
571 *Len = S->getString().size();
572 return S->getString().data();
578 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
580 return cast<MDNode>(unwrap(V))->getNumOperands();
583 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
585 const MDNode *N = cast<MDNode>(unwrap(V));
586 const unsigned numOperands = N->getNumOperands();
587 for (unsigned i = 0; i < numOperands; i++)
588 Dest[i] = wrap(N->getOperand(i));
591 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
593 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
594 return N->getNumOperands();
599 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
601 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
604 for (unsigned i=0;i<N->getNumOperands();i++)
605 Dest[i] = wrap(N->getOperand(i));
608 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
611 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
614 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
619 /*--.. Operations on scalar constants ......................................--*/
621 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
622 LLVMBool SignExtend) {
623 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
626 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
628 const uint64_t Words[]) {
629 IntegerType *Ty = unwrap<IntegerType>(IntTy);
630 return wrap(ConstantInt::get(Ty->getContext(),
631 APInt(Ty->getBitWidth(),
632 makeArrayRef(Words, NumWords))));
635 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
637 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
641 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
642 unsigned SLen, uint8_t Radix) {
643 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
647 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
648 return wrap(ConstantFP::get(unwrap(RealTy), N));
651 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
652 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
655 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
657 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
660 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
661 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
664 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
665 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
668 /*--.. Operations on composite constants ...................................--*/
670 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
672 LLVMBool DontNullTerminate) {
673 /* Inverted the sense of AddNull because ', 0)' is a
674 better mnemonic for null termination than ', 1)'. */
675 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
676 DontNullTerminate == 0));
678 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
679 LLVMValueRef *ConstantVals,
680 unsigned Count, LLVMBool Packed) {
681 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
682 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
686 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
687 LLVMBool DontNullTerminate) {
688 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
691 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
692 LLVMValueRef *ConstantVals, unsigned Length) {
693 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
694 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
696 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
698 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
702 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
703 LLVMValueRef *ConstantVals,
705 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
706 StructType *Ty = cast<StructType>(unwrap(StructTy));
708 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
711 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
712 return wrap(ConstantVector::get(makeArrayRef(
713 unwrap<Constant>(ScalarConstantVals, Size), Size)));
716 /*-- Opcode mapping */
718 static LLVMOpcode map_to_llvmopcode(int opcode)
721 default: llvm_unreachable("Unhandled Opcode.");
722 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
723 #include "llvm/IR/Instruction.def"
728 static int map_from_llvmopcode(LLVMOpcode code)
731 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
732 #include "llvm/IR/Instruction.def"
735 llvm_unreachable("Unhandled Opcode.");
738 /*--.. Constant expressions ................................................--*/
740 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
741 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
744 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
745 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
748 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
749 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
752 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
753 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
756 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
757 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
760 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
761 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
765 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
766 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
769 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
770 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
773 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
774 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
775 unwrap<Constant>(RHSConstant)));
778 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
779 LLVMValueRef RHSConstant) {
780 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
781 unwrap<Constant>(RHSConstant)));
784 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
785 LLVMValueRef RHSConstant) {
786 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
787 unwrap<Constant>(RHSConstant)));
790 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
791 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
792 unwrap<Constant>(RHSConstant)));
795 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
796 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
797 unwrap<Constant>(RHSConstant)));
800 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
801 LLVMValueRef RHSConstant) {
802 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
803 unwrap<Constant>(RHSConstant)));
806 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
807 LLVMValueRef RHSConstant) {
808 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
809 unwrap<Constant>(RHSConstant)));
812 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
813 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
814 unwrap<Constant>(RHSConstant)));
817 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
818 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
819 unwrap<Constant>(RHSConstant)));
822 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
823 LLVMValueRef RHSConstant) {
824 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
825 unwrap<Constant>(RHSConstant)));
828 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
829 LLVMValueRef RHSConstant) {
830 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
831 unwrap<Constant>(RHSConstant)));
834 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
835 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
836 unwrap<Constant>(RHSConstant)));
839 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
840 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
841 unwrap<Constant>(RHSConstant)));
844 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
845 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
846 unwrap<Constant>(RHSConstant)));
849 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
850 LLVMValueRef RHSConstant) {
851 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
852 unwrap<Constant>(RHSConstant)));
855 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
856 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
857 unwrap<Constant>(RHSConstant)));
860 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
861 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
862 unwrap<Constant>(RHSConstant)));
865 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
866 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
867 unwrap<Constant>(RHSConstant)));
870 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
871 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
872 unwrap<Constant>(RHSConstant)));
875 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
876 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
877 unwrap<Constant>(RHSConstant)));
880 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
881 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
882 unwrap<Constant>(RHSConstant)));
885 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
886 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
887 unwrap<Constant>(RHSConstant)));
890 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
891 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
892 return wrap(ConstantExpr::getICmp(Predicate,
893 unwrap<Constant>(LHSConstant),
894 unwrap<Constant>(RHSConstant)));
897 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
898 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
899 return wrap(ConstantExpr::getFCmp(Predicate,
900 unwrap<Constant>(LHSConstant),
901 unwrap<Constant>(RHSConstant)));
904 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
905 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
906 unwrap<Constant>(RHSConstant)));
909 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
910 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
911 unwrap<Constant>(RHSConstant)));
914 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
915 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
916 unwrap<Constant>(RHSConstant)));
919 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
920 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
921 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
923 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
927 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
928 LLVMValueRef *ConstantIndices,
929 unsigned NumIndices) {
930 Constant* Val = unwrap<Constant>(ConstantVal);
931 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
933 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
936 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
937 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
941 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
942 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
946 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
947 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
951 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
952 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
956 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
957 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
961 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
962 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
966 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
967 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
971 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
972 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
976 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
977 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
981 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
982 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
986 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
987 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
991 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
992 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
996 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
997 LLVMTypeRef ToType) {
998 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1002 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1003 LLVMTypeRef ToType) {
1004 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1008 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1009 LLVMTypeRef ToType) {
1010 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1014 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1015 LLVMTypeRef ToType) {
1016 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1020 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1021 LLVMBool isSigned) {
1022 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1023 unwrap(ToType), isSigned));
1026 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1027 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1031 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1032 LLVMValueRef ConstantIfTrue,
1033 LLVMValueRef ConstantIfFalse) {
1034 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1035 unwrap<Constant>(ConstantIfTrue),
1036 unwrap<Constant>(ConstantIfFalse)));
1039 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1040 LLVMValueRef IndexConstant) {
1041 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1042 unwrap<Constant>(IndexConstant)));
1045 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1046 LLVMValueRef ElementValueConstant,
1047 LLVMValueRef IndexConstant) {
1048 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1049 unwrap<Constant>(ElementValueConstant),
1050 unwrap<Constant>(IndexConstant)));
1053 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1054 LLVMValueRef VectorBConstant,
1055 LLVMValueRef MaskConstant) {
1056 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1057 unwrap<Constant>(VectorBConstant),
1058 unwrap<Constant>(MaskConstant)));
1061 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1063 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1064 makeArrayRef(IdxList, NumIdx)));
1067 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1068 LLVMValueRef ElementValueConstant,
1069 unsigned *IdxList, unsigned NumIdx) {
1070 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1071 unwrap<Constant>(ElementValueConstant),
1072 makeArrayRef(IdxList, NumIdx)));
1075 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1076 const char *Constraints,
1077 LLVMBool HasSideEffects,
1078 LLVMBool IsAlignStack) {
1079 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1080 Constraints, HasSideEffects, IsAlignStack));
1083 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1084 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1087 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1089 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1090 return wrap(unwrap<GlobalValue>(Global)->getParent());
1093 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1094 return unwrap<GlobalValue>(Global)->isDeclaration();
1097 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1098 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1099 case GlobalValue::ExternalLinkage:
1100 return LLVMExternalLinkage;
1101 case GlobalValue::AvailableExternallyLinkage:
1102 return LLVMAvailableExternallyLinkage;
1103 case GlobalValue::LinkOnceAnyLinkage:
1104 return LLVMLinkOnceAnyLinkage;
1105 case GlobalValue::LinkOnceODRLinkage:
1106 return LLVMLinkOnceODRLinkage;
1107 case GlobalValue::LinkOnceODRAutoHideLinkage:
1108 return LLVMLinkOnceODRAutoHideLinkage;
1109 case GlobalValue::WeakAnyLinkage:
1110 return LLVMWeakAnyLinkage;
1111 case GlobalValue::WeakODRLinkage:
1112 return LLVMWeakODRLinkage;
1113 case GlobalValue::AppendingLinkage:
1114 return LLVMAppendingLinkage;
1115 case GlobalValue::InternalLinkage:
1116 return LLVMInternalLinkage;
1117 case GlobalValue::PrivateLinkage:
1118 return LLVMPrivateLinkage;
1119 case GlobalValue::LinkerPrivateLinkage:
1120 return LLVMLinkerPrivateLinkage;
1121 case GlobalValue::LinkerPrivateWeakLinkage:
1122 return LLVMLinkerPrivateWeakLinkage;
1123 case GlobalValue::DLLImportLinkage:
1124 return LLVMDLLImportLinkage;
1125 case GlobalValue::DLLExportLinkage:
1126 return LLVMDLLExportLinkage;
1127 case GlobalValue::ExternalWeakLinkage:
1128 return LLVMExternalWeakLinkage;
1129 case GlobalValue::CommonLinkage:
1130 return LLVMCommonLinkage;
1133 llvm_unreachable("Invalid GlobalValue linkage!");
1136 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1137 GlobalValue *GV = unwrap<GlobalValue>(Global);
1140 case LLVMExternalLinkage:
1141 GV->setLinkage(GlobalValue::ExternalLinkage);
1143 case LLVMAvailableExternallyLinkage:
1144 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1146 case LLVMLinkOnceAnyLinkage:
1147 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1149 case LLVMLinkOnceODRLinkage:
1150 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1152 case LLVMLinkOnceODRAutoHideLinkage:
1153 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1155 case LLVMWeakAnyLinkage:
1156 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1158 case LLVMWeakODRLinkage:
1159 GV->setLinkage(GlobalValue::WeakODRLinkage);
1161 case LLVMAppendingLinkage:
1162 GV->setLinkage(GlobalValue::AppendingLinkage);
1164 case LLVMInternalLinkage:
1165 GV->setLinkage(GlobalValue::InternalLinkage);
1167 case LLVMPrivateLinkage:
1168 GV->setLinkage(GlobalValue::PrivateLinkage);
1170 case LLVMLinkerPrivateLinkage:
1171 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1173 case LLVMLinkerPrivateWeakLinkage:
1174 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1176 case LLVMDLLImportLinkage:
1177 GV->setLinkage(GlobalValue::DLLImportLinkage);
1179 case LLVMDLLExportLinkage:
1180 GV->setLinkage(GlobalValue::DLLExportLinkage);
1182 case LLVMExternalWeakLinkage:
1183 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1185 case LLVMGhostLinkage:
1187 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1189 case LLVMCommonLinkage:
1190 GV->setLinkage(GlobalValue::CommonLinkage);
1195 const char *LLVMGetSection(LLVMValueRef Global) {
1196 return unwrap<GlobalValue>(Global)->getSection().c_str();
1199 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1200 unwrap<GlobalValue>(Global)->setSection(Section);
1203 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1204 return static_cast<LLVMVisibility>(
1205 unwrap<GlobalValue>(Global)->getVisibility());
1208 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1209 unwrap<GlobalValue>(Global)
1210 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1213 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1214 return unwrap<GlobalValue>(Global)->getAlignment();
1217 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1218 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1221 /*--.. Operations on global variables ......................................--*/
1223 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1224 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1225 GlobalValue::ExternalLinkage, 0, Name));
1228 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1230 unsigned AddressSpace) {
1231 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1232 GlobalValue::ExternalLinkage, 0, Name, 0,
1233 GlobalVariable::NotThreadLocal, AddressSpace));
1236 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1237 return wrap(unwrap(M)->getNamedGlobal(Name));
1240 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1241 Module *Mod = unwrap(M);
1242 Module::global_iterator I = Mod->global_begin();
1243 if (I == Mod->global_end())
1248 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1249 Module *Mod = unwrap(M);
1250 Module::global_iterator I = Mod->global_end();
1251 if (I == Mod->global_begin())
1256 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1257 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1258 Module::global_iterator I = GV;
1259 if (++I == GV->getParent()->global_end())
1264 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1265 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1266 Module::global_iterator I = GV;
1267 if (I == GV->getParent()->global_begin())
1272 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1273 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1276 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1277 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1278 if ( !GV->hasInitializer() )
1280 return wrap(GV->getInitializer());
1283 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1284 unwrap<GlobalVariable>(GlobalVar)
1285 ->setInitializer(unwrap<Constant>(ConstantVal));
1288 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1289 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1292 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1293 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1296 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1297 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1300 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1301 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1304 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1305 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1306 case GlobalVariable::NotThreadLocal:
1307 return LLVMNotThreadLocal;
1308 case GlobalVariable::GeneralDynamicTLSModel:
1309 return LLVMGeneralDynamicTLSModel;
1310 case GlobalVariable::LocalDynamicTLSModel:
1311 return LLVMLocalDynamicTLSModel;
1312 case GlobalVariable::InitialExecTLSModel:
1313 return LLVMInitialExecTLSModel;
1314 case GlobalVariable::LocalExecTLSModel:
1315 return LLVMLocalExecTLSModel;
1318 llvm_unreachable("Invalid GlobalVariable thread local mode");
1321 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1322 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1325 case LLVMNotThreadLocal:
1326 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1328 case LLVMGeneralDynamicTLSModel:
1329 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1331 case LLVMLocalDynamicTLSModel:
1332 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1334 case LLVMInitialExecTLSModel:
1335 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1337 case LLVMLocalExecTLSModel:
1338 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1343 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1344 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1347 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1348 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1351 /*--.. Operations on aliases ......................................--*/
1353 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1355 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1356 unwrap<Constant>(Aliasee), unwrap (M)));
1359 /*--.. Operations on functions .............................................--*/
1361 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1362 LLVMTypeRef FunctionTy) {
1363 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1364 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1367 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1368 return wrap(unwrap(M)->getFunction(Name));
1371 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1372 Module *Mod = unwrap(M);
1373 Module::iterator I = Mod->begin();
1374 if (I == Mod->end())
1379 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1380 Module *Mod = unwrap(M);
1381 Module::iterator I = Mod->end();
1382 if (I == Mod->begin())
1387 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1388 Function *Func = unwrap<Function>(Fn);
1389 Module::iterator I = Func;
1390 if (++I == Func->getParent()->end())
1395 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1396 Function *Func = unwrap<Function>(Fn);
1397 Module::iterator I = Func;
1398 if (I == Func->getParent()->begin())
1403 void LLVMDeleteFunction(LLVMValueRef Fn) {
1404 unwrap<Function>(Fn)->eraseFromParent();
1407 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1408 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1409 return F->getIntrinsicID();
1413 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1414 return unwrap<Function>(Fn)->getCallingConv();
1417 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1418 return unwrap<Function>(Fn)->setCallingConv(
1419 static_cast<CallingConv::ID>(CC));
1422 const char *LLVMGetGC(LLVMValueRef Fn) {
1423 Function *F = unwrap<Function>(Fn);
1424 return F->hasGC()? F->getGC() : 0;
1427 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1428 Function *F = unwrap<Function>(Fn);
1435 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1436 Function *Func = unwrap<Function>(Fn);
1437 const AttributeSet PAL = Func->getAttributes();
1439 const AttributeSet PALnew =
1440 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1441 AttributeSet::get(Func->getContext(),
1442 AttributeSet::FunctionIndex, B));
1443 Func->setAttributes(PALnew);
1446 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1448 Function *Func = unwrap<Function>(Fn);
1449 int Idx = AttributeSet::FunctionIndex;
1452 B.addAttribute(A, V);
1453 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1454 Func->addAttributes(Idx, Set);
1457 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1458 Function *Func = unwrap<Function>(Fn);
1459 const AttributeSet PAL = Func->getAttributes();
1461 const AttributeSet PALnew =
1462 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1463 AttributeSet::get(Func->getContext(),
1464 AttributeSet::FunctionIndex, B));
1465 Func->setAttributes(PALnew);
1468 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1469 Function *Func = unwrap<Function>(Fn);
1470 const AttributeSet PAL = Func->getAttributes();
1471 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1474 /*--.. Operations on parameters ............................................--*/
1476 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1477 // This function is strictly redundant to
1478 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1479 return unwrap<Function>(FnRef)->arg_size();
1482 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1483 Function *Fn = unwrap<Function>(FnRef);
1484 for (Function::arg_iterator I = Fn->arg_begin(),
1485 E = Fn->arg_end(); I != E; I++)
1486 *ParamRefs++ = wrap(I);
1489 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1490 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1496 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1497 return wrap(unwrap<Argument>(V)->getParent());
1500 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1501 Function *Func = unwrap<Function>(Fn);
1502 Function::arg_iterator I = Func->arg_begin();
1503 if (I == Func->arg_end())
1508 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1509 Function *Func = unwrap<Function>(Fn);
1510 Function::arg_iterator I = Func->arg_end();
1511 if (I == Func->arg_begin())
1516 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1517 Argument *A = unwrap<Argument>(Arg);
1518 Function::arg_iterator I = A;
1519 if (++I == A->getParent()->arg_end())
1524 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1525 Argument *A = unwrap<Argument>(Arg);
1526 Function::arg_iterator I = A;
1527 if (I == A->getParent()->arg_begin())
1532 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1533 Argument *A = unwrap<Argument>(Arg);
1535 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1538 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1539 Argument *A = unwrap<Argument>(Arg);
1541 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1544 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1545 Argument *A = unwrap<Argument>(Arg);
1546 return (LLVMAttribute)A->getParent()->getAttributes().
1547 Raw(A->getArgNo()+1);
1551 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1552 Argument *A = unwrap<Argument>(Arg);
1554 B.addAlignmentAttr(align);
1555 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1558 /*--.. Operations on basic blocks ..........................................--*/
1560 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1561 return wrap(static_cast<Value*>(unwrap(BB)));
1564 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1565 return isa<BasicBlock>(unwrap(Val));
1568 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1569 return wrap(unwrap<BasicBlock>(Val));
1572 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1573 return wrap(unwrap(BB)->getParent());
1576 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1577 return wrap(unwrap(BB)->getTerminator());
1580 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1581 return unwrap<Function>(FnRef)->size();
1584 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1585 Function *Fn = unwrap<Function>(FnRef);
1586 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1587 *BasicBlocksRefs++ = wrap(I);
1590 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1591 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1594 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1595 Function *Func = unwrap<Function>(Fn);
1596 Function::iterator I = Func->begin();
1597 if (I == Func->end())
1602 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1603 Function *Func = unwrap<Function>(Fn);
1604 Function::iterator I = Func->end();
1605 if (I == Func->begin())
1610 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1611 BasicBlock *Block = unwrap(BB);
1612 Function::iterator I = Block;
1613 if (++I == Block->getParent()->end())
1618 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1619 BasicBlock *Block = unwrap(BB);
1620 Function::iterator I = Block;
1621 if (I == Block->getParent()->begin())
1626 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1629 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1632 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1633 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1636 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1637 LLVMBasicBlockRef BBRef,
1639 BasicBlock *BB = unwrap(BBRef);
1640 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1643 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1645 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1648 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1649 unwrap(BBRef)->eraseFromParent();
1652 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1653 unwrap(BBRef)->removeFromParent();
1656 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1657 unwrap(BB)->moveBefore(unwrap(MovePos));
1660 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1661 unwrap(BB)->moveAfter(unwrap(MovePos));
1664 /*--.. Operations on instructions ..........................................--*/
1666 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1667 return wrap(unwrap<Instruction>(Inst)->getParent());
1670 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1671 BasicBlock *Block = unwrap(BB);
1672 BasicBlock::iterator I = Block->begin();
1673 if (I == Block->end())
1678 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1679 BasicBlock *Block = unwrap(BB);
1680 BasicBlock::iterator I = Block->end();
1681 if (I == Block->begin())
1686 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1687 Instruction *Instr = unwrap<Instruction>(Inst);
1688 BasicBlock::iterator I = Instr;
1689 if (++I == Instr->getParent()->end())
1694 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1695 Instruction *Instr = unwrap<Instruction>(Inst);
1696 BasicBlock::iterator I = Instr;
1697 if (I == Instr->getParent()->begin())
1702 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1703 unwrap<Instruction>(Inst)->eraseFromParent();
1706 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1707 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1708 return (LLVMIntPredicate)I->getPredicate();
1709 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1710 if (CE->getOpcode() == Instruction::ICmp)
1711 return (LLVMIntPredicate)CE->getPredicate();
1712 return (LLVMIntPredicate)0;
1715 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1716 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1717 return map_to_llvmopcode(C->getOpcode());
1718 return (LLVMOpcode)0;
1721 /*--.. Call and invoke instructions ........................................--*/
1723 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1724 Value *V = unwrap(Instr);
1725 if (CallInst *CI = dyn_cast<CallInst>(V))
1726 return CI->getCallingConv();
1727 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1728 return II->getCallingConv();
1729 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1732 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1733 Value *V = unwrap(Instr);
1734 if (CallInst *CI = dyn_cast<CallInst>(V))
1735 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1736 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1737 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1738 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1741 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1743 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1746 Call.getAttributes().addAttributes(Call->getContext(), index,
1747 AttributeSet::get(Call->getContext(),
1751 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1753 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1755 Call.setAttributes(Call.getAttributes()
1756 .removeAttributes(Call->getContext(), index,
1757 AttributeSet::get(Call->getContext(),
1761 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1763 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1765 B.addAlignmentAttr(align);
1766 Call.setAttributes(Call.getAttributes()
1767 .addAttributes(Call->getContext(), index,
1768 AttributeSet::get(Call->getContext(),
1772 /*--.. Operations on call instructions (only) ..............................--*/
1774 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1775 return unwrap<CallInst>(Call)->isTailCall();
1778 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1779 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1782 /*--.. Operations on switch instructions (only) ............................--*/
1784 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1785 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1788 /*--.. Operations on phi nodes .............................................--*/
1790 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1791 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1792 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1793 for (unsigned I = 0; I != Count; ++I)
1794 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1797 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1798 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1801 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1802 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1805 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1806 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1810 /*===-- Instruction builders ----------------------------------------------===*/
1812 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1813 return wrap(new IRBuilder<>(*unwrap(C)));
1816 LLVMBuilderRef LLVMCreateBuilder(void) {
1817 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1820 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1821 LLVMValueRef Instr) {
1822 BasicBlock *BB = unwrap(Block);
1823 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1824 unwrap(Builder)->SetInsertPoint(BB, I);
1827 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1828 Instruction *I = unwrap<Instruction>(Instr);
1829 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1832 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1833 BasicBlock *BB = unwrap(Block);
1834 unwrap(Builder)->SetInsertPoint(BB);
1837 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1838 return wrap(unwrap(Builder)->GetInsertBlock());
1841 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1842 unwrap(Builder)->ClearInsertionPoint();
1845 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1846 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1849 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1851 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1854 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1855 delete unwrap(Builder);
1858 /*--.. Metadata builders ...................................................--*/
1860 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1861 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1862 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1865 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1866 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1867 .getAsMDNode(unwrap(Builder)->getContext()));
1870 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1871 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1875 /*--.. Instruction builders ................................................--*/
1877 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1878 return wrap(unwrap(B)->CreateRetVoid());
1881 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1882 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1885 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1887 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1890 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1891 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1894 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1895 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1896 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1899 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1900 LLVMBasicBlockRef Else, unsigned NumCases) {
1901 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1904 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1905 unsigned NumDests) {
1906 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1909 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1910 LLVMValueRef *Args, unsigned NumArgs,
1911 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1913 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1914 makeArrayRef(unwrap(Args), NumArgs),
1918 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1919 LLVMValueRef PersFn, unsigned NumClauses,
1921 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1922 cast<Function>(unwrap(PersFn)),
1926 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1927 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1930 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1931 return wrap(unwrap(B)->CreateUnreachable());
1934 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1935 LLVMBasicBlockRef Dest) {
1936 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1939 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1940 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1943 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1944 unwrap<LandingPadInst>(LandingPad)->
1945 addClause(cast<Constant>(unwrap(ClauseVal)));
1948 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1949 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1952 /*--.. Arithmetic ..........................................................--*/
1954 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1956 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1959 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1961 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1964 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1966 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1969 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1971 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1974 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1976 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1979 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1981 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1984 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1986 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1989 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1991 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1994 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1996 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1999 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2001 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2004 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2006 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2009 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2011 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2014 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2016 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2019 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2021 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2024 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2025 LLVMValueRef RHS, const char *Name) {
2026 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2029 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2031 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2034 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2036 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2039 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2041 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2044 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2046 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2049 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2051 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2054 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2056 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2059 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2061 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2064 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2066 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2069 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2071 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2074 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2076 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2079 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2080 LLVMValueRef LHS, LLVMValueRef RHS,
2082 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2083 unwrap(RHS), Name));
2086 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2087 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2090 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2092 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2095 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2097 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2100 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2101 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2104 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2105 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2108 /*--.. Memory ..............................................................--*/
2110 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2112 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2113 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2114 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2115 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2116 ITy, unwrap(Ty), AllocSize,
2118 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2121 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2122 LLVMValueRef Val, const char *Name) {
2123 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2124 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2125 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2126 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2127 ITy, unwrap(Ty), AllocSize,
2128 unwrap(Val), 0, "");
2129 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2132 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2134 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2137 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2138 LLVMValueRef Val, const char *Name) {
2139 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2142 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2143 return wrap(unwrap(B)->Insert(
2144 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2148 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2150 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2153 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2154 LLVMValueRef PointerVal) {
2155 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2158 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2159 LLVMValueRef *Indices, unsigned NumIndices,
2161 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2162 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2165 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2166 LLVMValueRef *Indices, unsigned NumIndices,
2168 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2169 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2172 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2173 unsigned Idx, const char *Name) {
2174 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2177 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2179 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2182 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2184 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2187 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2188 Value *P = unwrap<Value>(MemAccessInst);
2189 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2190 return LI->isVolatile();
2191 return cast<StoreInst>(P)->isVolatile();
2194 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2195 Value *P = unwrap<Value>(MemAccessInst);
2196 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2197 return LI->setVolatile(isVolatile);
2198 return cast<StoreInst>(P)->setVolatile(isVolatile);
2201 /*--.. Casts ...............................................................--*/
2203 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2204 LLVMTypeRef DestTy, const char *Name) {
2205 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2208 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2209 LLVMTypeRef DestTy, const char *Name) {
2210 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2213 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2214 LLVMTypeRef DestTy, const char *Name) {
2215 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2218 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2219 LLVMTypeRef DestTy, const char *Name) {
2220 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2223 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2224 LLVMTypeRef DestTy, const char *Name) {
2225 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2228 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2229 LLVMTypeRef DestTy, const char *Name) {
2230 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2233 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2234 LLVMTypeRef DestTy, const char *Name) {
2235 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2238 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2239 LLVMTypeRef DestTy, const char *Name) {
2240 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2243 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2244 LLVMTypeRef DestTy, const char *Name) {
2245 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2248 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2249 LLVMTypeRef DestTy, const char *Name) {
2250 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2253 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2254 LLVMTypeRef DestTy, const char *Name) {
2255 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2258 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2259 LLVMTypeRef DestTy, const char *Name) {
2260 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2263 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2264 LLVMTypeRef DestTy, const char *Name) {
2265 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2269 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2270 LLVMTypeRef DestTy, const char *Name) {
2271 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2275 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2276 LLVMTypeRef DestTy, const char *Name) {
2277 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2281 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2282 LLVMTypeRef DestTy, const char *Name) {
2283 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2284 unwrap(DestTy), Name));
2287 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2288 LLVMTypeRef DestTy, const char *Name) {
2289 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2292 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2293 LLVMTypeRef DestTy, const char *Name) {
2294 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2295 /*isSigned*/true, Name));
2298 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2299 LLVMTypeRef DestTy, const char *Name) {
2300 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2303 /*--.. Comparisons .........................................................--*/
2305 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2306 LLVMValueRef LHS, LLVMValueRef RHS,
2308 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2309 unwrap(LHS), unwrap(RHS), Name));
2312 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2313 LLVMValueRef LHS, LLVMValueRef RHS,
2315 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2316 unwrap(LHS), unwrap(RHS), Name));
2319 /*--.. Miscellaneous instructions ..........................................--*/
2321 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2322 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2325 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2326 LLVMValueRef *Args, unsigned NumArgs,
2328 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2329 makeArrayRef(unwrap(Args), NumArgs),
2333 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2334 LLVMValueRef Then, LLVMValueRef Else,
2336 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2340 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2341 LLVMTypeRef Ty, const char *Name) {
2342 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2345 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2346 LLVMValueRef Index, const char *Name) {
2347 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2351 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2352 LLVMValueRef EltVal, LLVMValueRef Index,
2354 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2355 unwrap(Index), Name));
2358 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2359 LLVMValueRef V2, LLVMValueRef Mask,
2361 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2362 unwrap(Mask), Name));
2365 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2366 unsigned Index, const char *Name) {
2367 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2370 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2371 LLVMValueRef EltVal, unsigned Index,
2373 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2377 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2379 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2382 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2384 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2387 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2388 LLVMValueRef RHS, const char *Name) {
2389 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2393 /*===-- Module providers --------------------------------------------------===*/
2395 LLVMModuleProviderRef
2396 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2397 return reinterpret_cast<LLVMModuleProviderRef>(M);
2400 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2405 /*===-- Memory buffers ----------------------------------------------------===*/
2407 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2409 LLVMMemoryBufferRef *OutMemBuf,
2410 char **OutMessage) {
2412 OwningPtr<MemoryBuffer> MB;
2414 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2415 *OutMemBuf = wrap(MB.take());
2419 *OutMessage = strdup(ec.message().c_str());
2423 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2424 char **OutMessage) {
2425 OwningPtr<MemoryBuffer> MB;
2427 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2428 *OutMemBuf = wrap(MB.take());
2432 *OutMessage = strdup(ec.message().c_str());
2436 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2437 const char *InputData,
2438 size_t InputDataLength,
2439 const char *BufferName,
2440 LLVMBool RequiresNullTerminator) {
2442 return wrap(MemoryBuffer::getMemBuffer(
2443 StringRef(InputData, InputDataLength),
2444 StringRef(BufferName),
2445 RequiresNullTerminator));
2448 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2449 const char *InputData,
2450 size_t InputDataLength,
2451 const char *BufferName) {
2453 return wrap(MemoryBuffer::getMemBufferCopy(
2454 StringRef(InputData, InputDataLength),
2455 StringRef(BufferName)));
2458 const char* LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2459 return unwrap(MemBuf)->getBufferStart();
2462 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2463 delete unwrap(MemBuf);
2466 /*===-- Pass Registry -----------------------------------------------------===*/
2468 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2469 return wrap(PassRegistry::getPassRegistry());
2472 /*===-- Pass Manager ------------------------------------------------------===*/
2474 LLVMPassManagerRef LLVMCreatePassManager() {
2475 return wrap(new PassManager());
2478 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2479 return wrap(new FunctionPassManager(unwrap(M)));
2482 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2483 return LLVMCreateFunctionPassManagerForModule(
2484 reinterpret_cast<LLVMModuleRef>(P));
2487 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2488 return unwrap<PassManager>(PM)->run(*unwrap(M));
2491 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2492 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2495 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2496 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2499 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2500 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2503 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2507 /*===-- Threading ------------------------------------------------------===*/
2509 LLVMBool LLVMStartMultithreaded() {
2510 return llvm_start_multithreaded();
2513 void LLVMStopMultithreaded() {
2514 llvm_stop_multithreaded();
2517 LLVMBool LLVMIsMultithreaded() {
2518 return llvm_is_multithreaded();