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/Support/CallSite.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
37 /*===-- Error handling ----------------------------------------------------===*/
39 void LLVMDisposeMessage(char *Message) {
44 /*===-- Operations on contexts --------------------------------------------===*/
46 LLVMContextRef LLVMContextCreate() {
47 return wrap(new LLVMContext());
50 LLVMContextRef LLVMGetGlobalContext() {
51 return wrap(&getGlobalContext());
54 void LLVMContextDispose(LLVMContextRef C) {
59 /*===-- Operations on modules ---------------------------------------------===*/
61 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
62 return wrap(new Module(ModuleID, getGlobalContext()));
65 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
67 return wrap(new Module(ModuleID, *unwrap(C)));
70 void LLVMDisposeModule(LLVMModuleRef M) {
74 /*--.. Data layout .........................................................--*/
75 const char * LLVMGetDataLayout(LLVMModuleRef M) {
76 return unwrap(M)->getDataLayout().c_str();
79 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
80 unwrap(M)->setDataLayout(Triple);
83 /*--.. Target triple .......................................................--*/
84 const char * LLVMGetTarget(LLVMModuleRef M) {
85 return unwrap(M)->getTargetTriple().c_str();
88 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
89 unwrap(M)->setTargetTriple(Triple);
92 /*--.. Type names ..........................................................--*/
93 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
94 return unwrap(M)->addTypeName(Name, unwrap(Ty));
97 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
100 TypeSymbolTable::iterator I = TST.find(Name);
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106 return wrap(unwrap(M)->getTypeByName(Name));
109 void LLVMDumpModule(LLVMModuleRef M) {
114 /*===-- Operations on types -----------------------------------------------===*/
116 /*--.. Operations on all types (mostly) ....................................--*/
118 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
119 switch (unwrap(Ty)->getTypeID()) {
121 assert(false && "Unhandled TypeID.");
123 return LLVMVoidTypeKind;
124 case Type::FloatTyID:
125 return LLVMFloatTypeKind;
126 case Type::DoubleTyID:
127 return LLVMDoubleTypeKind;
128 case Type::X86_FP80TyID:
129 return LLVMX86_FP80TypeKind;
130 case Type::FP128TyID:
131 return LLVMFP128TypeKind;
132 case Type::PPC_FP128TyID:
133 return LLVMPPC_FP128TypeKind;
134 case Type::LabelTyID:
135 return LLVMLabelTypeKind;
136 case Type::MetadataTyID:
137 return LLVMMetadataTypeKind;
138 case Type::IntegerTyID:
139 return LLVMIntegerTypeKind;
140 case Type::FunctionTyID:
141 return LLVMFunctionTypeKind;
142 case Type::StructTyID:
143 return LLVMStructTypeKind;
144 case Type::UnionTyID:
145 return LLVMUnionTypeKind;
146 case Type::ArrayTyID:
147 return LLVMArrayTypeKind;
148 case Type::PointerTyID:
149 return LLVMPointerTypeKind;
150 case Type::OpaqueTyID:
151 return LLVMOpaqueTypeKind;
152 case Type::VectorTyID:
153 return LLVMVectorTypeKind;
157 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
158 return wrap(&unwrap(Ty)->getContext());
161 /*--.. Operations on integer types .........................................--*/
163 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
164 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
166 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
167 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
169 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
170 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
172 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
173 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
175 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
176 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
178 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
179 return wrap(IntegerType::get(*unwrap(C), NumBits));
182 LLVMTypeRef LLVMInt1Type(void) {
183 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
185 LLVMTypeRef LLVMInt8Type(void) {
186 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
188 LLVMTypeRef LLVMInt16Type(void) {
189 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
191 LLVMTypeRef LLVMInt32Type(void) {
192 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
194 LLVMTypeRef LLVMInt64Type(void) {
195 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
197 LLVMTypeRef LLVMIntType(unsigned NumBits) {
198 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
201 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
202 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
205 /*--.. Operations on real types ............................................--*/
207 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
208 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
210 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
211 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
213 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
214 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
216 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
217 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
219 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
220 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
223 LLVMTypeRef LLVMFloatType(void) {
224 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
226 LLVMTypeRef LLVMDoubleType(void) {
227 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
229 LLVMTypeRef LLVMX86FP80Type(void) {
230 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
232 LLVMTypeRef LLVMFP128Type(void) {
233 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
235 LLVMTypeRef LLVMPPCFP128Type(void) {
236 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
239 /*--.. Operations on function types ........................................--*/
241 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
242 LLVMTypeRef *ParamTypes, unsigned ParamCount,
244 std::vector<const Type*> Tys;
245 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
246 Tys.push_back(unwrap(*I));
248 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
251 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
252 return unwrap<FunctionType>(FunctionTy)->isVarArg();
255 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
256 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
259 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
260 return unwrap<FunctionType>(FunctionTy)->getNumParams();
263 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
264 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
265 for (FunctionType::param_iterator I = Ty->param_begin(),
266 E = Ty->param_end(); I != E; ++I)
270 /*--.. Operations on struct types ..........................................--*/
272 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
273 unsigned ElementCount, LLVMBool Packed) {
274 std::vector<const Type*> Tys;
275 for (LLVMTypeRef *I = ElementTypes,
276 *E = ElementTypes + ElementCount; I != E; ++I)
277 Tys.push_back(unwrap(*I));
279 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
282 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
283 unsigned ElementCount, LLVMBool Packed) {
284 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
285 ElementCount, Packed);
289 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
290 return unwrap<StructType>(StructTy)->getNumElements();
293 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
294 StructType *Ty = unwrap<StructType>(StructTy);
295 for (FunctionType::param_iterator I = Ty->element_begin(),
296 E = Ty->element_end(); I != E; ++I)
300 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
301 return unwrap<StructType>(StructTy)->isPacked();
304 /*--.. Operations on union types ..........................................--*/
306 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
307 unsigned ElementCount) {
308 SmallVector<const Type*, 8> Tys;
309 for (LLVMTypeRef *I = ElementTypes,
310 *E = ElementTypes + ElementCount; I != E; ++I)
311 Tys.push_back(unwrap(*I));
313 return wrap(UnionType::get(&Tys[0], Tys.size()));
316 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes,
317 unsigned ElementCount, int Packed) {
318 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
322 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
323 return unwrap<UnionType>(UnionTy)->getNumElements();
326 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
327 UnionType *Ty = unwrap<UnionType>(UnionTy);
328 for (FunctionType::param_iterator I = Ty->element_begin(),
329 E = Ty->element_end(); I != E; ++I)
333 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
335 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
336 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
339 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
340 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
343 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
344 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
347 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
348 return wrap(unwrap<SequentialType>(Ty)->getElementType());
351 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
352 return unwrap<ArrayType>(ArrayTy)->getNumElements();
355 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
356 return unwrap<PointerType>(PointerTy)->getAddressSpace();
359 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
360 return unwrap<VectorType>(VectorTy)->getNumElements();
363 /*--.. Operations on other types ...........................................--*/
365 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
366 return wrap(Type::getVoidTy(*unwrap(C)));
368 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
369 return wrap(Type::getLabelTy(*unwrap(C)));
371 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
372 return wrap(OpaqueType::get(*unwrap(C)));
375 LLVMTypeRef LLVMVoidType(void) {
376 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
378 LLVMTypeRef LLVMLabelType(void) {
379 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
381 LLVMTypeRef LLVMOpaqueType(void) {
382 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
385 /*--.. Operations on type handles ..........................................--*/
387 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
388 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
391 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
392 delete unwrap(TypeHandle);
395 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
396 return wrap(unwrap(TypeHandle)->get());
399 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
400 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
404 /*===-- Operations on values ----------------------------------------------===*/
406 /*--.. Operations on all values ............................................--*/
408 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
409 return wrap(unwrap(Val)->getType());
412 const char *LLVMGetValueName(LLVMValueRef Val) {
413 return unwrap(Val)->getName().data();
416 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
417 unwrap(Val)->setName(Name);
420 void LLVMDumpValue(LLVMValueRef Val) {
424 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
425 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
428 /*--.. Conversion functions ................................................--*/
430 #define LLVM_DEFINE_VALUE_CAST(name) \
431 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
432 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
435 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
437 /*--.. Operations on Uses ..................................................--*/
438 LLVMUseIteratorRef LLVMGetFirstUse(LLVMValueRef Val) {
439 Value *V = unwrap(Val);
440 Value::use_iterator I = V->use_begin();
441 if (I == V->use_end())
443 return wrap(&(I.getUse()));
446 LLVMUseIteratorRef LLVMGetNextUse(LLVMUseIteratorRef UR) {
447 return wrap(unwrap(UR)->getNext());
450 LLVMValueRef LLVMGetUser(LLVMUseIteratorRef UR) {
451 return wrap(unwrap(UR)->getUser());
454 LLVMValueRef LLVMGetUsedValue(LLVMUseIteratorRef UR) {
455 return wrap(unwrap(UR)->get());
458 /*--.. Operations on Users .................................................--*/
459 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
460 return wrap(unwrap<User>(Val)->getOperand(Index));
463 /*--.. Operations on constants of any type .................................--*/
465 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
466 return wrap(Constant::getNullValue(unwrap(Ty)));
469 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
470 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
473 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
474 return wrap(UndefValue::get(unwrap(Ty)));
477 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
478 return isa<Constant>(unwrap(Ty));
481 LLVMBool LLVMIsNull(LLVMValueRef Val) {
482 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
483 return C->isNullValue();
487 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
488 return isa<UndefValue>(unwrap(Val));
491 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
493 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
496 /*--.. Operations on scalar constants ......................................--*/
498 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
499 LLVMBool SignExtend) {
500 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
503 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
505 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
509 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
510 unsigned SLen, uint8_t Radix) {
511 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
515 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
516 return wrap(ConstantFP::get(unwrap(RealTy), N));
519 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
520 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
523 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
525 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
528 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
529 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
532 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
533 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
536 /*--.. Operations on composite constants ...................................--*/
538 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
540 LLVMBool DontNullTerminate) {
541 /* Inverted the sense of AddNull because ', 0)' is a
542 better mnemonic for null termination than ', 1)'. */
543 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
544 DontNullTerminate == 0));
546 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
547 LLVMValueRef *ConstantVals,
548 unsigned Count, LLVMBool Packed) {
549 return wrap(ConstantStruct::get(*unwrap(C),
550 unwrap<Constant>(ConstantVals, Count),
551 Count, Packed != 0));
554 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
555 LLVMBool DontNullTerminate) {
556 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
559 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
560 LLVMValueRef *ConstantVals, unsigned Length) {
561 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
562 unwrap<Constant>(ConstantVals, Length),
565 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
567 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
571 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
572 return wrap(ConstantVector::get(
573 unwrap<Constant>(ScalarConstantVals, Size), Size));
576 /*--.. Constant expressions ................................................--*/
578 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
579 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
582 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
583 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
586 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
587 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
590 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
591 return wrap(ConstantExpr::getNeg(
592 unwrap<Constant>(ConstantVal)));
595 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
596 return wrap(ConstantExpr::getFNeg(
597 unwrap<Constant>(ConstantVal)));
600 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
601 return wrap(ConstantExpr::getNot(
602 unwrap<Constant>(ConstantVal)));
605 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
606 return wrap(ConstantExpr::getAdd(
607 unwrap<Constant>(LHSConstant),
608 unwrap<Constant>(RHSConstant)));
611 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
612 LLVMValueRef RHSConstant) {
613 return wrap(ConstantExpr::getNSWAdd(
614 unwrap<Constant>(LHSConstant),
615 unwrap<Constant>(RHSConstant)));
618 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
619 return wrap(ConstantExpr::getFAdd(
620 unwrap<Constant>(LHSConstant),
621 unwrap<Constant>(RHSConstant)));
624 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
625 return wrap(ConstantExpr::getSub(
626 unwrap<Constant>(LHSConstant),
627 unwrap<Constant>(RHSConstant)));
630 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
631 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
632 unwrap<Constant>(RHSConstant)));
635 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
636 return wrap(ConstantExpr::getMul(
637 unwrap<Constant>(LHSConstant),
638 unwrap<Constant>(RHSConstant)));
641 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
642 return wrap(ConstantExpr::getFMul(
643 unwrap<Constant>(LHSConstant),
644 unwrap<Constant>(RHSConstant)));
647 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
648 return wrap(ConstantExpr::getUDiv(
649 unwrap<Constant>(LHSConstant),
650 unwrap<Constant>(RHSConstant)));
653 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
654 return wrap(ConstantExpr::getSDiv(
655 unwrap<Constant>(LHSConstant),
656 unwrap<Constant>(RHSConstant)));
659 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
660 LLVMValueRef RHSConstant) {
661 return wrap(ConstantExpr::getExactSDiv(
662 unwrap<Constant>(LHSConstant),
663 unwrap<Constant>(RHSConstant)));
666 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
667 return wrap(ConstantExpr::getFDiv(
668 unwrap<Constant>(LHSConstant),
669 unwrap<Constant>(RHSConstant)));
672 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
673 return wrap(ConstantExpr::getURem(
674 unwrap<Constant>(LHSConstant),
675 unwrap<Constant>(RHSConstant)));
678 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
679 return wrap(ConstantExpr::getSRem(
680 unwrap<Constant>(LHSConstant),
681 unwrap<Constant>(RHSConstant)));
684 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
685 return wrap(ConstantExpr::getFRem(
686 unwrap<Constant>(LHSConstant),
687 unwrap<Constant>(RHSConstant)));
690 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
691 return wrap(ConstantExpr::getAnd(
692 unwrap<Constant>(LHSConstant),
693 unwrap<Constant>(RHSConstant)));
696 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
697 return wrap(ConstantExpr::getOr(
698 unwrap<Constant>(LHSConstant),
699 unwrap<Constant>(RHSConstant)));
702 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
703 return wrap(ConstantExpr::getXor(
704 unwrap<Constant>(LHSConstant),
705 unwrap<Constant>(RHSConstant)));
708 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
709 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
710 return wrap(ConstantExpr::getICmp(Predicate,
711 unwrap<Constant>(LHSConstant),
712 unwrap<Constant>(RHSConstant)));
715 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
716 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
717 return wrap(ConstantExpr::getFCmp(Predicate,
718 unwrap<Constant>(LHSConstant),
719 unwrap<Constant>(RHSConstant)));
722 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
723 return wrap(ConstantExpr::getShl(
724 unwrap<Constant>(LHSConstant),
725 unwrap<Constant>(RHSConstant)));
728 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
729 return wrap(ConstantExpr::getLShr(
730 unwrap<Constant>(LHSConstant),
731 unwrap<Constant>(RHSConstant)));
734 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
735 return wrap(ConstantExpr::getAShr(
736 unwrap<Constant>(LHSConstant),
737 unwrap<Constant>(RHSConstant)));
740 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
741 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
742 return wrap(ConstantExpr::getGetElementPtr(
743 unwrap<Constant>(ConstantVal),
744 unwrap<Constant>(ConstantIndices,
749 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
750 LLVMValueRef *ConstantIndices,
751 unsigned NumIndices) {
752 Constant* Val = unwrap<Constant>(ConstantVal);
753 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
754 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
757 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
758 return wrap(ConstantExpr::getTrunc(
759 unwrap<Constant>(ConstantVal),
763 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
764 return wrap(ConstantExpr::getSExt(
765 unwrap<Constant>(ConstantVal),
769 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
770 return wrap(ConstantExpr::getZExt(
771 unwrap<Constant>(ConstantVal),
775 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
776 return wrap(ConstantExpr::getFPTrunc(
777 unwrap<Constant>(ConstantVal),
781 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
782 return wrap(ConstantExpr::getFPExtend(
783 unwrap<Constant>(ConstantVal),
787 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
788 return wrap(ConstantExpr::getUIToFP(
789 unwrap<Constant>(ConstantVal),
793 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
794 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
798 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
799 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
803 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
804 return wrap(ConstantExpr::getFPToSI(
805 unwrap<Constant>(ConstantVal),
809 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
810 return wrap(ConstantExpr::getPtrToInt(
811 unwrap<Constant>(ConstantVal),
815 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
816 return wrap(ConstantExpr::getIntToPtr(
817 unwrap<Constant>(ConstantVal),
821 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
822 return wrap(ConstantExpr::getBitCast(
823 unwrap<Constant>(ConstantVal),
827 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
828 LLVMTypeRef ToType) {
829 return wrap(ConstantExpr::getZExtOrBitCast(
830 unwrap<Constant>(ConstantVal),
834 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
835 LLVMTypeRef ToType) {
836 return wrap(ConstantExpr::getSExtOrBitCast(
837 unwrap<Constant>(ConstantVal),
841 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
842 LLVMTypeRef ToType) {
843 return wrap(ConstantExpr::getTruncOrBitCast(
844 unwrap<Constant>(ConstantVal),
848 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
849 LLVMTypeRef ToType) {
850 return wrap(ConstantExpr::getPointerCast(
851 unwrap<Constant>(ConstantVal),
855 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
857 return wrap(ConstantExpr::getIntegerCast(
858 unwrap<Constant>(ConstantVal),
863 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864 return wrap(ConstantExpr::getFPCast(
865 unwrap<Constant>(ConstantVal),
869 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
870 LLVMValueRef ConstantIfTrue,
871 LLVMValueRef ConstantIfFalse) {
872 return wrap(ConstantExpr::getSelect(
873 unwrap<Constant>(ConstantCondition),
874 unwrap<Constant>(ConstantIfTrue),
875 unwrap<Constant>(ConstantIfFalse)));
878 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
879 LLVMValueRef IndexConstant) {
880 return wrap(ConstantExpr::getExtractElement(
881 unwrap<Constant>(VectorConstant),
882 unwrap<Constant>(IndexConstant)));
885 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
886 LLVMValueRef ElementValueConstant,
887 LLVMValueRef IndexConstant) {
888 return wrap(ConstantExpr::getInsertElement(
889 unwrap<Constant>(VectorConstant),
890 unwrap<Constant>(ElementValueConstant),
891 unwrap<Constant>(IndexConstant)));
894 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
895 LLVMValueRef VectorBConstant,
896 LLVMValueRef MaskConstant) {
897 return wrap(ConstantExpr::getShuffleVector(
898 unwrap<Constant>(VectorAConstant),
899 unwrap<Constant>(VectorBConstant),
900 unwrap<Constant>(MaskConstant)));
903 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
905 return wrap(ConstantExpr::getExtractValue(
906 unwrap<Constant>(AggConstant),
910 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
911 LLVMValueRef ElementValueConstant,
912 unsigned *IdxList, unsigned NumIdx) {
913 return wrap(ConstantExpr::getInsertValue(
914 unwrap<Constant>(AggConstant),
915 unwrap<Constant>(ElementValueConstant),
919 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
920 const char *Constraints,
921 LLVMBool HasSideEffects,
922 LLVMBool IsAlignStack) {
923 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
924 Constraints, HasSideEffects, IsAlignStack));
927 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
929 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
930 return wrap(unwrap<GlobalValue>(Global)->getParent());
933 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
934 return unwrap<GlobalValue>(Global)->isDeclaration();
937 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
938 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
940 assert(false && "Unhandled Linkage Type.");
941 case GlobalValue::ExternalLinkage:
942 return LLVMExternalLinkage;
943 case GlobalValue::AvailableExternallyLinkage:
944 return LLVMAvailableExternallyLinkage;
945 case GlobalValue::LinkOnceAnyLinkage:
946 return LLVMLinkOnceAnyLinkage;
947 case GlobalValue::LinkOnceODRLinkage:
948 return LLVMLinkOnceODRLinkage;
949 case GlobalValue::WeakAnyLinkage:
950 return LLVMWeakAnyLinkage;
951 case GlobalValue::WeakODRLinkage:
952 return LLVMWeakODRLinkage;
953 case GlobalValue::AppendingLinkage:
954 return LLVMAppendingLinkage;
955 case GlobalValue::InternalLinkage:
956 return LLVMInternalLinkage;
957 case GlobalValue::PrivateLinkage:
958 return LLVMPrivateLinkage;
959 case GlobalValue::LinkerPrivateLinkage:
960 return LLVMLinkerPrivateLinkage;
961 case GlobalValue::DLLImportLinkage:
962 return LLVMDLLImportLinkage;
963 case GlobalValue::DLLExportLinkage:
964 return LLVMDLLExportLinkage;
965 case GlobalValue::ExternalWeakLinkage:
966 return LLVMExternalWeakLinkage;
967 case GlobalValue::CommonLinkage:
968 return LLVMCommonLinkage;
971 // Should never get here.
972 return static_cast<LLVMLinkage>(0);
975 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
976 GlobalValue *GV = unwrap<GlobalValue>(Global);
980 assert(false && "Unhandled Linkage Type.");
981 case LLVMExternalLinkage:
982 GV->setLinkage(GlobalValue::ExternalLinkage);
984 case LLVMAvailableExternallyLinkage:
985 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
987 case LLVMLinkOnceAnyLinkage:
988 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
990 case LLVMLinkOnceODRLinkage:
991 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
993 case LLVMWeakAnyLinkage:
994 GV->setLinkage(GlobalValue::WeakAnyLinkage);
996 case LLVMWeakODRLinkage:
997 GV->setLinkage(GlobalValue::WeakODRLinkage);
999 case LLVMAppendingLinkage:
1000 GV->setLinkage(GlobalValue::AppendingLinkage);
1002 case LLVMInternalLinkage:
1003 GV->setLinkage(GlobalValue::InternalLinkage);
1005 case LLVMPrivateLinkage:
1006 GV->setLinkage(GlobalValue::PrivateLinkage);
1008 case LLVMLinkerPrivateLinkage:
1009 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1011 case LLVMDLLImportLinkage:
1012 GV->setLinkage(GlobalValue::DLLImportLinkage);
1014 case LLVMDLLExportLinkage:
1015 GV->setLinkage(GlobalValue::DLLExportLinkage);
1017 case LLVMExternalWeakLinkage:
1018 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1020 case LLVMGhostLinkage:
1022 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1024 case LLVMCommonLinkage:
1025 GV->setLinkage(GlobalValue::CommonLinkage);
1030 const char *LLVMGetSection(LLVMValueRef Global) {
1031 return unwrap<GlobalValue>(Global)->getSection().c_str();
1034 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1035 unwrap<GlobalValue>(Global)->setSection(Section);
1038 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1039 return static_cast<LLVMVisibility>(
1040 unwrap<GlobalValue>(Global)->getVisibility());
1043 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1044 unwrap<GlobalValue>(Global)
1045 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1048 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1049 return unwrap<GlobalValue>(Global)->getAlignment();
1052 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1053 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1056 /*--.. Operations on global variables ......................................--*/
1058 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1059 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1060 GlobalValue::ExternalLinkage, 0, Name));
1063 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1064 return wrap(unwrap(M)->getNamedGlobal(Name));
1067 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1068 Module *Mod = unwrap(M);
1069 Module::global_iterator I = Mod->global_begin();
1070 if (I == Mod->global_end())
1075 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1076 Module *Mod = unwrap(M);
1077 Module::global_iterator I = Mod->global_end();
1078 if (I == Mod->global_begin())
1083 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1084 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1085 Module::global_iterator I = GV;
1086 if (++I == GV->getParent()->global_end())
1091 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1092 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1093 Module::global_iterator I = GV;
1094 if (I == GV->getParent()->global_begin())
1099 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1100 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1103 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1104 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1105 if ( !GV->hasInitializer() )
1107 return wrap(GV->getInitializer());
1110 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1111 unwrap<GlobalVariable>(GlobalVar)
1112 ->setInitializer(unwrap<Constant>(ConstantVal));
1115 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1116 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1119 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1120 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1123 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1124 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1127 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1128 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1131 /*--.. Operations on aliases ......................................--*/
1133 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1135 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1136 unwrap<Constant>(Aliasee), unwrap (M)));
1139 /*--.. Operations on functions .............................................--*/
1141 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1142 LLVMTypeRef FunctionTy) {
1143 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1144 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1147 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1148 return wrap(unwrap(M)->getFunction(Name));
1151 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1152 Module *Mod = unwrap(M);
1153 Module::iterator I = Mod->begin();
1154 if (I == Mod->end())
1159 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1160 Module *Mod = unwrap(M);
1161 Module::iterator I = Mod->end();
1162 if (I == Mod->begin())
1167 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1168 Function *Func = unwrap<Function>(Fn);
1169 Module::iterator I = Func;
1170 if (++I == Func->getParent()->end())
1175 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1176 Function *Func = unwrap<Function>(Fn);
1177 Module::iterator I = Func;
1178 if (I == Func->getParent()->begin())
1183 void LLVMDeleteFunction(LLVMValueRef Fn) {
1184 unwrap<Function>(Fn)->eraseFromParent();
1187 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1188 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1189 return F->getIntrinsicID();
1193 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1194 return unwrap<Function>(Fn)->getCallingConv();
1197 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1198 return unwrap<Function>(Fn)->setCallingConv(
1199 static_cast<CallingConv::ID>(CC));
1202 const char *LLVMGetGC(LLVMValueRef Fn) {
1203 Function *F = unwrap<Function>(Fn);
1204 return F->hasGC()? F->getGC() : 0;
1207 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1208 Function *F = unwrap<Function>(Fn);
1215 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1216 Function *Func = unwrap<Function>(Fn);
1217 const AttrListPtr PAL = Func->getAttributes();
1218 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1219 Func->setAttributes(PALnew);
1222 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1223 Function *Func = unwrap<Function>(Fn);
1224 const AttrListPtr PAL = Func->getAttributes();
1225 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1226 Func->setAttributes(PALnew);
1229 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1230 Function *Func = unwrap<Function>(Fn);
1231 const AttrListPtr PAL = Func->getAttributes();
1232 Attributes attr = PAL.getFnAttributes();
1233 return (LLVMAttribute)attr;
1236 /*--.. Operations on parameters ............................................--*/
1238 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1239 // This function is strictly redundant to
1240 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1241 return unwrap<Function>(FnRef)->arg_size();
1244 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1245 Function *Fn = unwrap<Function>(FnRef);
1246 for (Function::arg_iterator I = Fn->arg_begin(),
1247 E = Fn->arg_end(); I != E; I++)
1248 *ParamRefs++ = wrap(I);
1251 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1252 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1258 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1259 return wrap(unwrap<Argument>(V)->getParent());
1262 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1263 Function *Func = unwrap<Function>(Fn);
1264 Function::arg_iterator I = Func->arg_begin();
1265 if (I == Func->arg_end())
1270 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1271 Function *Func = unwrap<Function>(Fn);
1272 Function::arg_iterator I = Func->arg_end();
1273 if (I == Func->arg_begin())
1278 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1279 Argument *A = unwrap<Argument>(Arg);
1280 Function::arg_iterator I = A;
1281 if (++I == A->getParent()->arg_end())
1286 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1287 Argument *A = unwrap<Argument>(Arg);
1288 Function::arg_iterator I = A;
1289 if (I == A->getParent()->arg_begin())
1294 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1295 unwrap<Argument>(Arg)->addAttr(PA);
1298 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1299 unwrap<Argument>(Arg)->removeAttr(PA);
1302 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1303 Argument *A = unwrap<Argument>(Arg);
1304 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1306 return (LLVMAttribute)attr;
1310 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1311 unwrap<Argument>(Arg)->addAttr(
1312 Attribute::constructAlignmentFromInt(align));
1315 /*--.. Operations on basic blocks ..........................................--*/
1317 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1318 return wrap(static_cast<Value*>(unwrap(BB)));
1321 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1322 return isa<BasicBlock>(unwrap(Val));
1325 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1326 return wrap(unwrap<BasicBlock>(Val));
1329 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1330 return wrap(unwrap(BB)->getParent());
1333 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1334 return unwrap<Function>(FnRef)->size();
1337 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1338 Function *Fn = unwrap<Function>(FnRef);
1339 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1340 *BasicBlocksRefs++ = wrap(I);
1343 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1344 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1347 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1348 Function *Func = unwrap<Function>(Fn);
1349 Function::iterator I = Func->begin();
1350 if (I == Func->end())
1355 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1356 Function *Func = unwrap<Function>(Fn);
1357 Function::iterator I = Func->end();
1358 if (I == Func->begin())
1363 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1364 BasicBlock *Block = unwrap(BB);
1365 Function::iterator I = Block;
1366 if (++I == Block->getParent()->end())
1371 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1372 BasicBlock *Block = unwrap(BB);
1373 Function::iterator I = Block;
1374 if (I == Block->getParent()->begin())
1379 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1382 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1385 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1386 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1389 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1390 LLVMBasicBlockRef BBRef,
1392 BasicBlock *BB = unwrap(BBRef);
1393 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1396 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1398 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1401 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1402 unwrap(BBRef)->eraseFromParent();
1405 /*--.. Operations on instructions ..........................................--*/
1407 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1408 return wrap(unwrap<Instruction>(Inst)->getParent());
1411 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1412 BasicBlock *Block = unwrap(BB);
1413 BasicBlock::iterator I = Block->begin();
1414 if (I == Block->end())
1419 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1420 BasicBlock *Block = unwrap(BB);
1421 BasicBlock::iterator I = Block->end();
1422 if (I == Block->begin())
1427 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1428 Instruction *Instr = unwrap<Instruction>(Inst);
1429 BasicBlock::iterator I = Instr;
1430 if (++I == Instr->getParent()->end())
1435 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1436 Instruction *Instr = unwrap<Instruction>(Inst);
1437 BasicBlock::iterator I = Instr;
1438 if (I == Instr->getParent()->begin())
1443 /*--.. Call and invoke instructions ........................................--*/
1445 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1446 Value *V = unwrap(Instr);
1447 if (CallInst *CI = dyn_cast<CallInst>(V))
1448 return CI->getCallingConv();
1449 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1450 return II->getCallingConv();
1451 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1455 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1456 Value *V = unwrap(Instr);
1457 if (CallInst *CI = dyn_cast<CallInst>(V))
1458 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1459 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1460 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1461 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1464 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1466 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1468 Call.getAttributes().addAttr(index, PA));
1471 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1473 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1475 Call.getAttributes().removeAttr(index, PA));
1478 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1480 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1482 Call.getAttributes().addAttr(index,
1483 Attribute::constructAlignmentFromInt(align)));
1486 /*--.. Operations on call instructions (only) ..............................--*/
1488 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1489 return unwrap<CallInst>(Call)->isTailCall();
1492 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1493 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1496 /*--.. Operations on phi nodes .............................................--*/
1498 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1499 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1500 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1501 for (unsigned I = 0; I != Count; ++I)
1502 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1505 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1506 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1509 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1510 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1513 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1514 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1518 /*===-- Instruction builders ----------------------------------------------===*/
1520 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1521 return wrap(new IRBuilder<>(*unwrap(C)));
1524 LLVMBuilderRef LLVMCreateBuilder(void) {
1525 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1528 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1529 LLVMValueRef Instr) {
1530 BasicBlock *BB = unwrap(Block);
1531 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1532 unwrap(Builder)->SetInsertPoint(BB, I);
1535 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1536 Instruction *I = unwrap<Instruction>(Instr);
1537 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1540 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1541 BasicBlock *BB = unwrap(Block);
1542 unwrap(Builder)->SetInsertPoint(BB);
1545 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1546 return wrap(unwrap(Builder)->GetInsertBlock());
1549 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1550 unwrap(Builder)->ClearInsertionPoint ();
1553 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1554 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1557 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1559 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1562 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1563 delete unwrap(Builder);
1566 /*--.. Instruction builders ................................................--*/
1568 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1569 return wrap(unwrap(B)->CreateRetVoid());
1572 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1573 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1576 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1578 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1581 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1582 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1585 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1586 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1587 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1590 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1591 LLVMBasicBlockRef Else, unsigned NumCases) {
1592 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1595 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1596 LLVMValueRef *Args, unsigned NumArgs,
1597 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1599 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1600 unwrap(Args), unwrap(Args) + NumArgs,
1604 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1605 return wrap(unwrap(B)->CreateUnwind());
1608 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1609 return wrap(unwrap(B)->CreateUnreachable());
1612 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1613 LLVMBasicBlockRef Dest) {
1614 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1617 /*--.. Arithmetic ..........................................................--*/
1619 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1621 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1624 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1626 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1629 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1631 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1634 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1636 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1639 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1641 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1644 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1646 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1649 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1651 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1654 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1656 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1659 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1661 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1664 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1665 LLVMValueRef RHS, const char *Name) {
1666 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1669 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1671 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1674 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1676 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1679 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1681 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1684 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1686 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1689 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1691 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1694 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1696 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1699 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1701 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1704 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1706 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1709 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1711 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1714 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1716 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1719 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1720 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1723 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1724 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1727 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1728 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1731 /*--.. Memory ..............................................................--*/
1733 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1735 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1736 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1737 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1738 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1739 ITy, unwrap(Ty), AllocSize,
1741 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1744 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1745 LLVMValueRef Val, const char *Name) {
1746 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1747 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1748 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1749 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1750 ITy, unwrap(Ty), AllocSize,
1751 unwrap(Val), 0, "");
1752 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1755 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1757 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1760 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1761 LLVMValueRef Val, const char *Name) {
1762 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1765 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1766 return wrap(unwrap(B)->Insert(
1767 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1771 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1773 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1776 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1777 LLVMValueRef PointerVal) {
1778 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1781 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1782 LLVMValueRef *Indices, unsigned NumIndices,
1784 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1785 unwrap(Indices) + NumIndices, Name));
1788 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1789 LLVMValueRef *Indices, unsigned NumIndices,
1791 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1792 unwrap(Indices) + NumIndices, Name));
1795 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1796 unsigned Idx, const char *Name) {
1797 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1800 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1802 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1805 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1807 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1810 /*--.. Casts ...............................................................--*/
1812 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1813 LLVMTypeRef DestTy, const char *Name) {
1814 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1817 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1818 LLVMTypeRef DestTy, const char *Name) {
1819 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1822 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1823 LLVMTypeRef DestTy, const char *Name) {
1824 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1827 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1828 LLVMTypeRef DestTy, const char *Name) {
1829 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1832 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1833 LLVMTypeRef DestTy, const char *Name) {
1834 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1837 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1838 LLVMTypeRef DestTy, const char *Name) {
1839 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1842 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1843 LLVMTypeRef DestTy, const char *Name) {
1844 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1847 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1848 LLVMTypeRef DestTy, const char *Name) {
1849 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1852 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1853 LLVMTypeRef DestTy, const char *Name) {
1854 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1857 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1858 LLVMTypeRef DestTy, const char *Name) {
1859 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1862 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1863 LLVMTypeRef DestTy, const char *Name) {
1864 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1867 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1868 LLVMTypeRef DestTy, const char *Name) {
1869 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1872 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1873 LLVMTypeRef DestTy, const char *Name) {
1874 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1878 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1879 LLVMTypeRef DestTy, const char *Name) {
1880 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1884 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1885 LLVMTypeRef DestTy, const char *Name) {
1886 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1890 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1891 LLVMTypeRef DestTy, const char *Name) {
1892 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1895 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1896 LLVMTypeRef DestTy, const char *Name) {
1897 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
1898 /*isSigned*/true, Name));
1901 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1902 LLVMTypeRef DestTy, const char *Name) {
1903 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
1906 /*--.. Comparisons .........................................................--*/
1908 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1909 LLVMValueRef LHS, LLVMValueRef RHS,
1911 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1912 unwrap(LHS), unwrap(RHS), Name));
1915 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1916 LLVMValueRef LHS, LLVMValueRef RHS,
1918 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1919 unwrap(LHS), unwrap(RHS), Name));
1922 /*--.. Miscellaneous instructions ..........................................--*/
1924 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1925 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1928 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1929 LLVMValueRef *Args, unsigned NumArgs,
1931 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1932 unwrap(Args) + NumArgs, Name));
1935 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1936 LLVMValueRef Then, LLVMValueRef Else,
1938 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1942 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1943 LLVMTypeRef Ty, const char *Name) {
1944 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1947 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1948 LLVMValueRef Index, const char *Name) {
1949 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1953 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1954 LLVMValueRef EltVal, LLVMValueRef Index,
1956 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1957 unwrap(Index), Name));
1960 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1961 LLVMValueRef V2, LLVMValueRef Mask,
1963 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1964 unwrap(Mask), Name));
1967 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1968 unsigned Index, const char *Name) {
1969 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1972 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1973 LLVMValueRef EltVal, unsigned Index,
1975 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1979 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
1981 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
1984 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
1986 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
1989 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
1990 LLVMValueRef RHS, const char *Name) {
1991 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
1995 /*===-- Module providers --------------------------------------------------===*/
1997 LLVMModuleProviderRef
1998 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1999 return reinterpret_cast<LLVMModuleProviderRef>(M);
2002 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2007 /*===-- Memory buffers ----------------------------------------------------===*/
2009 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2011 LLVMMemoryBufferRef *OutMemBuf,
2012 char **OutMessage) {
2015 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2016 *OutMemBuf = wrap(MB);
2020 *OutMessage = strdup(Error.c_str());
2024 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2025 char **OutMessage) {
2026 MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2027 if (!MB->getBufferSize()) {
2029 *OutMessage = strdup("stdin is empty.");
2033 *OutMemBuf = wrap(MB);
2037 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2038 delete unwrap(MemBuf);