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/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/ErrorHandling.h"
36 /*===-- Error handling ----------------------------------------------------===*/
38 void LLVMDisposeMessage(char *Message) {
43 /*===-- Operations on contexts --------------------------------------------===*/
45 LLVMContextRef LLVMContextCreate() {
46 return wrap(new LLVMContext());
49 LLVMContextRef LLVMGetGlobalContext() {
50 return wrap(&getGlobalContext());
53 void LLVMContextDispose(LLVMContextRef C) {
58 /*===-- Operations on modules ---------------------------------------------===*/
60 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
61 return wrap(new Module(ModuleID, getGlobalContext()));
64 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
66 return wrap(new Module(ModuleID, *unwrap(C)));
69 void LLVMDisposeModule(LLVMModuleRef M) {
73 /*--.. Data layout .........................................................--*/
74 const char * LLVMGetDataLayout(LLVMModuleRef M) {
75 return unwrap(M)->getDataLayout().c_str();
78 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
79 unwrap(M)->setDataLayout(Triple);
82 /*--.. Target triple .......................................................--*/
83 const char * LLVMGetTarget(LLVMModuleRef M) {
84 return unwrap(M)->getTargetTriple().c_str();
87 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
88 unwrap(M)->setTargetTriple(Triple);
91 /*--.. Type names ..........................................................--*/
92 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
93 return unwrap(M)->addTypeName(Name, unwrap(Ty));
96 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
97 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
99 TypeSymbolTable::iterator I = TST.find(Name);
104 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
105 return wrap(unwrap(M)->getTypeByName(Name));
108 void LLVMDumpModule(LLVMModuleRef M) {
113 /*===-- Operations on types -----------------------------------------------===*/
115 /*--.. Operations on all types (mostly) ....................................--*/
117 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
118 switch (unwrap(Ty)->getTypeID()) {
120 assert(false && "Unhandled TypeID.");
122 return LLVMVoidTypeKind;
123 case Type::FloatTyID:
124 return LLVMFloatTypeKind;
125 case Type::DoubleTyID:
126 return LLVMDoubleTypeKind;
127 case Type::X86_FP80TyID:
128 return LLVMX86_FP80TypeKind;
129 case Type::FP128TyID:
130 return LLVMFP128TypeKind;
131 case Type::PPC_FP128TyID:
132 return LLVMPPC_FP128TypeKind;
133 case Type::LabelTyID:
134 return LLVMLabelTypeKind;
135 case Type::MetadataTyID:
136 return LLVMMetadataTypeKind;
137 case Type::IntegerTyID:
138 return LLVMIntegerTypeKind;
139 case Type::FunctionTyID:
140 return LLVMFunctionTypeKind;
141 case Type::StructTyID:
142 return LLVMStructTypeKind;
143 case Type::ArrayTyID:
144 return LLVMArrayTypeKind;
145 case Type::PointerTyID:
146 return LLVMPointerTypeKind;
147 case Type::OpaqueTyID:
148 return LLVMOpaqueTypeKind;
149 case Type::VectorTyID:
150 return LLVMVectorTypeKind;
154 /*--.. Operations on integer types .........................................--*/
156 LLVMTypeRef LLVMInt1Type(void) {
157 return (LLVMTypeRef) Type::getInt1Ty(getGlobalContext());
159 LLVMTypeRef LLVMInt8Type(void) {
160 return (LLVMTypeRef) Type::getInt8Ty(getGlobalContext());
162 LLVMTypeRef LLVMInt16Type(void) {
163 return (LLVMTypeRef) Type::getInt16Ty(getGlobalContext());
165 LLVMTypeRef LLVMInt32Type(void) {
166 return (LLVMTypeRef) Type::getInt32Ty(getGlobalContext());
168 LLVMTypeRef LLVMInt64Type(void) {
169 return (LLVMTypeRef) Type::getInt64Ty(getGlobalContext());
172 LLVMTypeRef LLVMIntType(unsigned NumBits) {
173 return wrap(IntegerType::get(getGlobalContext(), NumBits));
176 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
177 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
180 /*--.. Operations on real types ............................................--*/
182 LLVMTypeRef LLVMFloatType(void) {
183 return (LLVMTypeRef) Type::getFloatTy(getGlobalContext());
185 LLVMTypeRef LLVMDoubleType(void) {
186 return (LLVMTypeRef) Type::getDoubleTy(getGlobalContext());
188 LLVMTypeRef LLVMX86FP80Type(void) {
189 return (LLVMTypeRef) Type::getX86_FP80Ty(getGlobalContext());
191 LLVMTypeRef LLVMFP128Type(void) {
192 return (LLVMTypeRef) Type::getFP128Ty(getGlobalContext());
194 LLVMTypeRef LLVMPPCFP128Type(void) {
195 return (LLVMTypeRef) Type::getPPC_FP128Ty(getGlobalContext());
198 /*--.. Operations on function types ........................................--*/
200 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
201 LLVMTypeRef *ParamTypes, unsigned ParamCount,
203 std::vector<const Type*> Tys;
204 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
205 Tys.push_back(unwrap(*I));
207 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
210 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
211 return unwrap<FunctionType>(FunctionTy)->isVarArg();
214 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
215 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
218 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
219 return unwrap<FunctionType>(FunctionTy)->getNumParams();
222 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
223 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
224 for (FunctionType::param_iterator I = Ty->param_begin(),
225 E = Ty->param_end(); I != E; ++I)
229 /*--.. Operations on struct types ..........................................--*/
231 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
232 unsigned ElementCount, int Packed) {
233 std::vector<const Type*> Tys;
234 for (LLVMTypeRef *I = ElementTypes,
235 *E = ElementTypes + ElementCount; I != E; ++I)
236 Tys.push_back(unwrap(*I));
238 return wrap(StructType::get(getGlobalContext(), Tys, Packed != 0));
241 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
242 return unwrap<StructType>(StructTy)->getNumElements();
245 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
246 StructType *Ty = unwrap<StructType>(StructTy);
247 for (FunctionType::param_iterator I = Ty->element_begin(),
248 E = Ty->element_end(); I != E; ++I)
252 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
253 return unwrap<StructType>(StructTy)->isPacked();
256 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
258 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
259 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
262 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
263 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
266 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
267 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
270 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
271 return wrap(unwrap<SequentialType>(Ty)->getElementType());
274 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
275 return unwrap<ArrayType>(ArrayTy)->getNumElements();
278 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
279 return unwrap<PointerType>(PointerTy)->getAddressSpace();
282 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
283 return unwrap<VectorType>(VectorTy)->getNumElements();
286 /*--.. Operations on other types ...........................................--*/
288 LLVMTypeRef LLVMVoidType(void) {
289 return (LLVMTypeRef) Type::getVoidTy(getGlobalContext());
291 LLVMTypeRef LLVMLabelType(void) {
292 return (LLVMTypeRef) Type::getLabelTy(getGlobalContext());
295 LLVMTypeRef LLVMOpaqueType(void) {
296 return wrap(OpaqueType::get());
299 /*--.. Operations on type handles ..........................................--*/
301 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
302 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
305 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
306 delete unwrap(TypeHandle);
309 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
310 return wrap(unwrap(TypeHandle)->get());
313 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
314 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
318 /*===-- Operations on values ----------------------------------------------===*/
320 /*--.. Operations on all values ............................................--*/
322 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
323 return wrap(unwrap(Val)->getType());
326 const char *LLVMGetValueName(LLVMValueRef Val) {
327 return unwrap(Val)->getName().data();
330 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
331 unwrap(Val)->setName(Name);
334 void LLVMDumpValue(LLVMValueRef Val) {
339 /*--.. Conversion functions ................................................--*/
341 #define LLVM_DEFINE_VALUE_CAST(name) \
342 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
343 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
346 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
349 /*--.. Operations on constants of any type .................................--*/
351 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
352 return wrap(Constant::getNullValue(unwrap(Ty)));
355 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
356 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
359 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
360 return wrap(UndefValue::get(unwrap(Ty)));
363 int LLVMIsConstant(LLVMValueRef Ty) {
364 return isa<Constant>(unwrap(Ty));
367 int LLVMIsNull(LLVMValueRef Val) {
368 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
369 return C->isNullValue();
373 int LLVMIsUndef(LLVMValueRef Val) {
374 return isa<UndefValue>(unwrap(Val));
377 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
379 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
382 /*--.. Operations on scalar constants ......................................--*/
384 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
386 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
389 static const fltSemantics &SemanticsForType(Type *Ty) {
390 assert(Ty->isFloatingPoint() && "Type is not floating point!");
391 if (Ty == Type::getFloatTy(getGlobalContext()))
392 return APFloat::IEEEsingle;
393 if (Ty == Type::getDoubleTy(getGlobalContext()))
394 return APFloat::IEEEdouble;
395 if (Ty == Type::getX86_FP80Ty(getGlobalContext()))
396 return APFloat::x87DoubleExtended;
397 if (Ty == Type::getFP128Ty(getGlobalContext()))
398 return APFloat::IEEEquad;
399 if (Ty == Type::getPPC_FP128Ty(getGlobalContext()))
400 return APFloat::PPCDoubleDouble;
401 return APFloat::Bogus;
404 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
407 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
409 return wrap(ConstantFP::get(getGlobalContext(), APN));
412 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
413 return wrap(ConstantFP::get(getGlobalContext(),
414 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
417 /*--.. Operations on composite constants ...................................--*/
419 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
420 int DontNullTerminate) {
421 /* Inverted the sense of AddNull because ', 0)' is a
422 better mnemonic for null termination than ', 1)'. */
423 return wrap(ConstantArray::get(getGlobalContext(), std::string(Str, Length),
424 DontNullTerminate == 0));
427 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
428 LLVMValueRef *ConstantVals, unsigned Length) {
429 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
430 unwrap<Constant>(ConstantVals, Length),
434 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
436 return wrap(ConstantStruct::get(getGlobalContext(),
437 unwrap<Constant>(ConstantVals, Count),
438 Count, Packed != 0));
441 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
442 return wrap(ConstantVector::get(
443 unwrap<Constant>(ScalarConstantVals, Size), Size));
446 /*--.. Constant expressions ................................................--*/
448 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
449 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
452 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
453 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
456 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
457 return wrap(ConstantExpr::getNeg(
458 unwrap<Constant>(ConstantVal)));
461 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
462 return wrap(ConstantExpr::getNot(
463 unwrap<Constant>(ConstantVal)));
466 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
467 return wrap(ConstantExpr::getAdd(
468 unwrap<Constant>(LHSConstant),
469 unwrap<Constant>(RHSConstant)));
472 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
473 return wrap(ConstantExpr::getSub(
474 unwrap<Constant>(LHSConstant),
475 unwrap<Constant>(RHSConstant)));
478 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
479 return wrap(ConstantExpr::getMul(
480 unwrap<Constant>(LHSConstant),
481 unwrap<Constant>(RHSConstant)));
484 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
485 return wrap(ConstantExpr::getUDiv(
486 unwrap<Constant>(LHSConstant),
487 unwrap<Constant>(RHSConstant)));
490 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
491 return wrap(ConstantExpr::getSDiv(
492 unwrap<Constant>(LHSConstant),
493 unwrap<Constant>(RHSConstant)));
496 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
497 return wrap(ConstantExpr::getFDiv(
498 unwrap<Constant>(LHSConstant),
499 unwrap<Constant>(RHSConstant)));
502 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
503 return wrap(ConstantExpr::getURem(
504 unwrap<Constant>(LHSConstant),
505 unwrap<Constant>(RHSConstant)));
508 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
509 return wrap(ConstantExpr::getSRem(
510 unwrap<Constant>(LHSConstant),
511 unwrap<Constant>(RHSConstant)));
514 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
515 return wrap(ConstantExpr::getFRem(
516 unwrap<Constant>(LHSConstant),
517 unwrap<Constant>(RHSConstant)));
520 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
521 return wrap(ConstantExpr::getAnd(
522 unwrap<Constant>(LHSConstant),
523 unwrap<Constant>(RHSConstant)));
526 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
527 return wrap(ConstantExpr::getOr(
528 unwrap<Constant>(LHSConstant),
529 unwrap<Constant>(RHSConstant)));
532 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
533 return wrap(ConstantExpr::getXor(
534 unwrap<Constant>(LHSConstant),
535 unwrap<Constant>(RHSConstant)));
538 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
539 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
540 return wrap(ConstantExpr::getICmp(Predicate,
541 unwrap<Constant>(LHSConstant),
542 unwrap<Constant>(RHSConstant)));
545 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
546 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
547 return wrap(ConstantExpr::getFCmp(Predicate,
548 unwrap<Constant>(LHSConstant),
549 unwrap<Constant>(RHSConstant)));
552 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
553 return wrap(ConstantExpr::getShl(
554 unwrap<Constant>(LHSConstant),
555 unwrap<Constant>(RHSConstant)));
558 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
559 return wrap(ConstantExpr::getLShr(
560 unwrap<Constant>(LHSConstant),
561 unwrap<Constant>(RHSConstant)));
564 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
565 return wrap(ConstantExpr::getAShr(
566 unwrap<Constant>(LHSConstant),
567 unwrap<Constant>(RHSConstant)));
570 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
571 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
572 return wrap(ConstantExpr::getGetElementPtr(
573 unwrap<Constant>(ConstantVal),
574 unwrap<Constant>(ConstantIndices,
579 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
580 return wrap(ConstantExpr::getTrunc(
581 unwrap<Constant>(ConstantVal),
585 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
586 return wrap(ConstantExpr::getSExt(
587 unwrap<Constant>(ConstantVal),
591 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
592 return wrap(ConstantExpr::getZExt(
593 unwrap<Constant>(ConstantVal),
597 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
598 return wrap(ConstantExpr::getFPTrunc(
599 unwrap<Constant>(ConstantVal),
603 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
604 return wrap(ConstantExpr::getFPExtend(
605 unwrap<Constant>(ConstantVal),
609 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
610 return wrap(ConstantExpr::getUIToFP(
611 unwrap<Constant>(ConstantVal),
615 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
616 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
620 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
621 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
625 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
626 return wrap(ConstantExpr::getFPToSI(
627 unwrap<Constant>(ConstantVal),
631 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
632 return wrap(ConstantExpr::getPtrToInt(
633 unwrap<Constant>(ConstantVal),
637 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
638 return wrap(ConstantExpr::getIntToPtr(
639 unwrap<Constant>(ConstantVal),
643 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
644 return wrap(ConstantExpr::getBitCast(
645 unwrap<Constant>(ConstantVal),
649 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
650 LLVMValueRef ConstantIfTrue,
651 LLVMValueRef ConstantIfFalse) {
652 return wrap(ConstantExpr::getSelect(
653 unwrap<Constant>(ConstantCondition),
654 unwrap<Constant>(ConstantIfTrue),
655 unwrap<Constant>(ConstantIfFalse)));
658 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
659 LLVMValueRef IndexConstant) {
660 return wrap(ConstantExpr::getExtractElement(
661 unwrap<Constant>(VectorConstant),
662 unwrap<Constant>(IndexConstant)));
665 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
666 LLVMValueRef ElementValueConstant,
667 LLVMValueRef IndexConstant) {
668 return wrap(ConstantExpr::getInsertElement(
669 unwrap<Constant>(VectorConstant),
670 unwrap<Constant>(ElementValueConstant),
671 unwrap<Constant>(IndexConstant)));
674 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
675 LLVMValueRef VectorBConstant,
676 LLVMValueRef MaskConstant) {
677 return wrap(ConstantExpr::getShuffleVector(
678 unwrap<Constant>(VectorAConstant),
679 unwrap<Constant>(VectorBConstant),
680 unwrap<Constant>(MaskConstant)));
683 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
685 return wrap(ConstantExpr::getExtractValue(
686 unwrap<Constant>(AggConstant),
690 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
691 LLVMValueRef ElementValueConstant,
692 unsigned *IdxList, unsigned NumIdx) {
693 return wrap(ConstantExpr::getInsertValue(
694 unwrap<Constant>(AggConstant),
695 unwrap<Constant>(ElementValueConstant),
699 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
700 const char *Constraints, int HasSideEffects) {
701 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
702 Constraints, HasSideEffects));
705 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
707 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
708 return wrap(unwrap<GlobalValue>(Global)->getParent());
711 int LLVMIsDeclaration(LLVMValueRef Global) {
712 return unwrap<GlobalValue>(Global)->isDeclaration();
715 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
716 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
718 assert(false && "Unhandled Linkage Type.");
719 case GlobalValue::ExternalLinkage:
720 return LLVMExternalLinkage;
721 case GlobalValue::AvailableExternallyLinkage:
722 return LLVMAvailableExternallyLinkage;
723 case GlobalValue::LinkOnceAnyLinkage:
724 return LLVMLinkOnceAnyLinkage;
725 case GlobalValue::LinkOnceODRLinkage:
726 return LLVMLinkOnceODRLinkage;
727 case GlobalValue::WeakAnyLinkage:
728 return LLVMWeakAnyLinkage;
729 case GlobalValue::WeakODRLinkage:
730 return LLVMWeakODRLinkage;
731 case GlobalValue::AppendingLinkage:
732 return LLVMAppendingLinkage;
733 case GlobalValue::InternalLinkage:
734 return LLVMInternalLinkage;
735 case GlobalValue::PrivateLinkage:
736 return LLVMPrivateLinkage;
737 case GlobalValue::LinkerPrivateLinkage:
738 return LLVMLinkerPrivateLinkage;
739 case GlobalValue::DLLImportLinkage:
740 return LLVMDLLImportLinkage;
741 case GlobalValue::DLLExportLinkage:
742 return LLVMDLLExportLinkage;
743 case GlobalValue::ExternalWeakLinkage:
744 return LLVMExternalWeakLinkage;
745 case GlobalValue::GhostLinkage:
746 return LLVMGhostLinkage;
747 case GlobalValue::CommonLinkage:
748 return LLVMCommonLinkage;
751 // Should never get here.
752 return static_cast<LLVMLinkage>(0);
755 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
756 GlobalValue *GV = unwrap<GlobalValue>(Global);
760 assert(false && "Unhandled Linkage Type.");
761 case LLVMExternalLinkage:
762 GV->setLinkage(GlobalValue::ExternalLinkage);
764 case LLVMAvailableExternallyLinkage:
765 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
767 case LLVMLinkOnceAnyLinkage:
768 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
770 case LLVMLinkOnceODRLinkage:
771 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
773 case LLVMWeakAnyLinkage:
774 GV->setLinkage(GlobalValue::WeakAnyLinkage);
776 case LLVMWeakODRLinkage:
777 GV->setLinkage(GlobalValue::WeakODRLinkage);
779 case LLVMAppendingLinkage:
780 GV->setLinkage(GlobalValue::AppendingLinkage);
782 case LLVMInternalLinkage:
783 GV->setLinkage(GlobalValue::InternalLinkage);
785 case LLVMPrivateLinkage:
786 GV->setLinkage(GlobalValue::PrivateLinkage);
788 case LLVMLinkerPrivateLinkage:
789 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
791 case LLVMDLLImportLinkage:
792 GV->setLinkage(GlobalValue::DLLImportLinkage);
794 case LLVMDLLExportLinkage:
795 GV->setLinkage(GlobalValue::DLLExportLinkage);
797 case LLVMExternalWeakLinkage:
798 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
800 case LLVMGhostLinkage:
801 GV->setLinkage(GlobalValue::GhostLinkage);
803 case LLVMCommonLinkage:
804 GV->setLinkage(GlobalValue::CommonLinkage);
809 const char *LLVMGetSection(LLVMValueRef Global) {
810 return unwrap<GlobalValue>(Global)->getSection().c_str();
813 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
814 unwrap<GlobalValue>(Global)->setSection(Section);
817 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
818 return static_cast<LLVMVisibility>(
819 unwrap<GlobalValue>(Global)->getVisibility());
822 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
823 unwrap<GlobalValue>(Global)
824 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
827 unsigned LLVMGetAlignment(LLVMValueRef Global) {
828 return unwrap<GlobalValue>(Global)->getAlignment();
831 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
832 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
835 /*--.. Operations on global variables ......................................--*/
837 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
838 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
839 GlobalValue::ExternalLinkage, 0, Name));
842 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
843 return wrap(unwrap(M)->getNamedGlobal(Name));
846 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
847 Module *Mod = unwrap(M);
848 Module::global_iterator I = Mod->global_begin();
849 if (I == Mod->global_end())
854 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
855 Module *Mod = unwrap(M);
856 Module::global_iterator I = Mod->global_end();
857 if (I == Mod->global_begin())
862 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
863 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
864 Module::global_iterator I = GV;
865 if (++I == GV->getParent()->global_end())
870 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
871 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
872 Module::global_iterator I = GV;
873 if (I == GV->getParent()->global_begin())
878 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
879 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
882 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
883 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
886 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
887 unwrap<GlobalVariable>(GlobalVar)
888 ->setInitializer(unwrap<Constant>(ConstantVal));
891 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
892 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
895 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
896 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
899 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
900 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
903 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
904 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
907 /*--.. Operations on aliases ......................................--*/
909 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
911 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
912 unwrap<Constant>(Aliasee), unwrap (M)));
915 /*--.. Operations on functions .............................................--*/
917 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
918 LLVMTypeRef FunctionTy) {
919 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
920 GlobalValue::ExternalLinkage, Name, unwrap(M)));
923 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
924 return wrap(unwrap(M)->getFunction(Name));
927 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
928 Module *Mod = unwrap(M);
929 Module::iterator I = Mod->begin();
935 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
936 Module *Mod = unwrap(M);
937 Module::iterator I = Mod->end();
938 if (I == Mod->begin())
943 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
944 Function *Func = unwrap<Function>(Fn);
945 Module::iterator I = Func;
946 if (++I == Func->getParent()->end())
951 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
952 Function *Func = unwrap<Function>(Fn);
953 Module::iterator I = Func;
954 if (I == Func->getParent()->begin())
959 void LLVMDeleteFunction(LLVMValueRef Fn) {
960 unwrap<Function>(Fn)->eraseFromParent();
963 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
964 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
965 return F->getIntrinsicID();
969 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
970 return unwrap<Function>(Fn)->getCallingConv();
973 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
974 return unwrap<Function>(Fn)->setCallingConv(CC);
977 const char *LLVMGetGC(LLVMValueRef Fn) {
978 Function *F = unwrap<Function>(Fn);
979 return F->hasGC()? F->getGC() : 0;
982 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
983 Function *F = unwrap<Function>(Fn);
990 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
991 Function *Func = unwrap<Function>(Fn);
992 const AttrListPtr PAL = Func->getAttributes();
993 const AttrListPtr PALnew = PAL.addAttr(0, PA);
994 Func->setAttributes(PALnew);
997 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
998 Function *Func = unwrap<Function>(Fn);
999 const AttrListPtr PAL = Func->getAttributes();
1000 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
1001 Func->setAttributes(PALnew);
1004 /*--.. Operations on parameters ............................................--*/
1006 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1007 // This function is strictly redundant to
1008 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1009 return unwrap<Function>(FnRef)->arg_size();
1012 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1013 Function *Fn = unwrap<Function>(FnRef);
1014 for (Function::arg_iterator I = Fn->arg_begin(),
1015 E = Fn->arg_end(); I != E; I++)
1016 *ParamRefs++ = wrap(I);
1019 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1020 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1026 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1027 return wrap(unwrap<Argument>(V)->getParent());
1030 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1031 Function *Func = unwrap<Function>(Fn);
1032 Function::arg_iterator I = Func->arg_begin();
1033 if (I == Func->arg_end())
1038 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1039 Function *Func = unwrap<Function>(Fn);
1040 Function::arg_iterator I = Func->arg_end();
1041 if (I == Func->arg_begin())
1046 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1047 Argument *A = unwrap<Argument>(Arg);
1048 Function::arg_iterator I = A;
1049 if (++I == A->getParent()->arg_end())
1054 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1055 Argument *A = unwrap<Argument>(Arg);
1056 Function::arg_iterator I = A;
1057 if (I == A->getParent()->arg_begin())
1062 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1063 unwrap<Argument>(Arg)->addAttr(PA);
1066 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1067 unwrap<Argument>(Arg)->removeAttr(PA);
1070 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1071 unwrap<Argument>(Arg)->addAttr(
1072 Attribute::constructAlignmentFromInt(align));
1075 /*--.. Operations on basic blocks ..........................................--*/
1077 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1078 return wrap(static_cast<Value*>(unwrap(BB)));
1081 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
1082 return isa<BasicBlock>(unwrap(Val));
1085 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1086 return wrap(unwrap<BasicBlock>(Val));
1089 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1090 return wrap(unwrap(BB)->getParent());
1093 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1094 return unwrap<Function>(FnRef)->size();
1097 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1098 Function *Fn = unwrap<Function>(FnRef);
1099 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1100 *BasicBlocksRefs++ = wrap(I);
1103 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1104 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1107 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1108 Function *Func = unwrap<Function>(Fn);
1109 Function::iterator I = Func->begin();
1110 if (I == Func->end())
1115 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1116 Function *Func = unwrap<Function>(Fn);
1117 Function::iterator I = Func->end();
1118 if (I == Func->begin())
1123 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1124 BasicBlock *Block = unwrap(BB);
1125 Function::iterator I = Block;
1126 if (++I == Block->getParent()->end())
1131 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1132 BasicBlock *Block = unwrap(BB);
1133 Function::iterator I = Block;
1134 if (I == Block->getParent()->begin())
1139 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1140 return wrap(BasicBlock::Create(getGlobalContext(), Name,
1141 unwrap<Function>(FnRef)));
1144 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
1146 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
1147 return wrap(BasicBlock::Create(getGlobalContext(), Name,
1148 InsertBeforeBB->getParent(),
1152 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1153 unwrap(BBRef)->eraseFromParent();
1156 /*--.. Operations on instructions ..........................................--*/
1158 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1159 return wrap(unwrap<Instruction>(Inst)->getParent());
1162 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1163 BasicBlock *Block = unwrap(BB);
1164 BasicBlock::iterator I = Block->begin();
1165 if (I == Block->end())
1170 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1171 BasicBlock *Block = unwrap(BB);
1172 BasicBlock::iterator I = Block->end();
1173 if (I == Block->begin())
1178 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1179 Instruction *Instr = unwrap<Instruction>(Inst);
1180 BasicBlock::iterator I = Instr;
1181 if (++I == Instr->getParent()->end())
1186 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1187 Instruction *Instr = unwrap<Instruction>(Inst);
1188 BasicBlock::iterator I = Instr;
1189 if (I == Instr->getParent()->begin())
1194 /*--.. Call and invoke instructions ........................................--*/
1196 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1197 Value *V = unwrap(Instr);
1198 if (CallInst *CI = dyn_cast<CallInst>(V))
1199 return CI->getCallingConv();
1200 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1201 return II->getCallingConv();
1202 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1206 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1207 Value *V = unwrap(Instr);
1208 if (CallInst *CI = dyn_cast<CallInst>(V))
1209 return CI->setCallingConv(CC);
1210 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1211 return II->setCallingConv(CC);
1212 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1215 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1217 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1219 Call.getAttributes().addAttr(index, PA));
1222 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1224 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1226 Call.getAttributes().removeAttr(index, PA));
1229 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1231 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1233 Call.getAttributes().addAttr(index,
1234 Attribute::constructAlignmentFromInt(align)));
1237 /*--.. Operations on call instructions (only) ..............................--*/
1239 int LLVMIsTailCall(LLVMValueRef Call) {
1240 return unwrap<CallInst>(Call)->isTailCall();
1243 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1244 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1247 /*--.. Operations on phi nodes .............................................--*/
1249 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1250 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1251 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1252 for (unsigned I = 0; I != Count; ++I)
1253 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1256 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1257 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1260 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1261 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1264 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1265 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1269 /*===-- Instruction builders ----------------------------------------------===*/
1271 LLVMBuilderRef LLVMCreateBuilder(void) {
1272 return wrap(new IRBuilder<>(getGlobalContext()));
1275 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1276 LLVMValueRef Instr) {
1277 BasicBlock *BB = unwrap(Block);
1278 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1279 unwrap(Builder)->SetInsertPoint(BB, I);
1282 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1283 Instruction *I = unwrap<Instruction>(Instr);
1284 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1287 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1288 BasicBlock *BB = unwrap(Block);
1289 unwrap(Builder)->SetInsertPoint(BB);
1292 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1293 return wrap(unwrap(Builder)->GetInsertBlock());
1296 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1297 unwrap(Builder)->ClearInsertionPoint ();
1300 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1301 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1304 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1305 delete unwrap(Builder);
1308 /*--.. Instruction builders ................................................--*/
1310 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1311 return wrap(unwrap(B)->CreateRetVoid());
1314 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1315 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1318 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1319 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1322 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1323 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1324 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1327 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1328 LLVMBasicBlockRef Else, unsigned NumCases) {
1329 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1332 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1333 LLVMValueRef *Args, unsigned NumArgs,
1334 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1336 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1337 unwrap(Args), unwrap(Args) + NumArgs,
1341 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1342 return wrap(unwrap(B)->CreateUnwind());
1345 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1346 return wrap(unwrap(B)->CreateUnreachable());
1349 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1350 LLVMBasicBlockRef Dest) {
1351 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1354 /*--.. Arithmetic ..........................................................--*/
1356 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1358 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1361 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1363 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1366 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1368 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1371 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1373 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1376 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1378 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1381 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1383 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1386 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1388 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1391 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1393 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1396 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1398 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1401 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1403 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1406 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1408 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1411 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1413 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1416 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1418 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1421 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1423 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1426 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1428 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1431 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1432 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1435 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1436 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1439 /*--.. Memory ..............................................................--*/
1441 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1443 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1446 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1447 LLVMValueRef Val, const char *Name) {
1448 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1451 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1453 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1456 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1457 LLVMValueRef Val, const char *Name) {
1458 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1461 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1462 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1466 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1468 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1471 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1472 LLVMValueRef PointerVal) {
1473 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1476 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1477 LLVMValueRef *Indices, unsigned NumIndices,
1479 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1480 unwrap(Indices) + NumIndices, Name));
1483 /*--.. Casts ...............................................................--*/
1485 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1486 LLVMTypeRef DestTy, const char *Name) {
1487 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1490 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1491 LLVMTypeRef DestTy, const char *Name) {
1492 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1495 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1496 LLVMTypeRef DestTy, const char *Name) {
1497 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1500 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1501 LLVMTypeRef DestTy, const char *Name) {
1502 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1505 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1506 LLVMTypeRef DestTy, const char *Name) {
1507 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1510 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1511 LLVMTypeRef DestTy, const char *Name) {
1512 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1515 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1516 LLVMTypeRef DestTy, const char *Name) {
1517 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1520 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1521 LLVMTypeRef DestTy, const char *Name) {
1522 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1525 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1526 LLVMTypeRef DestTy, const char *Name) {
1527 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1530 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1531 LLVMTypeRef DestTy, const char *Name) {
1532 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1535 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1536 LLVMTypeRef DestTy, const char *Name) {
1537 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1540 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1541 LLVMTypeRef DestTy, const char *Name) {
1542 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1545 /*--.. Comparisons .........................................................--*/
1547 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1548 LLVMValueRef LHS, LLVMValueRef RHS,
1550 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1551 unwrap(LHS), unwrap(RHS), Name));
1554 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1555 LLVMValueRef LHS, LLVMValueRef RHS,
1557 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1558 unwrap(LHS), unwrap(RHS), Name));
1561 /*--.. Miscellaneous instructions ..........................................--*/
1563 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1564 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1567 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1568 LLVMValueRef *Args, unsigned NumArgs,
1570 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1571 unwrap(Args) + NumArgs, Name));
1574 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1575 LLVMValueRef Then, LLVMValueRef Else,
1577 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1581 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1582 LLVMTypeRef Ty, const char *Name) {
1583 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1586 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1587 LLVMValueRef Index, const char *Name) {
1588 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1592 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1593 LLVMValueRef EltVal, LLVMValueRef Index,
1595 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1596 unwrap(Index), Name));
1599 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1600 LLVMValueRef V2, LLVMValueRef Mask,
1602 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1603 unwrap(Mask), Name));
1606 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1607 unsigned Index, const char *Name) {
1608 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1611 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1612 LLVMValueRef EltVal, unsigned Index,
1614 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1619 /*===-- Module providers --------------------------------------------------===*/
1621 LLVMModuleProviderRef
1622 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1623 return wrap(new ExistingModuleProvider(unwrap(M)));
1626 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1631 /*===-- Memory buffers ----------------------------------------------------===*/
1633 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1634 LLVMMemoryBufferRef *OutMemBuf,
1635 char **OutMessage) {
1637 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1638 *OutMemBuf = wrap(MB);
1642 *OutMessage = strdup(Error.c_str());
1646 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1647 char **OutMessage) {
1648 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1649 *OutMemBuf = wrap(MB);
1653 *OutMessage = strdup("stdin is empty.");
1657 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1658 delete unwrap(MemBuf);