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) {
99 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
100 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
107 return wrap(unwrap(M)->getTypeByName(N));
110 void LLVMDumpModule(LLVMModuleRef M) {
115 /*===-- Operations on types -----------------------------------------------===*/
117 /*--.. Operations on all types (mostly) ....................................--*/
119 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
120 switch (unwrap(Ty)->getTypeID()) {
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;
152 assert (false && "Unhandled TypeID.");
156 /*--.. Operations on integer types .........................................--*/
158 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
159 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
160 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
161 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
162 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
164 LLVMTypeRef LLVMIntType(unsigned NumBits) {
165 return wrap(getGlobalContext().getIntegerType(NumBits));
168 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
169 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
172 /*--.. Operations on real types ............................................--*/
174 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
175 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
176 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
177 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
178 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
180 /*--.. Operations on function types ........................................--*/
182 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
183 LLVMTypeRef *ParamTypes, unsigned ParamCount,
185 std::vector<const Type*> Tys;
186 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
187 Tys.push_back(unwrap(*I));
189 return wrap(getGlobalContext().getFunctionType(unwrap(ReturnType), Tys,
193 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
194 return unwrap<FunctionType>(FunctionTy)->isVarArg();
197 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
198 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
201 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
202 return unwrap<FunctionType>(FunctionTy)->getNumParams();
205 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
206 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
207 for (FunctionType::param_iterator I = Ty->param_begin(),
208 E = Ty->param_end(); I != E; ++I)
212 /*--.. Operations on struct types ..........................................--*/
214 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
215 unsigned ElementCount, int Packed) {
216 std::vector<const Type*> Tys;
217 for (LLVMTypeRef *I = ElementTypes,
218 *E = ElementTypes + ElementCount; I != E; ++I)
219 Tys.push_back(unwrap(*I));
221 return wrap(getGlobalContext().getStructType(Tys, Packed != 0));
224 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
225 return unwrap<StructType>(StructTy)->getNumElements();
228 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
229 StructType *Ty = unwrap<StructType>(StructTy);
230 for (FunctionType::param_iterator I = Ty->element_begin(),
231 E = Ty->element_end(); I != E; ++I)
235 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
236 return unwrap<StructType>(StructTy)->isPacked();
239 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
241 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
242 return wrap(getGlobalContext().getArrayType(unwrap(ElementType),
246 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
247 return wrap(getGlobalContext().getPointerType(unwrap(ElementType),
251 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
252 return wrap(getGlobalContext().getVectorType(unwrap(ElementType),
256 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
257 return wrap(unwrap<SequentialType>(Ty)->getElementType());
260 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
261 return unwrap<ArrayType>(ArrayTy)->getNumElements();
264 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
265 return unwrap<PointerType>(PointerTy)->getAddressSpace();
268 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
269 return unwrap<VectorType>(VectorTy)->getNumElements();
272 /*--.. Operations on other types ...........................................--*/
274 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
275 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
277 LLVMTypeRef LLVMOpaqueType(void) {
278 return wrap(getGlobalContext().getOpaqueType());
281 /*--.. Operations on type handles ..........................................--*/
283 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
284 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
287 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
288 delete unwrap(TypeHandle);
291 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
292 return wrap(unwrap(TypeHandle)->get());
295 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
296 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
300 /*===-- Operations on values ----------------------------------------------===*/
302 /*--.. Operations on all values ............................................--*/
304 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
305 return wrap(unwrap(Val)->getType());
308 const char *LLVMGetValueName(LLVMValueRef Val) {
309 return unwrap(Val)->getNameStart();
312 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
313 unwrap(Val)->setName(Name);
316 void LLVMDumpValue(LLVMValueRef Val) {
321 /*--.. Conversion functions ................................................--*/
323 #define LLVM_DEFINE_VALUE_CAST(name) \
324 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
325 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
328 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
331 /*--.. Operations on constants of any type .................................--*/
333 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
334 return wrap(getGlobalContext().getNullValue(unwrap(Ty)));
337 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
338 return wrap(getGlobalContext().getAllOnesValue(unwrap(Ty)));
341 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
342 return wrap(getGlobalContext().getUndef(unwrap(Ty)));
345 int LLVMIsConstant(LLVMValueRef Ty) {
346 return isa<Constant>(unwrap(Ty));
349 int LLVMIsNull(LLVMValueRef Val) {
350 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
351 return C->isNullValue();
355 int LLVMIsUndef(LLVMValueRef Val) {
356 return isa<UndefValue>(unwrap(Val));
359 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
361 wrap(getGlobalContext().getConstantPointerNull(unwrap<PointerType>(Ty)));
364 /*--.. Operations on scalar constants ......................................--*/
366 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
368 return wrap(getGlobalContext().getConstantInt(unwrap<IntegerType>(IntTy), N,
372 static const fltSemantics &SemanticsForType(Type *Ty) {
373 assert(Ty->isFloatingPoint() && "Type is not floating point!");
374 if (Ty == Type::FloatTy)
375 return APFloat::IEEEsingle;
376 if (Ty == Type::DoubleTy)
377 return APFloat::IEEEdouble;
378 if (Ty == Type::X86_FP80Ty)
379 return APFloat::x87DoubleExtended;
380 if (Ty == Type::FP128Ty)
381 return APFloat::IEEEquad;
382 if (Ty == Type::PPC_FP128Ty)
383 return APFloat::PPCDoubleDouble;
384 return APFloat::Bogus;
387 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
390 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
392 return wrap(getGlobalContext().getConstantFP(APN));
395 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
396 return wrap(getGlobalContext().getConstantFP(
397 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
400 /*--.. Operations on composite constants ...................................--*/
402 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
403 int DontNullTerminate) {
404 /* Inverted the sense of AddNull because ', 0)' is a
405 better mnemonic for null termination than ', 1)'. */
406 return wrap(getGlobalContext().getConstantArray(std::string(Str, Length),
407 DontNullTerminate == 0));
410 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
411 LLVMValueRef *ConstantVals, unsigned Length) {
412 return wrap(getGlobalContext().getConstantArray(
413 getGlobalContext().getArrayType(unwrap(ElementTy), Length),
414 unwrap<Constant>(ConstantVals, Length),
418 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
420 return wrap(getGlobalContext().getConstantStruct(
421 unwrap<Constant>(ConstantVals, Count),
422 Count, Packed != 0));
425 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
426 return wrap(getGlobalContext().getConstantVector(
427 unwrap<Constant>(ScalarConstantVals, Size), Size));
430 /*--.. Constant expressions ................................................--*/
432 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
433 return wrap(getGlobalContext().getConstantExprAlignOf(unwrap(Ty)));
436 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
437 return wrap(getGlobalContext().getConstantExprSizeOf(unwrap(Ty)));
440 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
441 return wrap(getGlobalContext().getConstantExprNeg(
442 unwrap<Constant>(ConstantVal)));
445 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
446 return wrap(getGlobalContext().getConstantExprNot(
447 unwrap<Constant>(ConstantVal)));
450 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
451 return wrap(getGlobalContext().getConstantExprAdd(
452 unwrap<Constant>(LHSConstant),
453 unwrap<Constant>(RHSConstant)));
456 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
457 return wrap(getGlobalContext().getConstantExprSub(
458 unwrap<Constant>(LHSConstant),
459 unwrap<Constant>(RHSConstant)));
462 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
463 return wrap(getGlobalContext().getConstantExprMul(
464 unwrap<Constant>(LHSConstant),
465 unwrap<Constant>(RHSConstant)));
468 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
469 return wrap(getGlobalContext().getConstantExprUDiv(
470 unwrap<Constant>(LHSConstant),
471 unwrap<Constant>(RHSConstant)));
474 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
475 return wrap(getGlobalContext().getConstantExprSDiv(
476 unwrap<Constant>(LHSConstant),
477 unwrap<Constant>(RHSConstant)));
480 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
481 return wrap(getGlobalContext().getConstantExprFDiv(
482 unwrap<Constant>(LHSConstant),
483 unwrap<Constant>(RHSConstant)));
486 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
487 return wrap(getGlobalContext().getConstantExprURem(
488 unwrap<Constant>(LHSConstant),
489 unwrap<Constant>(RHSConstant)));
492 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
493 return wrap(getGlobalContext().getConstantExprSRem(
494 unwrap<Constant>(LHSConstant),
495 unwrap<Constant>(RHSConstant)));
498 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
499 return wrap(getGlobalContext().getConstantExprFRem(
500 unwrap<Constant>(LHSConstant),
501 unwrap<Constant>(RHSConstant)));
504 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
505 return wrap(getGlobalContext().getConstantExprAnd(
506 unwrap<Constant>(LHSConstant),
507 unwrap<Constant>(RHSConstant)));
510 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
511 return wrap(getGlobalContext().getConstantExprOr(
512 unwrap<Constant>(LHSConstant),
513 unwrap<Constant>(RHSConstant)));
516 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
517 return wrap(getGlobalContext().getConstantExprXor(
518 unwrap<Constant>(LHSConstant),
519 unwrap<Constant>(RHSConstant)));
522 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
523 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
524 return wrap(getGlobalContext().getConstantExprICmp(Predicate,
525 unwrap<Constant>(LHSConstant),
526 unwrap<Constant>(RHSConstant)));
529 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
530 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
531 return wrap(getGlobalContext().getConstantExprFCmp(Predicate,
532 unwrap<Constant>(LHSConstant),
533 unwrap<Constant>(RHSConstant)));
536 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
537 return wrap(getGlobalContext().getConstantExprShl(
538 unwrap<Constant>(LHSConstant),
539 unwrap<Constant>(RHSConstant)));
542 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
543 return wrap(getGlobalContext().getConstantExprLShr(
544 unwrap<Constant>(LHSConstant),
545 unwrap<Constant>(RHSConstant)));
548 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
549 return wrap(getGlobalContext().getConstantExprAShr(
550 unwrap<Constant>(LHSConstant),
551 unwrap<Constant>(RHSConstant)));
554 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
555 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
556 return wrap(getGlobalContext().getConstantExprGetElementPtr(
557 unwrap<Constant>(ConstantVal),
558 unwrap<Constant>(ConstantIndices,
563 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
564 return wrap(getGlobalContext().getConstantExprTrunc(
565 unwrap<Constant>(ConstantVal),
569 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
570 return wrap(getGlobalContext().getConstantExprSExt(
571 unwrap<Constant>(ConstantVal),
575 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
576 return wrap(getGlobalContext().getConstantExprZExt(
577 unwrap<Constant>(ConstantVal),
581 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
582 return wrap(getGlobalContext().getConstantExprFPTrunc(
583 unwrap<Constant>(ConstantVal),
587 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
588 return wrap(getGlobalContext().getConstantExprFPExtend(
589 unwrap<Constant>(ConstantVal),
593 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
594 return wrap(getGlobalContext().getConstantExprUIToFP(
595 unwrap<Constant>(ConstantVal),
599 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
600 return wrap(getGlobalContext().getConstantExprSIToFP(unwrap<Constant>(ConstantVal),
604 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
605 return wrap(getGlobalContext().getConstantExprFPToUI(unwrap<Constant>(ConstantVal),
609 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
610 return wrap(getGlobalContext().getConstantExprFPToSI(
611 unwrap<Constant>(ConstantVal),
615 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
616 return wrap(getGlobalContext().getConstantExprPtrToInt(
617 unwrap<Constant>(ConstantVal),
621 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
622 return wrap(getGlobalContext().getConstantExprIntToPtr(
623 unwrap<Constant>(ConstantVal),
627 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
628 return wrap(getGlobalContext().getConstantExprBitCast(
629 unwrap<Constant>(ConstantVal),
633 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
634 LLVMValueRef ConstantIfTrue,
635 LLVMValueRef ConstantIfFalse) {
636 return wrap(getGlobalContext().getConstantExprSelect(
637 unwrap<Constant>(ConstantCondition),
638 unwrap<Constant>(ConstantIfTrue),
639 unwrap<Constant>(ConstantIfFalse)));
642 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
643 LLVMValueRef IndexConstant) {
644 return wrap(getGlobalContext().getConstantExprExtractElement(
645 unwrap<Constant>(VectorConstant),
646 unwrap<Constant>(IndexConstant)));
649 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
650 LLVMValueRef ElementValueConstant,
651 LLVMValueRef IndexConstant) {
652 return wrap(getGlobalContext().getConstantExprInsertElement(
653 unwrap<Constant>(VectorConstant),
654 unwrap<Constant>(ElementValueConstant),
655 unwrap<Constant>(IndexConstant)));
658 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
659 LLVMValueRef VectorBConstant,
660 LLVMValueRef MaskConstant) {
661 return wrap(getGlobalContext().getConstantExprShuffleVector(
662 unwrap<Constant>(VectorAConstant),
663 unwrap<Constant>(VectorBConstant),
664 unwrap<Constant>(MaskConstant)));
667 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
669 return wrap(getGlobalContext().getConstantExprExtractValue(
670 unwrap<Constant>(AggConstant),
674 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
675 LLVMValueRef ElementValueConstant,
676 unsigned *IdxList, unsigned NumIdx) {
677 return wrap(getGlobalContext().getConstantExprInsertValue(
678 unwrap<Constant>(AggConstant),
679 unwrap<Constant>(ElementValueConstant),
683 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
684 const char *Constraints, int HasSideEffects) {
685 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
686 Constraints, HasSideEffects));
689 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
691 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
692 return wrap(unwrap<GlobalValue>(Global)->getParent());
695 int LLVMIsDeclaration(LLVMValueRef Global) {
696 return unwrap<GlobalValue>(Global)->isDeclaration();
699 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
700 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
703 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
704 unwrap<GlobalValue>(Global)
705 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
708 const char *LLVMGetSection(LLVMValueRef Global) {
709 return unwrap<GlobalValue>(Global)->getSection().c_str();
712 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
713 unwrap<GlobalValue>(Global)->setSection(Section);
716 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
717 return static_cast<LLVMVisibility>(
718 unwrap<GlobalValue>(Global)->getVisibility());
721 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
722 unwrap<GlobalValue>(Global)
723 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
726 unsigned LLVMGetAlignment(LLVMValueRef Global) {
727 return unwrap<GlobalValue>(Global)->getAlignment();
730 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
731 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
734 /*--.. Operations on global variables ......................................--*/
736 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
737 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
738 GlobalValue::ExternalLinkage, 0, Name));
741 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
742 return wrap(unwrap(M)->getNamedGlobal(Name));
745 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
746 Module *Mod = unwrap(M);
747 Module::global_iterator I = Mod->global_begin();
748 if (I == Mod->global_end())
753 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
754 Module *Mod = unwrap(M);
755 Module::global_iterator I = Mod->global_end();
756 if (I == Mod->global_begin())
761 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
762 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
763 Module::global_iterator I = GV;
764 if (++I == GV->getParent()->global_end())
769 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
770 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
771 Module::global_iterator I = GV;
772 if (I == GV->getParent()->global_begin())
777 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
778 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
781 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
782 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
785 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
786 unwrap<GlobalVariable>(GlobalVar)
787 ->setInitializer(unwrap<Constant>(ConstantVal));
790 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
791 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
794 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
795 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
798 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
799 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
802 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
803 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
806 /*--.. Operations on aliases ......................................--*/
808 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
810 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
811 unwrap<Constant>(Aliasee), unwrap (M)));
814 /*--.. Operations on functions .............................................--*/
816 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
817 LLVMTypeRef FunctionTy) {
818 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
819 GlobalValue::ExternalLinkage, Name, unwrap(M)));
822 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
823 return wrap(unwrap(M)->getFunction(Name));
826 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
827 Module *Mod = unwrap(M);
828 Module::iterator I = Mod->begin();
834 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
835 Module *Mod = unwrap(M);
836 Module::iterator I = Mod->end();
837 if (I == Mod->begin())
842 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
843 Function *Func = unwrap<Function>(Fn);
844 Module::iterator I = Func;
845 if (++I == Func->getParent()->end())
850 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
851 Function *Func = unwrap<Function>(Fn);
852 Module::iterator I = Func;
853 if (I == Func->getParent()->begin())
858 void LLVMDeleteFunction(LLVMValueRef Fn) {
859 unwrap<Function>(Fn)->eraseFromParent();
862 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
863 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
864 return F->getIntrinsicID();
868 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
869 return unwrap<Function>(Fn)->getCallingConv();
872 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
873 return unwrap<Function>(Fn)->setCallingConv(CC);
876 const char *LLVMGetGC(LLVMValueRef Fn) {
877 Function *F = unwrap<Function>(Fn);
878 return F->hasGC()? F->getGC() : 0;
881 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
882 Function *F = unwrap<Function>(Fn);
889 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
890 Function *Func = unwrap<Function>(Fn);
891 const AttrListPtr PAL = Func->getAttributes();
892 const AttrListPtr PALnew = PAL.addAttr(0, PA);
893 Func->setAttributes(PALnew);
896 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
897 Function *Func = unwrap<Function>(Fn);
898 const AttrListPtr PAL = Func->getAttributes();
899 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
900 Func->setAttributes(PALnew);
903 /*--.. Operations on parameters ............................................--*/
905 unsigned LLVMCountParams(LLVMValueRef FnRef) {
906 // This function is strictly redundant to
907 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
908 return unwrap<Function>(FnRef)->arg_size();
911 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
912 Function *Fn = unwrap<Function>(FnRef);
913 for (Function::arg_iterator I = Fn->arg_begin(),
914 E = Fn->arg_end(); I != E; I++)
915 *ParamRefs++ = wrap(I);
918 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
919 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
925 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
926 return wrap(unwrap<Argument>(V)->getParent());
929 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
930 Function *Func = unwrap<Function>(Fn);
931 Function::arg_iterator I = Func->arg_begin();
932 if (I == Func->arg_end())
937 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
938 Function *Func = unwrap<Function>(Fn);
939 Function::arg_iterator I = Func->arg_end();
940 if (I == Func->arg_begin())
945 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
946 Argument *A = unwrap<Argument>(Arg);
947 Function::arg_iterator I = A;
948 if (++I == A->getParent()->arg_end())
953 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
954 Argument *A = unwrap<Argument>(Arg);
955 Function::arg_iterator I = A;
956 if (I == A->getParent()->arg_begin())
961 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
962 unwrap<Argument>(Arg)->addAttr(PA);
965 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
966 unwrap<Argument>(Arg)->removeAttr(PA);
969 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
970 unwrap<Argument>(Arg)->addAttr(
971 Attribute::constructAlignmentFromInt(align));
974 /*--.. Operations on basic blocks ..........................................--*/
976 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
977 return wrap(static_cast<Value*>(unwrap(BB)));
980 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
981 return isa<BasicBlock>(unwrap(Val));
984 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
985 return wrap(unwrap<BasicBlock>(Val));
988 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
989 return wrap(unwrap(BB)->getParent());
992 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
993 return unwrap<Function>(FnRef)->size();
996 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
997 Function *Fn = unwrap<Function>(FnRef);
998 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
999 *BasicBlocksRefs++ = wrap(I);
1002 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1003 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1006 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1007 Function *Func = unwrap<Function>(Fn);
1008 Function::iterator I = Func->begin();
1009 if (I == Func->end())
1014 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1015 Function *Func = unwrap<Function>(Fn);
1016 Function::iterator I = Func->end();
1017 if (I == Func->begin())
1022 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1023 BasicBlock *Block = unwrap(BB);
1024 Function::iterator I = Block;
1025 if (++I == Block->getParent()->end())
1030 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1031 BasicBlock *Block = unwrap(BB);
1032 Function::iterator I = Block;
1033 if (I == Block->getParent()->begin())
1038 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1039 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
1042 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
1044 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
1045 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
1049 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1050 unwrap(BBRef)->eraseFromParent();
1053 /*--.. Operations on instructions ..........................................--*/
1055 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1056 return wrap(unwrap<Instruction>(Inst)->getParent());
1059 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1060 BasicBlock *Block = unwrap(BB);
1061 BasicBlock::iterator I = Block->begin();
1062 if (I == Block->end())
1067 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1068 BasicBlock *Block = unwrap(BB);
1069 BasicBlock::iterator I = Block->end();
1070 if (I == Block->begin())
1075 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1076 Instruction *Instr = unwrap<Instruction>(Inst);
1077 BasicBlock::iterator I = Instr;
1078 if (++I == Instr->getParent()->end())
1083 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1084 Instruction *Instr = unwrap<Instruction>(Inst);
1085 BasicBlock::iterator I = Instr;
1086 if (I == Instr->getParent()->begin())
1091 /*--.. Call and invoke instructions ........................................--*/
1093 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1094 Value *V = unwrap(Instr);
1095 if (CallInst *CI = dyn_cast<CallInst>(V))
1096 return CI->getCallingConv();
1097 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1098 return II->getCallingConv();
1099 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1103 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1104 Value *V = unwrap(Instr);
1105 if (CallInst *CI = dyn_cast<CallInst>(V))
1106 return CI->setCallingConv(CC);
1107 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1108 return II->setCallingConv(CC);
1109 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1112 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1114 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1116 Call.getAttributes().addAttr(index, PA));
1119 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1121 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1123 Call.getAttributes().removeAttr(index, PA));
1126 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1128 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1130 Call.getAttributes().addAttr(index,
1131 Attribute::constructAlignmentFromInt(align)));
1134 /*--.. Operations on call instructions (only) ..............................--*/
1136 int LLVMIsTailCall(LLVMValueRef Call) {
1137 return unwrap<CallInst>(Call)->isTailCall();
1140 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1141 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1144 /*--.. Operations on phi nodes .............................................--*/
1146 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1147 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1148 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1149 for (unsigned I = 0; I != Count; ++I)
1150 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1153 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1154 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1157 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1158 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1161 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1162 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1166 /*===-- Instruction builders ----------------------------------------------===*/
1168 LLVMBuilderRef LLVMCreateBuilder(void) {
1169 return wrap(new IRBuilder<>(getGlobalContext()));
1172 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1173 LLVMValueRef Instr) {
1174 BasicBlock *BB = unwrap(Block);
1175 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1176 unwrap(Builder)->SetInsertPoint(BB, I);
1179 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1180 Instruction *I = unwrap<Instruction>(Instr);
1181 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1184 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1185 BasicBlock *BB = unwrap(Block);
1186 unwrap(Builder)->SetInsertPoint(BB);
1189 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1190 return wrap(unwrap(Builder)->GetInsertBlock());
1193 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1194 unwrap(Builder)->ClearInsertionPoint ();
1197 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1198 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1201 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1202 delete unwrap(Builder);
1205 /*--.. Instruction builders ................................................--*/
1207 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1208 return wrap(unwrap(B)->CreateRetVoid());
1211 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1212 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1215 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1216 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1219 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1220 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1221 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1224 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1225 LLVMBasicBlockRef Else, unsigned NumCases) {
1226 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1229 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1230 LLVMValueRef *Args, unsigned NumArgs,
1231 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1233 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1234 unwrap(Args), unwrap(Args) + NumArgs,
1238 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1239 return wrap(unwrap(B)->CreateUnwind());
1242 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1243 return wrap(unwrap(B)->CreateUnreachable());
1246 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1247 LLVMBasicBlockRef Dest) {
1248 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1251 /*--.. Arithmetic ..........................................................--*/
1253 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1255 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1258 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1260 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1263 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1265 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1268 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1270 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1273 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1275 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1278 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1280 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1283 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1285 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1288 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1290 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1293 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1295 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1298 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1300 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1303 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1305 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1308 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1310 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1313 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1315 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1318 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1320 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1323 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1325 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1328 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1329 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1332 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1333 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1336 /*--.. Memory ..............................................................--*/
1338 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1340 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1343 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1344 LLVMValueRef Val, const char *Name) {
1345 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1348 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1350 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1353 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1354 LLVMValueRef Val, const char *Name) {
1355 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1358 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1359 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1363 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1365 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1368 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1369 LLVMValueRef PointerVal) {
1370 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1373 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1374 LLVMValueRef *Indices, unsigned NumIndices,
1376 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1377 unwrap(Indices) + NumIndices, Name));
1380 /*--.. Casts ...............................................................--*/
1382 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1383 LLVMTypeRef DestTy, const char *Name) {
1384 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1387 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1388 LLVMTypeRef DestTy, const char *Name) {
1389 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1392 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1393 LLVMTypeRef DestTy, const char *Name) {
1394 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1397 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1398 LLVMTypeRef DestTy, const char *Name) {
1399 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1402 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1403 LLVMTypeRef DestTy, const char *Name) {
1404 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1407 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1408 LLVMTypeRef DestTy, const char *Name) {
1409 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1412 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1413 LLVMTypeRef DestTy, const char *Name) {
1414 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1417 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1418 LLVMTypeRef DestTy, const char *Name) {
1419 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1422 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1423 LLVMTypeRef DestTy, const char *Name) {
1424 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1427 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1428 LLVMTypeRef DestTy, const char *Name) {
1429 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1432 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1433 LLVMTypeRef DestTy, const char *Name) {
1434 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1437 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1438 LLVMTypeRef DestTy, const char *Name) {
1439 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1442 /*--.. Comparisons .........................................................--*/
1444 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1445 LLVMValueRef LHS, LLVMValueRef RHS,
1447 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1448 unwrap(LHS), unwrap(RHS), Name));
1451 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1452 LLVMValueRef LHS, LLVMValueRef RHS,
1454 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1455 unwrap(LHS), unwrap(RHS), Name));
1458 /*--.. Miscellaneous instructions ..........................................--*/
1460 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1461 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1464 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1465 LLVMValueRef *Args, unsigned NumArgs,
1467 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1468 unwrap(Args) + NumArgs, Name));
1471 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1472 LLVMValueRef Then, LLVMValueRef Else,
1474 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1478 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1479 LLVMTypeRef Ty, const char *Name) {
1480 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1483 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1484 LLVMValueRef Index, const char *Name) {
1485 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1489 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1490 LLVMValueRef EltVal, LLVMValueRef Index,
1492 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1493 unwrap(Index), Name));
1496 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1497 LLVMValueRef V2, LLVMValueRef Mask,
1499 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1500 unwrap(Mask), Name));
1503 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1504 unsigned Index, const char *Name) {
1505 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1508 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1509 LLVMValueRef EltVal, unsigned Index,
1511 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1516 /*===-- Module providers --------------------------------------------------===*/
1518 LLVMModuleProviderRef
1519 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1520 return wrap(new ExistingModuleProvider(unwrap(M)));
1523 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1528 /*===-- Memory buffers ----------------------------------------------------===*/
1530 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1531 LLVMMemoryBufferRef *OutMemBuf,
1532 char **OutMessage) {
1534 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1535 *OutMemBuf = wrap(MB);
1539 *OutMessage = strdup(Error.c_str());
1543 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1544 char **OutMessage) {
1545 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1546 *OutMemBuf = wrap(MB);
1550 *OutMessage = strdup("stdin is empty.");
1554 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1555 delete unwrap(MemBuf);