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 C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.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"
38 /*===-- Error handling ----------------------------------------------------===*/
40 void LLVMDisposeMessage(char *Message) {
45 /*===-- Operations on contexts --------------------------------------------===*/
47 LLVMContextRef LLVMContextCreate() {
48 return wrap(new LLVMContext());
51 LLVMContextRef LLVMGetGlobalContext() {
52 return wrap(&getGlobalContext());
55 void LLVMContextDispose(LLVMContextRef C) {
59 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
61 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
64 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
65 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
69 /*===-- Operations on modules ---------------------------------------------===*/
71 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
72 return wrap(new Module(ModuleID, getGlobalContext()));
75 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
77 return wrap(new Module(ModuleID, *unwrap(C)));
80 void LLVMDisposeModule(LLVMModuleRef M) {
84 /*--.. Data layout .........................................................--*/
85 const char * LLVMGetDataLayout(LLVMModuleRef M) {
86 return unwrap(M)->getDataLayout().c_str();
89 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
90 unwrap(M)->setDataLayout(Triple);
93 /*--.. Target triple .......................................................--*/
94 const char * LLVMGetTarget(LLVMModuleRef M) {
95 return unwrap(M)->getTargetTriple().c_str();
98 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
99 unwrap(M)->setTargetTriple(Triple);
102 /*--.. Type names ..........................................................--*/
103 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
104 return unwrap(M)->addTypeName(Name, unwrap(Ty));
107 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
108 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
110 TypeSymbolTable::iterator I = TST.find(Name);
115 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
116 return wrap(unwrap(M)->getTypeByName(Name));
119 void LLVMDumpModule(LLVMModuleRef M) {
123 /*--.. Operations on inline assembler ......................................--*/
124 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
125 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
129 /*===-- Operations on types -----------------------------------------------===*/
131 /*--.. Operations on all types (mostly) ....................................--*/
133 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
134 switch (unwrap(Ty)->getTypeID()) {
136 assert(false && "Unhandled TypeID.");
138 return LLVMVoidTypeKind;
139 case Type::FloatTyID:
140 return LLVMFloatTypeKind;
141 case Type::DoubleTyID:
142 return LLVMDoubleTypeKind;
143 case Type::X86_FP80TyID:
144 return LLVMX86_FP80TypeKind;
145 case Type::FP128TyID:
146 return LLVMFP128TypeKind;
147 case Type::PPC_FP128TyID:
148 return LLVMPPC_FP128TypeKind;
149 case Type::LabelTyID:
150 return LLVMLabelTypeKind;
151 case Type::MetadataTyID:
152 return LLVMMetadataTypeKind;
153 case Type::IntegerTyID:
154 return LLVMIntegerTypeKind;
155 case Type::FunctionTyID:
156 return LLVMFunctionTypeKind;
157 case Type::StructTyID:
158 return LLVMStructTypeKind;
159 case Type::UnionTyID:
160 return LLVMUnionTypeKind;
161 case Type::ArrayTyID:
162 return LLVMArrayTypeKind;
163 case Type::PointerTyID:
164 return LLVMPointerTypeKind;
165 case Type::OpaqueTyID:
166 return LLVMOpaqueTypeKind;
167 case Type::VectorTyID:
168 return LLVMVectorTypeKind;
172 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
173 return wrap(&unwrap(Ty)->getContext());
176 /*--.. Operations on integer types .........................................--*/
178 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
179 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
181 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
182 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
184 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
185 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
187 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
188 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
190 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
191 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
193 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
194 return wrap(IntegerType::get(*unwrap(C), NumBits));
197 LLVMTypeRef LLVMInt1Type(void) {
198 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
200 LLVMTypeRef LLVMInt8Type(void) {
201 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
203 LLVMTypeRef LLVMInt16Type(void) {
204 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
206 LLVMTypeRef LLVMInt32Type(void) {
207 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
209 LLVMTypeRef LLVMInt64Type(void) {
210 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
212 LLVMTypeRef LLVMIntType(unsigned NumBits) {
213 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
216 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
217 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
220 /*--.. Operations on real types ............................................--*/
222 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
223 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
225 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
226 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
228 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
229 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
231 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
232 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
234 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
235 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
238 LLVMTypeRef LLVMFloatType(void) {
239 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
241 LLVMTypeRef LLVMDoubleType(void) {
242 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
244 LLVMTypeRef LLVMX86FP80Type(void) {
245 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
247 LLVMTypeRef LLVMFP128Type(void) {
248 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
250 LLVMTypeRef LLVMPPCFP128Type(void) {
251 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
254 /*--.. Operations on function types ........................................--*/
256 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
257 LLVMTypeRef *ParamTypes, unsigned ParamCount,
259 std::vector<const Type*> Tys;
260 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
261 Tys.push_back(unwrap(*I));
263 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
266 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
267 return unwrap<FunctionType>(FunctionTy)->isVarArg();
270 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
271 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
274 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
275 return unwrap<FunctionType>(FunctionTy)->getNumParams();
278 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
279 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
280 for (FunctionType::param_iterator I = Ty->param_begin(),
281 E = Ty->param_end(); I != E; ++I)
285 /*--.. Operations on struct types ..........................................--*/
287 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
288 unsigned ElementCount, LLVMBool Packed) {
289 std::vector<const Type*> Tys;
290 for (LLVMTypeRef *I = ElementTypes,
291 *E = ElementTypes + ElementCount; I != E; ++I)
292 Tys.push_back(unwrap(*I));
294 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
297 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
298 unsigned ElementCount, LLVMBool Packed) {
299 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
300 ElementCount, Packed);
304 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
305 return unwrap<StructType>(StructTy)->getNumElements();
308 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
309 StructType *Ty = unwrap<StructType>(StructTy);
310 for (FunctionType::param_iterator I = Ty->element_begin(),
311 E = Ty->element_end(); I != E; ++I)
315 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
316 return unwrap<StructType>(StructTy)->isPacked();
319 /*--.. Operations on union types ..........................................--*/
321 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
322 unsigned ElementCount) {
323 SmallVector<const Type*, 8> Tys;
324 for (LLVMTypeRef *I = ElementTypes,
325 *E = ElementTypes + ElementCount; I != E; ++I)
326 Tys.push_back(unwrap(*I));
328 return wrap(UnionType::get(&Tys[0], Tys.size()));
331 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes, unsigned ElementCount) {
332 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
336 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
337 return unwrap<UnionType>(UnionTy)->getNumElements();
340 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
341 UnionType *Ty = unwrap<UnionType>(UnionTy);
342 for (FunctionType::param_iterator I = Ty->element_begin(),
343 E = Ty->element_end(); I != E; ++I)
347 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
349 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
350 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
353 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
354 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
357 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
358 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
361 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
362 return wrap(unwrap<SequentialType>(Ty)->getElementType());
365 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
366 return unwrap<ArrayType>(ArrayTy)->getNumElements();
369 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
370 return unwrap<PointerType>(PointerTy)->getAddressSpace();
373 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
374 return unwrap<VectorType>(VectorTy)->getNumElements();
377 /*--.. Operations on other types ...........................................--*/
379 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
380 return wrap(Type::getVoidTy(*unwrap(C)));
382 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
383 return wrap(Type::getLabelTy(*unwrap(C)));
385 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
386 return wrap(OpaqueType::get(*unwrap(C)));
389 LLVMTypeRef LLVMVoidType(void) {
390 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
392 LLVMTypeRef LLVMLabelType(void) {
393 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
395 LLVMTypeRef LLVMOpaqueType(void) {
396 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
399 /*--.. Operations on type handles ..........................................--*/
401 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
402 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
405 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
406 delete unwrap(TypeHandle);
409 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
410 return wrap(unwrap(TypeHandle)->get());
413 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
414 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
418 /*===-- Operations on values ----------------------------------------------===*/
420 /*--.. Operations on all values ............................................--*/
422 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
423 return wrap(unwrap(Val)->getType());
426 const char *LLVMGetValueName(LLVMValueRef Val) {
427 return unwrap(Val)->getName().data();
430 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
431 unwrap(Val)->setName(Name);
434 void LLVMDumpValue(LLVMValueRef Val) {
438 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
439 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
442 int LLVMHasMetadata(LLVMValueRef Inst) {
443 return unwrap<Instruction>(Inst)->hasMetadata();
446 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
447 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
450 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
451 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
454 /*--.. Conversion functions ................................................--*/
456 #define LLVM_DEFINE_VALUE_CAST(name) \
457 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
458 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
461 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
463 /*--.. Operations on Uses ..................................................--*/
464 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
465 Value *V = unwrap(Val);
466 Value::use_iterator I = V->use_begin();
467 if (I == V->use_end())
469 return wrap(&(I.getUse()));
472 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
473 Use *Next = unwrap(U)->getNext();
479 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
480 return wrap(unwrap(U)->getUser());
483 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
484 return wrap(unwrap(U)->get());
487 /*--.. Operations on Users .................................................--*/
488 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
489 return wrap(unwrap<User>(Val)->getOperand(Index));
492 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
493 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
496 int LLVMGetNumOperands(LLVMValueRef Val) {
497 return unwrap<User>(Val)->getNumOperands();
500 /*--.. Operations on constants of any type .................................--*/
502 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
503 return wrap(Constant::getNullValue(unwrap(Ty)));
506 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
507 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
510 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
511 return wrap(UndefValue::get(unwrap(Ty)));
514 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
515 return isa<Constant>(unwrap(Ty));
518 LLVMBool LLVMIsNull(LLVMValueRef Val) {
519 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
520 return C->isNullValue();
524 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
525 return isa<UndefValue>(unwrap(Val));
528 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
530 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
533 /*--.. Operations on metadata nodes ........................................--*/
535 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
537 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
540 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
541 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
544 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
546 return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
549 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
550 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
553 /*--.. Operations on scalar constants ......................................--*/
555 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
556 LLVMBool SignExtend) {
557 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
560 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
562 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
566 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
567 unsigned SLen, uint8_t Radix) {
568 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
572 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
573 return wrap(ConstantFP::get(unwrap(RealTy), N));
576 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
577 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
580 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
582 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
585 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
586 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
589 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
590 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
593 /*--.. Operations on composite constants ...................................--*/
595 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
597 LLVMBool DontNullTerminate) {
598 /* Inverted the sense of AddNull because ', 0)' is a
599 better mnemonic for null termination than ', 1)'. */
600 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
601 DontNullTerminate == 0));
603 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
604 LLVMValueRef *ConstantVals,
605 unsigned Count, LLVMBool Packed) {
606 return wrap(ConstantStruct::get(*unwrap(C),
607 unwrap<Constant>(ConstantVals, Count),
608 Count, Packed != 0));
611 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
612 LLVMBool DontNullTerminate) {
613 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
616 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
617 LLVMValueRef *ConstantVals, unsigned Length) {
618 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
619 unwrap<Constant>(ConstantVals, Length),
622 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
624 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
627 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
628 return wrap(ConstantVector::get(
629 unwrap<Constant>(ScalarConstantVals, Size), Size));
631 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
632 return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
635 /*--.. Constant expressions ................................................--*/
637 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
638 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
641 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
642 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
645 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
646 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
649 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
650 return wrap(ConstantExpr::getNeg(
651 unwrap<Constant>(ConstantVal)));
654 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
655 return wrap(ConstantExpr::getNSWNeg(
656 unwrap<Constant>(ConstantVal)));
659 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
660 return wrap(ConstantExpr::getNUWNeg(
661 unwrap<Constant>(ConstantVal)));
665 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
666 return wrap(ConstantExpr::getFNeg(
667 unwrap<Constant>(ConstantVal)));
670 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
671 return wrap(ConstantExpr::getNot(
672 unwrap<Constant>(ConstantVal)));
675 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
676 return wrap(ConstantExpr::getAdd(
677 unwrap<Constant>(LHSConstant),
678 unwrap<Constant>(RHSConstant)));
681 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
682 LLVMValueRef RHSConstant) {
683 return wrap(ConstantExpr::getNSWAdd(
684 unwrap<Constant>(LHSConstant),
685 unwrap<Constant>(RHSConstant)));
688 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
689 LLVMValueRef RHSConstant) {
690 return wrap(ConstantExpr::getNUWAdd(
691 unwrap<Constant>(LHSConstant),
692 unwrap<Constant>(RHSConstant)));
695 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
696 return wrap(ConstantExpr::getFAdd(
697 unwrap<Constant>(LHSConstant),
698 unwrap<Constant>(RHSConstant)));
701 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
702 return wrap(ConstantExpr::getSub(
703 unwrap<Constant>(LHSConstant),
704 unwrap<Constant>(RHSConstant)));
707 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
708 LLVMValueRef RHSConstant) {
709 return wrap(ConstantExpr::getNSWSub(
710 unwrap<Constant>(LHSConstant),
711 unwrap<Constant>(RHSConstant)));
714 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
715 LLVMValueRef RHSConstant) {
716 return wrap(ConstantExpr::getNUWSub(
717 unwrap<Constant>(LHSConstant),
718 unwrap<Constant>(RHSConstant)));
721 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
722 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
723 unwrap<Constant>(RHSConstant)));
726 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727 return wrap(ConstantExpr::getMul(
728 unwrap<Constant>(LHSConstant),
729 unwrap<Constant>(RHSConstant)));
732 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
733 LLVMValueRef RHSConstant) {
734 return wrap(ConstantExpr::getNSWMul(
735 unwrap<Constant>(LHSConstant),
736 unwrap<Constant>(RHSConstant)));
739 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
740 LLVMValueRef RHSConstant) {
741 return wrap(ConstantExpr::getNUWMul(
742 unwrap<Constant>(LHSConstant),
743 unwrap<Constant>(RHSConstant)));
746 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
747 return wrap(ConstantExpr::getFMul(
748 unwrap<Constant>(LHSConstant),
749 unwrap<Constant>(RHSConstant)));
752 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
753 return wrap(ConstantExpr::getUDiv(
754 unwrap<Constant>(LHSConstant),
755 unwrap<Constant>(RHSConstant)));
758 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
759 return wrap(ConstantExpr::getSDiv(
760 unwrap<Constant>(LHSConstant),
761 unwrap<Constant>(RHSConstant)));
764 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
765 LLVMValueRef RHSConstant) {
766 return wrap(ConstantExpr::getExactSDiv(
767 unwrap<Constant>(LHSConstant),
768 unwrap<Constant>(RHSConstant)));
771 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
772 return wrap(ConstantExpr::getFDiv(
773 unwrap<Constant>(LHSConstant),
774 unwrap<Constant>(RHSConstant)));
777 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
778 return wrap(ConstantExpr::getURem(
779 unwrap<Constant>(LHSConstant),
780 unwrap<Constant>(RHSConstant)));
783 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
784 return wrap(ConstantExpr::getSRem(
785 unwrap<Constant>(LHSConstant),
786 unwrap<Constant>(RHSConstant)));
789 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
790 return wrap(ConstantExpr::getFRem(
791 unwrap<Constant>(LHSConstant),
792 unwrap<Constant>(RHSConstant)));
795 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
796 return wrap(ConstantExpr::getAnd(
797 unwrap<Constant>(LHSConstant),
798 unwrap<Constant>(RHSConstant)));
801 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
802 return wrap(ConstantExpr::getOr(
803 unwrap<Constant>(LHSConstant),
804 unwrap<Constant>(RHSConstant)));
807 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
808 return wrap(ConstantExpr::getXor(
809 unwrap<Constant>(LHSConstant),
810 unwrap<Constant>(RHSConstant)));
813 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
814 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
815 return wrap(ConstantExpr::getICmp(Predicate,
816 unwrap<Constant>(LHSConstant),
817 unwrap<Constant>(RHSConstant)));
820 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
821 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
822 return wrap(ConstantExpr::getFCmp(Predicate,
823 unwrap<Constant>(LHSConstant),
824 unwrap<Constant>(RHSConstant)));
827 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
828 return wrap(ConstantExpr::getShl(
829 unwrap<Constant>(LHSConstant),
830 unwrap<Constant>(RHSConstant)));
833 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
834 return wrap(ConstantExpr::getLShr(
835 unwrap<Constant>(LHSConstant),
836 unwrap<Constant>(RHSConstant)));
839 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
840 return wrap(ConstantExpr::getAShr(
841 unwrap<Constant>(LHSConstant),
842 unwrap<Constant>(RHSConstant)));
845 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
846 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
847 return wrap(ConstantExpr::getGetElementPtr(
848 unwrap<Constant>(ConstantVal),
849 unwrap<Constant>(ConstantIndices,
854 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
855 LLVMValueRef *ConstantIndices,
856 unsigned NumIndices) {
857 Constant* Val = unwrap<Constant>(ConstantVal);
858 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
859 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
862 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
863 return wrap(ConstantExpr::getTrunc(
864 unwrap<Constant>(ConstantVal),
868 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
869 return wrap(ConstantExpr::getSExt(
870 unwrap<Constant>(ConstantVal),
874 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
875 return wrap(ConstantExpr::getZExt(
876 unwrap<Constant>(ConstantVal),
880 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
881 return wrap(ConstantExpr::getFPTrunc(
882 unwrap<Constant>(ConstantVal),
886 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
887 return wrap(ConstantExpr::getFPExtend(
888 unwrap<Constant>(ConstantVal),
892 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
893 return wrap(ConstantExpr::getUIToFP(
894 unwrap<Constant>(ConstantVal),
898 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
899 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
903 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
904 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
908 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
909 return wrap(ConstantExpr::getFPToSI(
910 unwrap<Constant>(ConstantVal),
914 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
915 return wrap(ConstantExpr::getPtrToInt(
916 unwrap<Constant>(ConstantVal),
920 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
921 return wrap(ConstantExpr::getIntToPtr(
922 unwrap<Constant>(ConstantVal),
926 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
927 return wrap(ConstantExpr::getBitCast(
928 unwrap<Constant>(ConstantVal),
932 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
933 LLVMTypeRef ToType) {
934 return wrap(ConstantExpr::getZExtOrBitCast(
935 unwrap<Constant>(ConstantVal),
939 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
940 LLVMTypeRef ToType) {
941 return wrap(ConstantExpr::getSExtOrBitCast(
942 unwrap<Constant>(ConstantVal),
946 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
947 LLVMTypeRef ToType) {
948 return wrap(ConstantExpr::getTruncOrBitCast(
949 unwrap<Constant>(ConstantVal),
953 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
954 LLVMTypeRef ToType) {
955 return wrap(ConstantExpr::getPointerCast(
956 unwrap<Constant>(ConstantVal),
960 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
962 return wrap(ConstantExpr::getIntegerCast(
963 unwrap<Constant>(ConstantVal),
968 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
969 return wrap(ConstantExpr::getFPCast(
970 unwrap<Constant>(ConstantVal),
974 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
975 LLVMValueRef ConstantIfTrue,
976 LLVMValueRef ConstantIfFalse) {
977 return wrap(ConstantExpr::getSelect(
978 unwrap<Constant>(ConstantCondition),
979 unwrap<Constant>(ConstantIfTrue),
980 unwrap<Constant>(ConstantIfFalse)));
983 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
984 LLVMValueRef IndexConstant) {
985 return wrap(ConstantExpr::getExtractElement(
986 unwrap<Constant>(VectorConstant),
987 unwrap<Constant>(IndexConstant)));
990 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
991 LLVMValueRef ElementValueConstant,
992 LLVMValueRef IndexConstant) {
993 return wrap(ConstantExpr::getInsertElement(
994 unwrap<Constant>(VectorConstant),
995 unwrap<Constant>(ElementValueConstant),
996 unwrap<Constant>(IndexConstant)));
999 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1000 LLVMValueRef VectorBConstant,
1001 LLVMValueRef MaskConstant) {
1002 return wrap(ConstantExpr::getShuffleVector(
1003 unwrap<Constant>(VectorAConstant),
1004 unwrap<Constant>(VectorBConstant),
1005 unwrap<Constant>(MaskConstant)));
1008 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1010 return wrap(ConstantExpr::getExtractValue(
1011 unwrap<Constant>(AggConstant),
1015 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1016 LLVMValueRef ElementValueConstant,
1017 unsigned *IdxList, unsigned NumIdx) {
1018 return wrap(ConstantExpr::getInsertValue(
1019 unwrap<Constant>(AggConstant),
1020 unwrap<Constant>(ElementValueConstant),
1024 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1025 const char *Constraints,
1026 LLVMBool HasSideEffects,
1027 LLVMBool IsAlignStack) {
1028 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1029 Constraints, HasSideEffects, IsAlignStack));
1032 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1033 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1036 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1038 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1039 return wrap(unwrap<GlobalValue>(Global)->getParent());
1042 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1043 return unwrap<GlobalValue>(Global)->isDeclaration();
1046 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1047 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1049 assert(false && "Unhandled Linkage Type.");
1050 case GlobalValue::ExternalLinkage:
1051 return LLVMExternalLinkage;
1052 case GlobalValue::AvailableExternallyLinkage:
1053 return LLVMAvailableExternallyLinkage;
1054 case GlobalValue::LinkOnceAnyLinkage:
1055 return LLVMLinkOnceAnyLinkage;
1056 case GlobalValue::LinkOnceODRLinkage:
1057 return LLVMLinkOnceODRLinkage;
1058 case GlobalValue::WeakAnyLinkage:
1059 return LLVMWeakAnyLinkage;
1060 case GlobalValue::WeakODRLinkage:
1061 return LLVMWeakODRLinkage;
1062 case GlobalValue::AppendingLinkage:
1063 return LLVMAppendingLinkage;
1064 case GlobalValue::InternalLinkage:
1065 return LLVMInternalLinkage;
1066 case GlobalValue::PrivateLinkage:
1067 return LLVMPrivateLinkage;
1068 case GlobalValue::LinkerPrivateLinkage:
1069 return LLVMLinkerPrivateLinkage;
1070 case GlobalValue::LinkerPrivateWeakLinkage:
1071 return LLVMLinkerPrivateWeakLinkage;
1072 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1073 return LLVMLinkerPrivateWeakDefAutoLinkage;
1074 case GlobalValue::DLLImportLinkage:
1075 return LLVMDLLImportLinkage;
1076 case GlobalValue::DLLExportLinkage:
1077 return LLVMDLLExportLinkage;
1078 case GlobalValue::ExternalWeakLinkage:
1079 return LLVMExternalWeakLinkage;
1080 case GlobalValue::CommonLinkage:
1081 return LLVMCommonLinkage;
1084 // Should never get here.
1085 return static_cast<LLVMLinkage>(0);
1088 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1089 GlobalValue *GV = unwrap<GlobalValue>(Global);
1093 assert(false && "Unhandled Linkage Type.");
1094 case LLVMExternalLinkage:
1095 GV->setLinkage(GlobalValue::ExternalLinkage);
1097 case LLVMAvailableExternallyLinkage:
1098 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1100 case LLVMLinkOnceAnyLinkage:
1101 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1103 case LLVMLinkOnceODRLinkage:
1104 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1106 case LLVMWeakAnyLinkage:
1107 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1109 case LLVMWeakODRLinkage:
1110 GV->setLinkage(GlobalValue::WeakODRLinkage);
1112 case LLVMAppendingLinkage:
1113 GV->setLinkage(GlobalValue::AppendingLinkage);
1115 case LLVMInternalLinkage:
1116 GV->setLinkage(GlobalValue::InternalLinkage);
1118 case LLVMPrivateLinkage:
1119 GV->setLinkage(GlobalValue::PrivateLinkage);
1121 case LLVMLinkerPrivateLinkage:
1122 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1124 case LLVMLinkerPrivateWeakLinkage:
1125 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1127 case LLVMLinkerPrivateWeakDefAutoLinkage:
1128 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1130 case LLVMDLLImportLinkage:
1131 GV->setLinkage(GlobalValue::DLLImportLinkage);
1133 case LLVMDLLExportLinkage:
1134 GV->setLinkage(GlobalValue::DLLExportLinkage);
1136 case LLVMExternalWeakLinkage:
1137 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1139 case LLVMGhostLinkage:
1141 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1143 case LLVMCommonLinkage:
1144 GV->setLinkage(GlobalValue::CommonLinkage);
1149 const char *LLVMGetSection(LLVMValueRef Global) {
1150 return unwrap<GlobalValue>(Global)->getSection().c_str();
1153 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1154 unwrap<GlobalValue>(Global)->setSection(Section);
1157 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1158 return static_cast<LLVMVisibility>(
1159 unwrap<GlobalValue>(Global)->getVisibility());
1162 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1163 unwrap<GlobalValue>(Global)
1164 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1167 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1168 return unwrap<GlobalValue>(Global)->getAlignment();
1171 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1172 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1175 /*--.. Operations on global variables ......................................--*/
1177 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1178 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1179 GlobalValue::ExternalLinkage, 0, Name));
1182 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1184 unsigned AddressSpace) {
1185 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1186 GlobalValue::ExternalLinkage, 0, Name, 0,
1187 false, AddressSpace));
1190 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1191 return wrap(unwrap(M)->getNamedGlobal(Name));
1194 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1195 Module *Mod = unwrap(M);
1196 Module::global_iterator I = Mod->global_begin();
1197 if (I == Mod->global_end())
1202 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1203 Module *Mod = unwrap(M);
1204 Module::global_iterator I = Mod->global_end();
1205 if (I == Mod->global_begin())
1210 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1211 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1212 Module::global_iterator I = GV;
1213 if (++I == GV->getParent()->global_end())
1218 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1219 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1220 Module::global_iterator I = GV;
1221 if (I == GV->getParent()->global_begin())
1226 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1227 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1230 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1231 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1232 if ( !GV->hasInitializer() )
1234 return wrap(GV->getInitializer());
1237 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1238 unwrap<GlobalVariable>(GlobalVar)
1239 ->setInitializer(unwrap<Constant>(ConstantVal));
1242 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1243 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1246 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1247 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1250 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1251 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1254 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1255 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1258 /*--.. Operations on aliases ......................................--*/
1260 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1262 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1263 unwrap<Constant>(Aliasee), unwrap (M)));
1266 /*--.. Operations on functions .............................................--*/
1268 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1269 LLVMTypeRef FunctionTy) {
1270 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1271 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1274 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1275 return wrap(unwrap(M)->getFunction(Name));
1278 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1279 Module *Mod = unwrap(M);
1280 Module::iterator I = Mod->begin();
1281 if (I == Mod->end())
1286 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1287 Module *Mod = unwrap(M);
1288 Module::iterator I = Mod->end();
1289 if (I == Mod->begin())
1294 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1295 Function *Func = unwrap<Function>(Fn);
1296 Module::iterator I = Func;
1297 if (++I == Func->getParent()->end())
1302 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1303 Function *Func = unwrap<Function>(Fn);
1304 Module::iterator I = Func;
1305 if (I == Func->getParent()->begin())
1310 void LLVMDeleteFunction(LLVMValueRef Fn) {
1311 unwrap<Function>(Fn)->eraseFromParent();
1314 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1315 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1316 return F->getIntrinsicID();
1320 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1321 return unwrap<Function>(Fn)->getCallingConv();
1324 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1325 return unwrap<Function>(Fn)->setCallingConv(
1326 static_cast<CallingConv::ID>(CC));
1329 const char *LLVMGetGC(LLVMValueRef Fn) {
1330 Function *F = unwrap<Function>(Fn);
1331 return F->hasGC()? F->getGC() : 0;
1334 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1335 Function *F = unwrap<Function>(Fn);
1342 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1343 Function *Func = unwrap<Function>(Fn);
1344 const AttrListPtr PAL = Func->getAttributes();
1345 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1346 Func->setAttributes(PALnew);
1349 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1350 Function *Func = unwrap<Function>(Fn);
1351 const AttrListPtr PAL = Func->getAttributes();
1352 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1353 Func->setAttributes(PALnew);
1356 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1357 Function *Func = unwrap<Function>(Fn);
1358 const AttrListPtr PAL = Func->getAttributes();
1359 Attributes attr = PAL.getFnAttributes();
1360 return (LLVMAttribute)attr;
1363 /*--.. Operations on parameters ............................................--*/
1365 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1366 // This function is strictly redundant to
1367 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1368 return unwrap<Function>(FnRef)->arg_size();
1371 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1372 Function *Fn = unwrap<Function>(FnRef);
1373 for (Function::arg_iterator I = Fn->arg_begin(),
1374 E = Fn->arg_end(); I != E; I++)
1375 *ParamRefs++ = wrap(I);
1378 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1379 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1385 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1386 return wrap(unwrap<Argument>(V)->getParent());
1389 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1390 Function *Func = unwrap<Function>(Fn);
1391 Function::arg_iterator I = Func->arg_begin();
1392 if (I == Func->arg_end())
1397 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1398 Function *Func = unwrap<Function>(Fn);
1399 Function::arg_iterator I = Func->arg_end();
1400 if (I == Func->arg_begin())
1405 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1406 Argument *A = unwrap<Argument>(Arg);
1407 Function::arg_iterator I = A;
1408 if (++I == A->getParent()->arg_end())
1413 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1414 Argument *A = unwrap<Argument>(Arg);
1415 Function::arg_iterator I = A;
1416 if (I == A->getParent()->arg_begin())
1421 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1422 unwrap<Argument>(Arg)->addAttr(PA);
1425 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1426 unwrap<Argument>(Arg)->removeAttr(PA);
1429 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1430 Argument *A = unwrap<Argument>(Arg);
1431 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1433 return (LLVMAttribute)attr;
1437 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1438 unwrap<Argument>(Arg)->addAttr(
1439 Attribute::constructAlignmentFromInt(align));
1442 /*--.. Operations on basic blocks ..........................................--*/
1444 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1445 return wrap(static_cast<Value*>(unwrap(BB)));
1448 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1449 return isa<BasicBlock>(unwrap(Val));
1452 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1453 return wrap(unwrap<BasicBlock>(Val));
1456 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1457 return wrap(unwrap(BB)->getParent());
1460 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1461 return unwrap<Function>(FnRef)->size();
1464 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1465 Function *Fn = unwrap<Function>(FnRef);
1466 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1467 *BasicBlocksRefs++ = wrap(I);
1470 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1471 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1474 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1475 Function *Func = unwrap<Function>(Fn);
1476 Function::iterator I = Func->begin();
1477 if (I == Func->end())
1482 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1483 Function *Func = unwrap<Function>(Fn);
1484 Function::iterator I = Func->end();
1485 if (I == Func->begin())
1490 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1491 BasicBlock *Block = unwrap(BB);
1492 Function::iterator I = Block;
1493 if (++I == Block->getParent()->end())
1498 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1499 BasicBlock *Block = unwrap(BB);
1500 Function::iterator I = Block;
1501 if (I == Block->getParent()->begin())
1506 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1509 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1512 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1513 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1516 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1517 LLVMBasicBlockRef BBRef,
1519 BasicBlock *BB = unwrap(BBRef);
1520 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1523 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1525 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1528 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1529 unwrap(BBRef)->eraseFromParent();
1532 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1533 unwrap(BB)->moveBefore(unwrap(MovePos));
1536 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1537 unwrap(BB)->moveAfter(unwrap(MovePos));
1540 /*--.. Operations on instructions ..........................................--*/
1542 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1543 return wrap(unwrap<Instruction>(Inst)->getParent());
1546 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1547 BasicBlock *Block = unwrap(BB);
1548 BasicBlock::iterator I = Block->begin();
1549 if (I == Block->end())
1554 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1555 BasicBlock *Block = unwrap(BB);
1556 BasicBlock::iterator I = Block->end();
1557 if (I == Block->begin())
1562 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1563 Instruction *Instr = unwrap<Instruction>(Inst);
1564 BasicBlock::iterator I = Instr;
1565 if (++I == Instr->getParent()->end())
1570 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1571 Instruction *Instr = unwrap<Instruction>(Inst);
1572 BasicBlock::iterator I = Instr;
1573 if (I == Instr->getParent()->begin())
1578 /*--.. Call and invoke instructions ........................................--*/
1580 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1581 Value *V = unwrap(Instr);
1582 if (CallInst *CI = dyn_cast<CallInst>(V))
1583 return CI->getCallingConv();
1584 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1585 return II->getCallingConv();
1586 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1590 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1591 Value *V = unwrap(Instr);
1592 if (CallInst *CI = dyn_cast<CallInst>(V))
1593 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1594 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1595 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1596 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1599 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1601 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1603 Call.getAttributes().addAttr(index, PA));
1606 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1608 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1610 Call.getAttributes().removeAttr(index, PA));
1613 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1615 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1617 Call.getAttributes().addAttr(index,
1618 Attribute::constructAlignmentFromInt(align)));
1621 /*--.. Operations on call instructions (only) ..............................--*/
1623 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1624 return unwrap<CallInst>(Call)->isTailCall();
1627 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1628 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1631 /*--.. Operations on phi nodes .............................................--*/
1633 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1634 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1635 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1636 for (unsigned I = 0; I != Count; ++I)
1637 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1640 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1641 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1644 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1645 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1648 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1649 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1653 /*===-- Instruction builders ----------------------------------------------===*/
1655 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1656 return wrap(new IRBuilder<>(*unwrap(C)));
1659 LLVMBuilderRef LLVMCreateBuilder(void) {
1660 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1663 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1664 LLVMValueRef Instr) {
1665 BasicBlock *BB = unwrap(Block);
1666 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1667 unwrap(Builder)->SetInsertPoint(BB, I);
1670 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1671 Instruction *I = unwrap<Instruction>(Instr);
1672 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1675 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1676 BasicBlock *BB = unwrap(Block);
1677 unwrap(Builder)->SetInsertPoint(BB);
1680 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1681 return wrap(unwrap(Builder)->GetInsertBlock());
1684 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1685 unwrap(Builder)->ClearInsertionPoint();
1688 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1689 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1692 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1694 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1697 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1698 delete unwrap(Builder);
1701 /*--.. Metadata builders ...................................................--*/
1703 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1704 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1705 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1708 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1709 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1710 .getAsMDNode(unwrap(Builder)->getContext()));
1713 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1714 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1718 /*--.. Instruction builders ................................................--*/
1720 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1721 return wrap(unwrap(B)->CreateRetVoid());
1724 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1725 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1728 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1730 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1733 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1734 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1737 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1738 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1739 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1742 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1743 LLVMBasicBlockRef Else, unsigned NumCases) {
1744 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1747 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1748 unsigned NumDests) {
1749 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1752 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1753 LLVMValueRef *Args, unsigned NumArgs,
1754 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1756 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1757 unwrap(Args), unwrap(Args) + NumArgs,
1761 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1762 return wrap(unwrap(B)->CreateUnwind());
1765 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1766 return wrap(unwrap(B)->CreateUnreachable());
1769 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1770 LLVMBasicBlockRef Dest) {
1771 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1774 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1775 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1778 /*--.. Arithmetic ..........................................................--*/
1780 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1782 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1785 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1787 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1790 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1792 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1795 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1797 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1800 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1802 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1805 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1807 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1810 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1812 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1815 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1817 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1820 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1822 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1825 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1827 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1830 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1832 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1835 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1837 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1840 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1842 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1845 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1847 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1850 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1851 LLVMValueRef RHS, const char *Name) {
1852 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1855 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1857 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1860 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1862 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1865 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1867 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1870 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1872 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1875 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1877 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1880 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1882 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1885 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1887 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1890 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1892 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1895 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1897 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1900 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1902 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1905 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1906 LLVMValueRef LHS, LLVMValueRef RHS,
1908 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1909 unwrap(RHS), Name));
1912 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1913 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1916 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1918 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1921 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1923 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1926 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1927 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1930 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1931 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1934 /*--.. Memory ..............................................................--*/
1936 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1938 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1939 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1940 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1941 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1942 ITy, unwrap(Ty), AllocSize,
1944 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1947 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1948 LLVMValueRef Val, const char *Name) {
1949 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1950 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1951 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1952 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1953 ITy, unwrap(Ty), AllocSize,
1954 unwrap(Val), 0, "");
1955 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1958 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1960 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1963 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1964 LLVMValueRef Val, const char *Name) {
1965 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1968 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1969 return wrap(unwrap(B)->Insert(
1970 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1974 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1976 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1979 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1980 LLVMValueRef PointerVal) {
1981 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1984 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1985 LLVMValueRef *Indices, unsigned NumIndices,
1987 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1988 unwrap(Indices) + NumIndices, Name));
1991 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1992 LLVMValueRef *Indices, unsigned NumIndices,
1994 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1995 unwrap(Indices) + NumIndices, Name));
1998 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1999 unsigned Idx, const char *Name) {
2000 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2003 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2005 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2008 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2010 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2013 /*--.. Casts ...............................................................--*/
2015 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2016 LLVMTypeRef DestTy, const char *Name) {
2017 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2020 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2021 LLVMTypeRef DestTy, const char *Name) {
2022 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2025 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2026 LLVMTypeRef DestTy, const char *Name) {
2027 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2030 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2031 LLVMTypeRef DestTy, const char *Name) {
2032 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2035 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2036 LLVMTypeRef DestTy, const char *Name) {
2037 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2040 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2041 LLVMTypeRef DestTy, const char *Name) {
2042 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2045 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2046 LLVMTypeRef DestTy, const char *Name) {
2047 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2050 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2051 LLVMTypeRef DestTy, const char *Name) {
2052 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2055 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2056 LLVMTypeRef DestTy, const char *Name) {
2057 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2060 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2061 LLVMTypeRef DestTy, const char *Name) {
2062 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2065 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2066 LLVMTypeRef DestTy, const char *Name) {
2067 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2070 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2071 LLVMTypeRef DestTy, const char *Name) {
2072 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2075 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2076 LLVMTypeRef DestTy, const char *Name) {
2077 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2081 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2082 LLVMTypeRef DestTy, const char *Name) {
2083 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2087 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2088 LLVMTypeRef DestTy, const char *Name) {
2089 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2093 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2094 LLVMTypeRef DestTy, const char *Name) {
2095 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2096 unwrap(DestTy), Name));
2099 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2100 LLVMTypeRef DestTy, const char *Name) {
2101 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2104 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2105 LLVMTypeRef DestTy, const char *Name) {
2106 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2107 /*isSigned*/true, Name));
2110 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2111 LLVMTypeRef DestTy, const char *Name) {
2112 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2115 /*--.. Comparisons .........................................................--*/
2117 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2118 LLVMValueRef LHS, LLVMValueRef RHS,
2120 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2121 unwrap(LHS), unwrap(RHS), Name));
2124 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2125 LLVMValueRef LHS, LLVMValueRef RHS,
2127 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2128 unwrap(LHS), unwrap(RHS), Name));
2131 /*--.. Miscellaneous instructions ..........................................--*/
2133 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2134 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2137 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2138 LLVMValueRef *Args, unsigned NumArgs,
2140 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2141 unwrap(Args) + NumArgs, Name));
2144 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2145 LLVMValueRef Then, LLVMValueRef Else,
2147 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2151 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2152 LLVMTypeRef Ty, const char *Name) {
2153 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2156 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2157 LLVMValueRef Index, const char *Name) {
2158 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2162 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2163 LLVMValueRef EltVal, LLVMValueRef Index,
2165 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2166 unwrap(Index), Name));
2169 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2170 LLVMValueRef V2, LLVMValueRef Mask,
2172 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2173 unwrap(Mask), Name));
2176 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2177 unsigned Index, const char *Name) {
2178 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2181 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2182 LLVMValueRef EltVal, unsigned Index,
2184 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2188 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2190 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2193 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2195 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2198 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2199 LLVMValueRef RHS, const char *Name) {
2200 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2204 /*===-- Module providers --------------------------------------------------===*/
2206 LLVMModuleProviderRef
2207 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2208 return reinterpret_cast<LLVMModuleProviderRef>(M);
2211 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2216 /*===-- Memory buffers ----------------------------------------------------===*/
2218 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2220 LLVMMemoryBufferRef *OutMemBuf,
2221 char **OutMessage) {
2224 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2225 *OutMemBuf = wrap(MB);
2229 *OutMessage = strdup(Error.c_str());
2233 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2234 char **OutMessage) {
2236 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) {
2237 *OutMemBuf = wrap(MB);
2241 *OutMessage = strdup(Error.c_str());
2245 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2246 delete unwrap(MemBuf);
2250 /*===-- Pass Manager ------------------------------------------------------===*/
2252 LLVMPassManagerRef LLVMCreatePassManager() {
2253 return wrap(new PassManager());
2256 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2257 return wrap(new FunctionPassManager(unwrap(M)));
2260 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2261 return LLVMCreateFunctionPassManagerForModule(
2262 reinterpret_cast<LLVMModuleRef>(P));
2265 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2266 return unwrap<PassManager>(PM)->run(*unwrap(M));
2269 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2270 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2273 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2274 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2277 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2278 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2281 void LLVMDisposePassManager(LLVMPassManagerRef PM) {