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::ArrayTyID:
145 return LLVMArrayTypeKind;
146 case Type::PointerTyID:
147 return LLVMPointerTypeKind;
148 case Type::OpaqueTyID:
149 return LLVMOpaqueTypeKind;
150 case Type::VectorTyID:
151 return LLVMVectorTypeKind;
155 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
156 return wrap(&unwrap(Ty)->getContext());
159 /*--.. Operations on integer types .........................................--*/
161 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
162 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
164 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
165 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
167 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
168 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
170 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
171 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
173 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
174 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
176 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
177 return wrap(IntegerType::get(*unwrap(C), NumBits));
180 LLVMTypeRef LLVMInt1Type(void) {
181 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
183 LLVMTypeRef LLVMInt8Type(void) {
184 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
186 LLVMTypeRef LLVMInt16Type(void) {
187 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
189 LLVMTypeRef LLVMInt32Type(void) {
190 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
192 LLVMTypeRef LLVMInt64Type(void) {
193 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
195 LLVMTypeRef LLVMIntType(unsigned NumBits) {
196 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
199 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
200 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
203 /*--.. Operations on real types ............................................--*/
205 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
206 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
208 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
209 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
211 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
212 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
214 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
215 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
217 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
218 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
221 LLVMTypeRef LLVMFloatType(void) {
222 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
224 LLVMTypeRef LLVMDoubleType(void) {
225 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
227 LLVMTypeRef LLVMX86FP80Type(void) {
228 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
230 LLVMTypeRef LLVMFP128Type(void) {
231 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
233 LLVMTypeRef LLVMPPCFP128Type(void) {
234 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
237 /*--.. Operations on function types ........................................--*/
239 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
240 LLVMTypeRef *ParamTypes, unsigned ParamCount,
242 std::vector<const Type*> Tys;
243 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
244 Tys.push_back(unwrap(*I));
246 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
249 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
250 return unwrap<FunctionType>(FunctionTy)->isVarArg();
253 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
254 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
257 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
258 return unwrap<FunctionType>(FunctionTy)->getNumParams();
261 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
262 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
263 for (FunctionType::param_iterator I = Ty->param_begin(),
264 E = Ty->param_end(); I != E; ++I)
268 /*--.. Operations on struct types ..........................................--*/
270 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
271 unsigned ElementCount, LLVMBool Packed) {
272 std::vector<const Type*> Tys;
273 for (LLVMTypeRef *I = ElementTypes,
274 *E = ElementTypes + ElementCount; I != E; ++I)
275 Tys.push_back(unwrap(*I));
277 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
280 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
281 unsigned ElementCount, LLVMBool Packed) {
282 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
283 ElementCount, Packed);
287 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
288 return unwrap<StructType>(StructTy)->getNumElements();
291 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
292 StructType *Ty = unwrap<StructType>(StructTy);
293 for (FunctionType::param_iterator I = Ty->element_begin(),
294 E = Ty->element_end(); I != E; ++I)
298 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
299 return unwrap<StructType>(StructTy)->isPacked();
302 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
304 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
305 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
308 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
309 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
312 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
313 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
316 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
317 return wrap(unwrap<SequentialType>(Ty)->getElementType());
320 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
321 return unwrap<ArrayType>(ArrayTy)->getNumElements();
324 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
325 return unwrap<PointerType>(PointerTy)->getAddressSpace();
328 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
329 return unwrap<VectorType>(VectorTy)->getNumElements();
332 /*--.. Operations on other types ...........................................--*/
334 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
335 return wrap(Type::getVoidTy(*unwrap(C)));
337 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
338 return wrap(Type::getLabelTy(*unwrap(C)));
340 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
341 return wrap(OpaqueType::get(*unwrap(C)));
344 LLVMTypeRef LLVMVoidType(void) {
345 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
347 LLVMTypeRef LLVMLabelType(void) {
348 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
350 LLVMTypeRef LLVMOpaqueType(void) {
351 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
354 /*--.. Operations on type handles ..........................................--*/
356 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
357 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
360 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
361 delete unwrap(TypeHandle);
364 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
365 return wrap(unwrap(TypeHandle)->get());
368 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
369 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
373 /*===-- Operations on values ----------------------------------------------===*/
375 /*--.. Operations on all values ............................................--*/
377 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
378 return wrap(unwrap(Val)->getType());
381 const char *LLVMGetValueName(LLVMValueRef Val) {
382 return unwrap(Val)->getName().data();
385 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
386 unwrap(Val)->setName(Name);
389 void LLVMDumpValue(LLVMValueRef Val) {
393 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
394 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
397 /*--.. Conversion functions ................................................--*/
399 #define LLVM_DEFINE_VALUE_CAST(name) \
400 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
401 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
404 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
406 /*--.. Operations on Uses ..................................................--*/
407 LLVMUseIteratorRef LLVMGetFirstUse(LLVMValueRef Val) {
408 Value *V = unwrap(Val);
409 Value::use_iterator I = V->use_begin();
410 if (I == V->use_end())
412 return wrap(&(I.getUse()));
415 LLVMUseIteratorRef LLVMGetNextUse(LLVMUseIteratorRef UR) {
416 return wrap(unwrap(UR)->getNext());
419 LLVMValueRef LLVMGetUser(LLVMUseIteratorRef UR) {
420 return wrap(unwrap(UR)->getUser());
423 LLVMValueRef LLVMGetUsedValue(LLVMUseIteratorRef UR) {
424 return wrap(unwrap(UR)->get());
427 /*--.. Operations on Users .................................................--*/
428 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
429 return wrap(unwrap<User>(Val)->getOperand(Index));
432 /*--.. Operations on constants of any type .................................--*/
434 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
435 return wrap(Constant::getNullValue(unwrap(Ty)));
438 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
439 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
442 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
443 return wrap(UndefValue::get(unwrap(Ty)));
446 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
447 return isa<Constant>(unwrap(Ty));
450 LLVMBool LLVMIsNull(LLVMValueRef Val) {
451 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
452 return C->isNullValue();
456 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
457 return isa<UndefValue>(unwrap(Val));
460 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
462 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
465 /*--.. Operations on scalar constants ......................................--*/
467 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
468 LLVMBool SignExtend) {
469 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
472 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
474 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
478 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
479 unsigned SLen, uint8_t Radix) {
480 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
484 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
485 return wrap(ConstantFP::get(unwrap(RealTy), N));
488 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
489 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
492 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
494 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
497 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
498 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
501 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
502 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
505 /*--.. Operations on composite constants ...................................--*/
507 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
509 LLVMBool DontNullTerminate) {
510 /* Inverted the sense of AddNull because ', 0)' is a
511 better mnemonic for null termination than ', 1)'. */
512 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
513 DontNullTerminate == 0));
515 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
516 LLVMValueRef *ConstantVals,
517 unsigned Count, LLVMBool Packed) {
518 return wrap(ConstantStruct::get(*unwrap(C),
519 unwrap<Constant>(ConstantVals, Count),
520 Count, Packed != 0));
523 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
524 LLVMBool DontNullTerminate) {
525 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
528 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
529 LLVMValueRef *ConstantVals, unsigned Length) {
530 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
531 unwrap<Constant>(ConstantVals, Length),
534 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
536 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
540 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
541 return wrap(ConstantVector::get(
542 unwrap<Constant>(ScalarConstantVals, Size), Size));
545 /*--.. Constant expressions ................................................--*/
547 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
548 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
551 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
552 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
555 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
556 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
559 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
560 return wrap(ConstantExpr::getNeg(
561 unwrap<Constant>(ConstantVal)));
564 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
565 return wrap(ConstantExpr::getFNeg(
566 unwrap<Constant>(ConstantVal)));
569 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
570 return wrap(ConstantExpr::getNot(
571 unwrap<Constant>(ConstantVal)));
574 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
575 return wrap(ConstantExpr::getAdd(
576 unwrap<Constant>(LHSConstant),
577 unwrap<Constant>(RHSConstant)));
580 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
581 LLVMValueRef RHSConstant) {
582 return wrap(ConstantExpr::getNSWAdd(
583 unwrap<Constant>(LHSConstant),
584 unwrap<Constant>(RHSConstant)));
587 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
588 return wrap(ConstantExpr::getFAdd(
589 unwrap<Constant>(LHSConstant),
590 unwrap<Constant>(RHSConstant)));
593 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
594 return wrap(ConstantExpr::getSub(
595 unwrap<Constant>(LHSConstant),
596 unwrap<Constant>(RHSConstant)));
599 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
600 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
601 unwrap<Constant>(RHSConstant)));
604 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
605 return wrap(ConstantExpr::getMul(
606 unwrap<Constant>(LHSConstant),
607 unwrap<Constant>(RHSConstant)));
610 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
611 return wrap(ConstantExpr::getFMul(
612 unwrap<Constant>(LHSConstant),
613 unwrap<Constant>(RHSConstant)));
616 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
617 return wrap(ConstantExpr::getUDiv(
618 unwrap<Constant>(LHSConstant),
619 unwrap<Constant>(RHSConstant)));
622 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
623 return wrap(ConstantExpr::getSDiv(
624 unwrap<Constant>(LHSConstant),
625 unwrap<Constant>(RHSConstant)));
628 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
629 LLVMValueRef RHSConstant) {
630 return wrap(ConstantExpr::getExactSDiv(
631 unwrap<Constant>(LHSConstant),
632 unwrap<Constant>(RHSConstant)));
635 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
636 return wrap(ConstantExpr::getFDiv(
637 unwrap<Constant>(LHSConstant),
638 unwrap<Constant>(RHSConstant)));
641 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
642 return wrap(ConstantExpr::getURem(
643 unwrap<Constant>(LHSConstant),
644 unwrap<Constant>(RHSConstant)));
647 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
648 return wrap(ConstantExpr::getSRem(
649 unwrap<Constant>(LHSConstant),
650 unwrap<Constant>(RHSConstant)));
653 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
654 return wrap(ConstantExpr::getFRem(
655 unwrap<Constant>(LHSConstant),
656 unwrap<Constant>(RHSConstant)));
659 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
660 return wrap(ConstantExpr::getAnd(
661 unwrap<Constant>(LHSConstant),
662 unwrap<Constant>(RHSConstant)));
665 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
666 return wrap(ConstantExpr::getOr(
667 unwrap<Constant>(LHSConstant),
668 unwrap<Constant>(RHSConstant)));
671 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
672 return wrap(ConstantExpr::getXor(
673 unwrap<Constant>(LHSConstant),
674 unwrap<Constant>(RHSConstant)));
677 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
678 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
679 return wrap(ConstantExpr::getICmp(Predicate,
680 unwrap<Constant>(LHSConstant),
681 unwrap<Constant>(RHSConstant)));
684 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
685 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
686 return wrap(ConstantExpr::getFCmp(Predicate,
687 unwrap<Constant>(LHSConstant),
688 unwrap<Constant>(RHSConstant)));
691 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
692 return wrap(ConstantExpr::getShl(
693 unwrap<Constant>(LHSConstant),
694 unwrap<Constant>(RHSConstant)));
697 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
698 return wrap(ConstantExpr::getLShr(
699 unwrap<Constant>(LHSConstant),
700 unwrap<Constant>(RHSConstant)));
703 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
704 return wrap(ConstantExpr::getAShr(
705 unwrap<Constant>(LHSConstant),
706 unwrap<Constant>(RHSConstant)));
709 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
710 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
711 return wrap(ConstantExpr::getGetElementPtr(
712 unwrap<Constant>(ConstantVal),
713 unwrap<Constant>(ConstantIndices,
718 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
719 LLVMValueRef *ConstantIndices,
720 unsigned NumIndices) {
721 Constant* Val = unwrap<Constant>(ConstantVal);
722 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
723 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
726 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
727 return wrap(ConstantExpr::getTrunc(
728 unwrap<Constant>(ConstantVal),
732 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
733 return wrap(ConstantExpr::getSExt(
734 unwrap<Constant>(ConstantVal),
738 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
739 return wrap(ConstantExpr::getZExt(
740 unwrap<Constant>(ConstantVal),
744 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
745 return wrap(ConstantExpr::getFPTrunc(
746 unwrap<Constant>(ConstantVal),
750 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
751 return wrap(ConstantExpr::getFPExtend(
752 unwrap<Constant>(ConstantVal),
756 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
757 return wrap(ConstantExpr::getUIToFP(
758 unwrap<Constant>(ConstantVal),
762 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
763 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
767 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
768 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
772 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
773 return wrap(ConstantExpr::getFPToSI(
774 unwrap<Constant>(ConstantVal),
778 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
779 return wrap(ConstantExpr::getPtrToInt(
780 unwrap<Constant>(ConstantVal),
784 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
785 return wrap(ConstantExpr::getIntToPtr(
786 unwrap<Constant>(ConstantVal),
790 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
791 return wrap(ConstantExpr::getBitCast(
792 unwrap<Constant>(ConstantVal),
796 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
797 LLVMTypeRef ToType) {
798 return wrap(ConstantExpr::getZExtOrBitCast(
799 unwrap<Constant>(ConstantVal),
803 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
804 LLVMTypeRef ToType) {
805 return wrap(ConstantExpr::getSExtOrBitCast(
806 unwrap<Constant>(ConstantVal),
810 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
811 LLVMTypeRef ToType) {
812 return wrap(ConstantExpr::getTruncOrBitCast(
813 unwrap<Constant>(ConstantVal),
817 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
818 LLVMTypeRef ToType) {
819 return wrap(ConstantExpr::getPointerCast(
820 unwrap<Constant>(ConstantVal),
824 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
826 return wrap(ConstantExpr::getIntegerCast(
827 unwrap<Constant>(ConstantVal),
832 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
833 return wrap(ConstantExpr::getFPCast(
834 unwrap<Constant>(ConstantVal),
838 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
839 LLVMValueRef ConstantIfTrue,
840 LLVMValueRef ConstantIfFalse) {
841 return wrap(ConstantExpr::getSelect(
842 unwrap<Constant>(ConstantCondition),
843 unwrap<Constant>(ConstantIfTrue),
844 unwrap<Constant>(ConstantIfFalse)));
847 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
848 LLVMValueRef IndexConstant) {
849 return wrap(ConstantExpr::getExtractElement(
850 unwrap<Constant>(VectorConstant),
851 unwrap<Constant>(IndexConstant)));
854 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
855 LLVMValueRef ElementValueConstant,
856 LLVMValueRef IndexConstant) {
857 return wrap(ConstantExpr::getInsertElement(
858 unwrap<Constant>(VectorConstant),
859 unwrap<Constant>(ElementValueConstant),
860 unwrap<Constant>(IndexConstant)));
863 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
864 LLVMValueRef VectorBConstant,
865 LLVMValueRef MaskConstant) {
866 return wrap(ConstantExpr::getShuffleVector(
867 unwrap<Constant>(VectorAConstant),
868 unwrap<Constant>(VectorBConstant),
869 unwrap<Constant>(MaskConstant)));
872 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
874 return wrap(ConstantExpr::getExtractValue(
875 unwrap<Constant>(AggConstant),
879 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
880 LLVMValueRef ElementValueConstant,
881 unsigned *IdxList, unsigned NumIdx) {
882 return wrap(ConstantExpr::getInsertValue(
883 unwrap<Constant>(AggConstant),
884 unwrap<Constant>(ElementValueConstant),
888 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
889 const char *Constraints,
890 LLVMBool HasSideEffects,
891 LLVMBool IsAlignStack) {
892 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
893 Constraints, HasSideEffects, IsAlignStack));
896 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
898 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
899 return wrap(unwrap<GlobalValue>(Global)->getParent());
902 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
903 return unwrap<GlobalValue>(Global)->isDeclaration();
906 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
907 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
909 assert(false && "Unhandled Linkage Type.");
910 case GlobalValue::ExternalLinkage:
911 return LLVMExternalLinkage;
912 case GlobalValue::AvailableExternallyLinkage:
913 return LLVMAvailableExternallyLinkage;
914 case GlobalValue::LinkOnceAnyLinkage:
915 return LLVMLinkOnceAnyLinkage;
916 case GlobalValue::LinkOnceODRLinkage:
917 return LLVMLinkOnceODRLinkage;
918 case GlobalValue::WeakAnyLinkage:
919 return LLVMWeakAnyLinkage;
920 case GlobalValue::WeakODRLinkage:
921 return LLVMWeakODRLinkage;
922 case GlobalValue::AppendingLinkage:
923 return LLVMAppendingLinkage;
924 case GlobalValue::InternalLinkage:
925 return LLVMInternalLinkage;
926 case GlobalValue::PrivateLinkage:
927 return LLVMPrivateLinkage;
928 case GlobalValue::LinkerPrivateLinkage:
929 return LLVMLinkerPrivateLinkage;
930 case GlobalValue::DLLImportLinkage:
931 return LLVMDLLImportLinkage;
932 case GlobalValue::DLLExportLinkage:
933 return LLVMDLLExportLinkage;
934 case GlobalValue::ExternalWeakLinkage:
935 return LLVMExternalWeakLinkage;
936 case GlobalValue::CommonLinkage:
937 return LLVMCommonLinkage;
940 // Should never get here.
941 return static_cast<LLVMLinkage>(0);
944 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
945 GlobalValue *GV = unwrap<GlobalValue>(Global);
949 assert(false && "Unhandled Linkage Type.");
950 case LLVMExternalLinkage:
951 GV->setLinkage(GlobalValue::ExternalLinkage);
953 case LLVMAvailableExternallyLinkage:
954 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
956 case LLVMLinkOnceAnyLinkage:
957 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
959 case LLVMLinkOnceODRLinkage:
960 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
962 case LLVMWeakAnyLinkage:
963 GV->setLinkage(GlobalValue::WeakAnyLinkage);
965 case LLVMWeakODRLinkage:
966 GV->setLinkage(GlobalValue::WeakODRLinkage);
968 case LLVMAppendingLinkage:
969 GV->setLinkage(GlobalValue::AppendingLinkage);
971 case LLVMInternalLinkage:
972 GV->setLinkage(GlobalValue::InternalLinkage);
974 case LLVMPrivateLinkage:
975 GV->setLinkage(GlobalValue::PrivateLinkage);
977 case LLVMLinkerPrivateLinkage:
978 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
980 case LLVMDLLImportLinkage:
981 GV->setLinkage(GlobalValue::DLLImportLinkage);
983 case LLVMDLLExportLinkage:
984 GV->setLinkage(GlobalValue::DLLExportLinkage);
986 case LLVMExternalWeakLinkage:
987 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
989 case LLVMGhostLinkage:
991 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
993 case LLVMCommonLinkage:
994 GV->setLinkage(GlobalValue::CommonLinkage);
999 const char *LLVMGetSection(LLVMValueRef Global) {
1000 return unwrap<GlobalValue>(Global)->getSection().c_str();
1003 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1004 unwrap<GlobalValue>(Global)->setSection(Section);
1007 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1008 return static_cast<LLVMVisibility>(
1009 unwrap<GlobalValue>(Global)->getVisibility());
1012 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1013 unwrap<GlobalValue>(Global)
1014 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1017 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1018 return unwrap<GlobalValue>(Global)->getAlignment();
1021 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1022 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1025 /*--.. Operations on global variables ......................................--*/
1027 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1028 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1029 GlobalValue::ExternalLinkage, 0, Name));
1032 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1033 return wrap(unwrap(M)->getNamedGlobal(Name));
1036 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1037 Module *Mod = unwrap(M);
1038 Module::global_iterator I = Mod->global_begin();
1039 if (I == Mod->global_end())
1044 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1045 Module *Mod = unwrap(M);
1046 Module::global_iterator I = Mod->global_end();
1047 if (I == Mod->global_begin())
1052 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1053 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1054 Module::global_iterator I = GV;
1055 if (++I == GV->getParent()->global_end())
1060 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1061 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1062 Module::global_iterator I = GV;
1063 if (I == GV->getParent()->global_begin())
1068 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1069 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1072 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1073 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1074 if ( !GV->hasInitializer() )
1076 return wrap(GV->getInitializer());
1079 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1080 unwrap<GlobalVariable>(GlobalVar)
1081 ->setInitializer(unwrap<Constant>(ConstantVal));
1084 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1085 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1088 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1089 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1092 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1093 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1096 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1097 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1100 /*--.. Operations on aliases ......................................--*/
1102 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1104 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1105 unwrap<Constant>(Aliasee), unwrap (M)));
1108 /*--.. Operations on functions .............................................--*/
1110 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1111 LLVMTypeRef FunctionTy) {
1112 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1113 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1116 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1117 return wrap(unwrap(M)->getFunction(Name));
1120 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1121 Module *Mod = unwrap(M);
1122 Module::iterator I = Mod->begin();
1123 if (I == Mod->end())
1128 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1129 Module *Mod = unwrap(M);
1130 Module::iterator I = Mod->end();
1131 if (I == Mod->begin())
1136 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1137 Function *Func = unwrap<Function>(Fn);
1138 Module::iterator I = Func;
1139 if (++I == Func->getParent()->end())
1144 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1145 Function *Func = unwrap<Function>(Fn);
1146 Module::iterator I = Func;
1147 if (I == Func->getParent()->begin())
1152 void LLVMDeleteFunction(LLVMValueRef Fn) {
1153 unwrap<Function>(Fn)->eraseFromParent();
1156 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1157 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1158 return F->getIntrinsicID();
1162 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1163 return unwrap<Function>(Fn)->getCallingConv();
1166 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1167 return unwrap<Function>(Fn)->setCallingConv(
1168 static_cast<CallingConv::ID>(CC));
1171 const char *LLVMGetGC(LLVMValueRef Fn) {
1172 Function *F = unwrap<Function>(Fn);
1173 return F->hasGC()? F->getGC() : 0;
1176 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1177 Function *F = unwrap<Function>(Fn);
1184 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1185 Function *Func = unwrap<Function>(Fn);
1186 const AttrListPtr PAL = Func->getAttributes();
1187 const AttrListPtr PALnew = PAL.addAttr(0, PA);
1188 Func->setAttributes(PALnew);
1191 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1192 Function *Func = unwrap<Function>(Fn);
1193 const AttrListPtr PAL = Func->getAttributes();
1194 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
1195 Func->setAttributes(PALnew);
1198 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1199 Function *Func = unwrap<Function>(Fn);
1200 const AttrListPtr PAL = Func->getAttributes();
1201 Attributes attr = PAL.getFnAttributes();
1202 return (LLVMAttribute)attr;
1205 /*--.. Operations on parameters ............................................--*/
1207 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1208 // This function is strictly redundant to
1209 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1210 return unwrap<Function>(FnRef)->arg_size();
1213 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1214 Function *Fn = unwrap<Function>(FnRef);
1215 for (Function::arg_iterator I = Fn->arg_begin(),
1216 E = Fn->arg_end(); I != E; I++)
1217 *ParamRefs++ = wrap(I);
1220 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1221 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1227 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1228 return wrap(unwrap<Argument>(V)->getParent());
1231 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1232 Function *Func = unwrap<Function>(Fn);
1233 Function::arg_iterator I = Func->arg_begin();
1234 if (I == Func->arg_end())
1239 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1240 Function *Func = unwrap<Function>(Fn);
1241 Function::arg_iterator I = Func->arg_end();
1242 if (I == Func->arg_begin())
1247 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1248 Argument *A = unwrap<Argument>(Arg);
1249 Function::arg_iterator I = A;
1250 if (++I == A->getParent()->arg_end())
1255 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1256 Argument *A = unwrap<Argument>(Arg);
1257 Function::arg_iterator I = A;
1258 if (I == A->getParent()->arg_begin())
1263 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1264 unwrap<Argument>(Arg)->addAttr(PA);
1267 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1268 unwrap<Argument>(Arg)->removeAttr(PA);
1271 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1272 Argument *A = unwrap<Argument>(Arg);
1273 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1275 return (LLVMAttribute)attr;
1279 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1280 unwrap<Argument>(Arg)->addAttr(
1281 Attribute::constructAlignmentFromInt(align));
1284 /*--.. Operations on basic blocks ..........................................--*/
1286 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1287 return wrap(static_cast<Value*>(unwrap(BB)));
1290 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1291 return isa<BasicBlock>(unwrap(Val));
1294 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1295 return wrap(unwrap<BasicBlock>(Val));
1298 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1299 return wrap(unwrap(BB)->getParent());
1302 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1303 return unwrap<Function>(FnRef)->size();
1306 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1307 Function *Fn = unwrap<Function>(FnRef);
1308 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1309 *BasicBlocksRefs++ = wrap(I);
1312 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1313 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1316 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1317 Function *Func = unwrap<Function>(Fn);
1318 Function::iterator I = Func->begin();
1319 if (I == Func->end())
1324 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1325 Function *Func = unwrap<Function>(Fn);
1326 Function::iterator I = Func->end();
1327 if (I == Func->begin())
1332 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1333 BasicBlock *Block = unwrap(BB);
1334 Function::iterator I = Block;
1335 if (++I == Block->getParent()->end())
1340 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1341 BasicBlock *Block = unwrap(BB);
1342 Function::iterator I = Block;
1343 if (I == Block->getParent()->begin())
1348 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1351 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1354 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1355 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1358 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1359 LLVMBasicBlockRef BBRef,
1361 BasicBlock *BB = unwrap(BBRef);
1362 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1365 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1367 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1370 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1371 unwrap(BBRef)->eraseFromParent();
1374 /*--.. Operations on instructions ..........................................--*/
1376 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1377 return wrap(unwrap<Instruction>(Inst)->getParent());
1380 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1381 BasicBlock *Block = unwrap(BB);
1382 BasicBlock::iterator I = Block->begin();
1383 if (I == Block->end())
1388 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1389 BasicBlock *Block = unwrap(BB);
1390 BasicBlock::iterator I = Block->end();
1391 if (I == Block->begin())
1396 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1397 Instruction *Instr = unwrap<Instruction>(Inst);
1398 BasicBlock::iterator I = Instr;
1399 if (++I == Instr->getParent()->end())
1404 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1405 Instruction *Instr = unwrap<Instruction>(Inst);
1406 BasicBlock::iterator I = Instr;
1407 if (I == Instr->getParent()->begin())
1412 /*--.. Call and invoke instructions ........................................--*/
1414 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1415 Value *V = unwrap(Instr);
1416 if (CallInst *CI = dyn_cast<CallInst>(V))
1417 return CI->getCallingConv();
1418 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1419 return II->getCallingConv();
1420 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1424 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1425 Value *V = unwrap(Instr);
1426 if (CallInst *CI = dyn_cast<CallInst>(V))
1427 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1428 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1429 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1430 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1433 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1435 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1437 Call.getAttributes().addAttr(index, PA));
1440 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1442 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1444 Call.getAttributes().removeAttr(index, PA));
1447 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1449 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1451 Call.getAttributes().addAttr(index,
1452 Attribute::constructAlignmentFromInt(align)));
1455 /*--.. Operations on call instructions (only) ..............................--*/
1457 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1458 return unwrap<CallInst>(Call)->isTailCall();
1461 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1462 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1465 /*--.. Operations on phi nodes .............................................--*/
1467 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1468 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1469 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1470 for (unsigned I = 0; I != Count; ++I)
1471 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1474 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1475 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1478 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1479 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1482 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1483 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1487 /*===-- Instruction builders ----------------------------------------------===*/
1489 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1490 return wrap(new IRBuilder<>(*unwrap(C)));
1493 LLVMBuilderRef LLVMCreateBuilder(void) {
1494 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1497 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1498 LLVMValueRef Instr) {
1499 BasicBlock *BB = unwrap(Block);
1500 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1501 unwrap(Builder)->SetInsertPoint(BB, I);
1504 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1505 Instruction *I = unwrap<Instruction>(Instr);
1506 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1509 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1510 BasicBlock *BB = unwrap(Block);
1511 unwrap(Builder)->SetInsertPoint(BB);
1514 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1515 return wrap(unwrap(Builder)->GetInsertBlock());
1518 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1519 unwrap(Builder)->ClearInsertionPoint ();
1522 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1523 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1526 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1528 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1531 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1532 delete unwrap(Builder);
1535 /*--.. Instruction builders ................................................--*/
1537 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1538 return wrap(unwrap(B)->CreateRetVoid());
1541 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1542 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1545 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1547 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1550 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1551 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1554 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1555 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1556 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1559 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1560 LLVMBasicBlockRef Else, unsigned NumCases) {
1561 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1564 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1565 LLVMValueRef *Args, unsigned NumArgs,
1566 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1568 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1569 unwrap(Args), unwrap(Args) + NumArgs,
1573 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1574 return wrap(unwrap(B)->CreateUnwind());
1577 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1578 return wrap(unwrap(B)->CreateUnreachable());
1581 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1582 LLVMBasicBlockRef Dest) {
1583 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1586 /*--.. Arithmetic ..........................................................--*/
1588 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1590 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1593 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1595 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1598 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1600 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1603 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1605 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1608 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1610 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1613 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1615 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1618 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1620 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1623 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1625 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1628 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1630 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1633 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1634 LLVMValueRef RHS, const char *Name) {
1635 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1638 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1640 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1643 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1645 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1648 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1650 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1653 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1655 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1658 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1660 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1663 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1665 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1668 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1670 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1673 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1675 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1678 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1680 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1683 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1685 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1688 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1689 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1692 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1693 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1696 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1697 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1700 /*--.. Memory ..............................................................--*/
1702 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1704 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1705 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1706 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1707 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1708 ITy, unwrap(Ty), AllocSize,
1710 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1713 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1714 LLVMValueRef Val, const char *Name) {
1715 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1716 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1717 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1718 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1719 ITy, unwrap(Ty), AllocSize,
1720 unwrap(Val), 0, "");
1721 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1724 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1726 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1729 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1730 LLVMValueRef Val, const char *Name) {
1731 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1734 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1735 return wrap(unwrap(B)->Insert(
1736 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1740 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1742 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1745 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1746 LLVMValueRef PointerVal) {
1747 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1750 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1751 LLVMValueRef *Indices, unsigned NumIndices,
1753 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1754 unwrap(Indices) + NumIndices, Name));
1757 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1758 LLVMValueRef *Indices, unsigned NumIndices,
1760 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1761 unwrap(Indices) + NumIndices, Name));
1764 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1765 unsigned Idx, const char *Name) {
1766 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1769 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1771 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1774 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1776 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1779 /*--.. Casts ...............................................................--*/
1781 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1782 LLVMTypeRef DestTy, const char *Name) {
1783 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1786 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1787 LLVMTypeRef DestTy, const char *Name) {
1788 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1791 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1792 LLVMTypeRef DestTy, const char *Name) {
1793 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1796 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1797 LLVMTypeRef DestTy, const char *Name) {
1798 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1801 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1802 LLVMTypeRef DestTy, const char *Name) {
1803 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1806 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1807 LLVMTypeRef DestTy, const char *Name) {
1808 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1811 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1812 LLVMTypeRef DestTy, const char *Name) {
1813 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1816 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1817 LLVMTypeRef DestTy, const char *Name) {
1818 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1821 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1822 LLVMTypeRef DestTy, const char *Name) {
1823 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1826 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1827 LLVMTypeRef DestTy, const char *Name) {
1828 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1831 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1832 LLVMTypeRef DestTy, const char *Name) {
1833 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1836 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1837 LLVMTypeRef DestTy, const char *Name) {
1838 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1841 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1842 LLVMTypeRef DestTy, const char *Name) {
1843 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1847 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1848 LLVMTypeRef DestTy, const char *Name) {
1849 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1853 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1854 LLVMTypeRef DestTy, const char *Name) {
1855 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1859 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1860 LLVMTypeRef DestTy, const char *Name) {
1861 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1864 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1865 LLVMTypeRef DestTy, const char *Name) {
1866 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
1867 /*isSigned*/true, Name));
1870 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1871 LLVMTypeRef DestTy, const char *Name) {
1872 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
1875 /*--.. Comparisons .........................................................--*/
1877 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1878 LLVMValueRef LHS, LLVMValueRef RHS,
1880 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1881 unwrap(LHS), unwrap(RHS), Name));
1884 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1885 LLVMValueRef LHS, LLVMValueRef RHS,
1887 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1888 unwrap(LHS), unwrap(RHS), Name));
1891 /*--.. Miscellaneous instructions ..........................................--*/
1893 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1894 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1897 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1898 LLVMValueRef *Args, unsigned NumArgs,
1900 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1901 unwrap(Args) + NumArgs, Name));
1904 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1905 LLVMValueRef Then, LLVMValueRef Else,
1907 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1911 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1912 LLVMTypeRef Ty, const char *Name) {
1913 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1916 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1917 LLVMValueRef Index, const char *Name) {
1918 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1922 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1923 LLVMValueRef EltVal, LLVMValueRef Index,
1925 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1926 unwrap(Index), Name));
1929 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1930 LLVMValueRef V2, LLVMValueRef Mask,
1932 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1933 unwrap(Mask), Name));
1936 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1937 unsigned Index, const char *Name) {
1938 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1941 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1942 LLVMValueRef EltVal, unsigned Index,
1944 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1948 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
1950 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
1953 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
1955 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
1958 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
1959 LLVMValueRef RHS, const char *Name) {
1960 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
1964 /*===-- Module providers --------------------------------------------------===*/
1966 LLVMModuleProviderRef
1967 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1968 return reinterpret_cast<LLVMModuleProviderRef>(M);
1971 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1976 /*===-- Memory buffers ----------------------------------------------------===*/
1978 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
1980 LLVMMemoryBufferRef *OutMemBuf,
1981 char **OutMessage) {
1984 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1985 *OutMemBuf = wrap(MB);
1989 *OutMessage = strdup(Error.c_str());
1993 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1994 char **OutMessage) {
1995 MemoryBuffer *MB = MemoryBuffer::getSTDIN();
1996 if (!MB->getBufferSize()) {
1998 *OutMessage = strdup("stdin is empty.");
2002 *OutMemBuf = wrap(MB);
2006 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2007 delete unwrap(MemBuf);