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) { return (LLVMTypeRef) Type::Int1Ty; }
157 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
158 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
159 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
160 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
162 LLVMTypeRef LLVMIntType(unsigned NumBits) {
163 return wrap(IntegerType::get(NumBits));
166 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
167 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
170 /*--.. Operations on real types ............................................--*/
172 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
173 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
174 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
175 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
176 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
178 /*--.. Operations on function types ........................................--*/
180 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
181 LLVMTypeRef *ParamTypes, unsigned ParamCount,
183 std::vector<const Type*> Tys;
184 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
185 Tys.push_back(unwrap(*I));
187 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
190 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
191 return unwrap<FunctionType>(FunctionTy)->isVarArg();
194 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
195 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
198 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
199 return unwrap<FunctionType>(FunctionTy)->getNumParams();
202 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
203 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
204 for (FunctionType::param_iterator I = Ty->param_begin(),
205 E = Ty->param_end(); I != E; ++I)
209 /*--.. Operations on struct types ..........................................--*/
211 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
212 unsigned ElementCount, int Packed) {
213 std::vector<const Type*> Tys;
214 for (LLVMTypeRef *I = ElementTypes,
215 *E = ElementTypes + ElementCount; I != E; ++I)
216 Tys.push_back(unwrap(*I));
218 return wrap(StructType::get(getGlobalContext(), Tys, Packed != 0));
221 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
222 return unwrap<StructType>(StructTy)->getNumElements();
225 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
226 StructType *Ty = unwrap<StructType>(StructTy);
227 for (FunctionType::param_iterator I = Ty->element_begin(),
228 E = Ty->element_end(); I != E; ++I)
232 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
233 return unwrap<StructType>(StructTy)->isPacked();
236 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
238 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
239 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
242 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
243 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
246 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
247 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
250 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
251 return wrap(unwrap<SequentialType>(Ty)->getElementType());
254 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
255 return unwrap<ArrayType>(ArrayTy)->getNumElements();
258 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
259 return unwrap<PointerType>(PointerTy)->getAddressSpace();
262 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
263 return unwrap<VectorType>(VectorTy)->getNumElements();
266 /*--.. Operations on other types ...........................................--*/
268 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
269 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
271 LLVMTypeRef LLVMOpaqueType(void) {
272 return wrap(OpaqueType::get());
275 /*--.. Operations on type handles ..........................................--*/
277 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
278 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
281 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
282 delete unwrap(TypeHandle);
285 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
286 return wrap(unwrap(TypeHandle)->get());
289 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
290 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
294 /*===-- Operations on values ----------------------------------------------===*/
296 /*--.. Operations on all values ............................................--*/
298 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
299 return wrap(unwrap(Val)->getType());
302 const char *LLVMGetValueName(LLVMValueRef Val) {
303 return unwrap(Val)->getName().data();
306 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
307 unwrap(Val)->setName(Name);
310 void LLVMDumpValue(LLVMValueRef Val) {
315 /*--.. Conversion functions ................................................--*/
317 #define LLVM_DEFINE_VALUE_CAST(name) \
318 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
319 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
322 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
325 /*--.. Operations on constants of any type .................................--*/
327 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
328 return wrap(Constant::getNullValue(unwrap(Ty)));
331 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
332 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
335 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
336 return wrap(UndefValue::get(unwrap(Ty)));
339 int LLVMIsConstant(LLVMValueRef Ty) {
340 return isa<Constant>(unwrap(Ty));
343 int LLVMIsNull(LLVMValueRef Val) {
344 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
345 return C->isNullValue();
349 int LLVMIsUndef(LLVMValueRef Val) {
350 return isa<UndefValue>(unwrap(Val));
353 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
355 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
358 /*--.. Operations on scalar constants ......................................--*/
360 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
362 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
365 static const fltSemantics &SemanticsForType(Type *Ty) {
366 assert(Ty->isFloatingPoint() && "Type is not floating point!");
367 if (Ty == Type::FloatTy)
368 return APFloat::IEEEsingle;
369 if (Ty == Type::DoubleTy)
370 return APFloat::IEEEdouble;
371 if (Ty == Type::X86_FP80Ty)
372 return APFloat::x87DoubleExtended;
373 if (Ty == Type::FP128Ty)
374 return APFloat::IEEEquad;
375 if (Ty == Type::PPC_FP128Ty)
376 return APFloat::PPCDoubleDouble;
377 return APFloat::Bogus;
380 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
383 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
385 return wrap(ConstantFP::get(getGlobalContext(), APN));
388 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
389 return wrap(ConstantFP::get(getGlobalContext(),
390 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
393 /*--.. Operations on composite constants ...................................--*/
395 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
396 int DontNullTerminate) {
397 /* Inverted the sense of AddNull because ', 0)' is a
398 better mnemonic for null termination than ', 1)'. */
399 return wrap(ConstantArray::get(std::string(Str, Length),
400 DontNullTerminate == 0));
403 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
404 LLVMValueRef *ConstantVals, unsigned Length) {
405 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
406 unwrap<Constant>(ConstantVals, Length),
410 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
412 return wrap(ConstantStruct::get(getGlobalContext(),
413 unwrap<Constant>(ConstantVals, Count),
414 Count, Packed != 0));
417 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
418 return wrap(ConstantVector::get(
419 unwrap<Constant>(ScalarConstantVals, Size), Size));
422 /*--.. Constant expressions ................................................--*/
424 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
425 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
428 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
429 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
432 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
433 return wrap(ConstantExpr::getNeg(
434 unwrap<Constant>(ConstantVal)));
437 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
438 return wrap(ConstantExpr::getNot(
439 unwrap<Constant>(ConstantVal)));
442 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
443 return wrap(ConstantExpr::getAdd(
444 unwrap<Constant>(LHSConstant),
445 unwrap<Constant>(RHSConstant)));
448 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
449 return wrap(ConstantExpr::getSub(
450 unwrap<Constant>(LHSConstant),
451 unwrap<Constant>(RHSConstant)));
454 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
455 return wrap(ConstantExpr::getMul(
456 unwrap<Constant>(LHSConstant),
457 unwrap<Constant>(RHSConstant)));
460 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
461 return wrap(ConstantExpr::getUDiv(
462 unwrap<Constant>(LHSConstant),
463 unwrap<Constant>(RHSConstant)));
466 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
467 return wrap(ConstantExpr::getSDiv(
468 unwrap<Constant>(LHSConstant),
469 unwrap<Constant>(RHSConstant)));
472 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
473 return wrap(ConstantExpr::getFDiv(
474 unwrap<Constant>(LHSConstant),
475 unwrap<Constant>(RHSConstant)));
478 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
479 return wrap(ConstantExpr::getURem(
480 unwrap<Constant>(LHSConstant),
481 unwrap<Constant>(RHSConstant)));
484 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
485 return wrap(ConstantExpr::getSRem(
486 unwrap<Constant>(LHSConstant),
487 unwrap<Constant>(RHSConstant)));
490 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
491 return wrap(ConstantExpr::getFRem(
492 unwrap<Constant>(LHSConstant),
493 unwrap<Constant>(RHSConstant)));
496 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
497 return wrap(ConstantExpr::getAnd(
498 unwrap<Constant>(LHSConstant),
499 unwrap<Constant>(RHSConstant)));
502 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
503 return wrap(ConstantExpr::getOr(
504 unwrap<Constant>(LHSConstant),
505 unwrap<Constant>(RHSConstant)));
508 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
509 return wrap(ConstantExpr::getXor(
510 unwrap<Constant>(LHSConstant),
511 unwrap<Constant>(RHSConstant)));
514 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
515 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
516 return wrap(ConstantExpr::getICmp(Predicate,
517 unwrap<Constant>(LHSConstant),
518 unwrap<Constant>(RHSConstant)));
521 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
522 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
523 return wrap(ConstantExpr::getFCmp(Predicate,
524 unwrap<Constant>(LHSConstant),
525 unwrap<Constant>(RHSConstant)));
528 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
529 return wrap(ConstantExpr::getShl(
530 unwrap<Constant>(LHSConstant),
531 unwrap<Constant>(RHSConstant)));
534 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
535 return wrap(ConstantExpr::getLShr(
536 unwrap<Constant>(LHSConstant),
537 unwrap<Constant>(RHSConstant)));
540 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
541 return wrap(ConstantExpr::getAShr(
542 unwrap<Constant>(LHSConstant),
543 unwrap<Constant>(RHSConstant)));
546 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
547 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
548 return wrap(ConstantExpr::getGetElementPtr(
549 unwrap<Constant>(ConstantVal),
550 unwrap<Constant>(ConstantIndices,
555 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
556 return wrap(ConstantExpr::getTrunc(
557 unwrap<Constant>(ConstantVal),
561 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
562 return wrap(ConstantExpr::getSExt(
563 unwrap<Constant>(ConstantVal),
567 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
568 return wrap(ConstantExpr::getZExt(
569 unwrap<Constant>(ConstantVal),
573 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
574 return wrap(ConstantExpr::getFPTrunc(
575 unwrap<Constant>(ConstantVal),
579 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
580 return wrap(ConstantExpr::getFPExtend(
581 unwrap<Constant>(ConstantVal),
585 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
586 return wrap(ConstantExpr::getUIToFP(
587 unwrap<Constant>(ConstantVal),
591 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
592 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
596 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
597 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
601 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
602 return wrap(ConstantExpr::getFPToSI(
603 unwrap<Constant>(ConstantVal),
607 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
608 return wrap(ConstantExpr::getPtrToInt(
609 unwrap<Constant>(ConstantVal),
613 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
614 return wrap(ConstantExpr::getIntToPtr(
615 unwrap<Constant>(ConstantVal),
619 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
620 return wrap(ConstantExpr::getBitCast(
621 unwrap<Constant>(ConstantVal),
625 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
626 LLVMValueRef ConstantIfTrue,
627 LLVMValueRef ConstantIfFalse) {
628 return wrap(ConstantExpr::getSelect(
629 unwrap<Constant>(ConstantCondition),
630 unwrap<Constant>(ConstantIfTrue),
631 unwrap<Constant>(ConstantIfFalse)));
634 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
635 LLVMValueRef IndexConstant) {
636 return wrap(ConstantExpr::getExtractElement(
637 unwrap<Constant>(VectorConstant),
638 unwrap<Constant>(IndexConstant)));
641 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
642 LLVMValueRef ElementValueConstant,
643 LLVMValueRef IndexConstant) {
644 return wrap(ConstantExpr::getInsertElement(
645 unwrap<Constant>(VectorConstant),
646 unwrap<Constant>(ElementValueConstant),
647 unwrap<Constant>(IndexConstant)));
650 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
651 LLVMValueRef VectorBConstant,
652 LLVMValueRef MaskConstant) {
653 return wrap(ConstantExpr::getShuffleVector(
654 unwrap<Constant>(VectorAConstant),
655 unwrap<Constant>(VectorBConstant),
656 unwrap<Constant>(MaskConstant)));
659 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
661 return wrap(ConstantExpr::getExtractValue(
662 unwrap<Constant>(AggConstant),
666 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
667 LLVMValueRef ElementValueConstant,
668 unsigned *IdxList, unsigned NumIdx) {
669 return wrap(ConstantExpr::getInsertValue(
670 unwrap<Constant>(AggConstant),
671 unwrap<Constant>(ElementValueConstant),
675 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
676 const char *Constraints, int HasSideEffects) {
677 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
678 Constraints, HasSideEffects));
681 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
683 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
684 return wrap(unwrap<GlobalValue>(Global)->getParent());
687 int LLVMIsDeclaration(LLVMValueRef Global) {
688 return unwrap<GlobalValue>(Global)->isDeclaration();
691 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
692 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
694 assert(false && "Unhandled Linkage Type.");
695 case GlobalValue::ExternalLinkage:
696 return LLVMExternalLinkage;
697 case GlobalValue::AvailableExternallyLinkage:
698 return LLVMAvailableExternallyLinkage;
699 case GlobalValue::LinkOnceAnyLinkage:
700 return LLVMLinkOnceAnyLinkage;
701 case GlobalValue::LinkOnceODRLinkage:
702 return LLVMLinkOnceODRLinkage;
703 case GlobalValue::WeakAnyLinkage:
704 return LLVMWeakAnyLinkage;
705 case GlobalValue::WeakODRLinkage:
706 return LLVMWeakODRLinkage;
707 case GlobalValue::AppendingLinkage:
708 return LLVMAppendingLinkage;
709 case GlobalValue::InternalLinkage:
710 return LLVMInternalLinkage;
711 case GlobalValue::PrivateLinkage:
712 return LLVMPrivateLinkage;
713 case GlobalValue::LinkerPrivateLinkage:
714 return LLVMLinkerPrivateLinkage;
715 case GlobalValue::DLLImportLinkage:
716 return LLVMDLLImportLinkage;
717 case GlobalValue::DLLExportLinkage:
718 return LLVMDLLExportLinkage;
719 case GlobalValue::ExternalWeakLinkage:
720 return LLVMExternalWeakLinkage;
721 case GlobalValue::GhostLinkage:
722 return LLVMGhostLinkage;
723 case GlobalValue::CommonLinkage:
724 return LLVMCommonLinkage;
727 // Should never get here.
728 return static_cast<LLVMLinkage>(0);
731 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
732 GlobalValue *GV = unwrap<GlobalValue>(Global);
736 assert(false && "Unhandled Linkage Type.");
737 case LLVMExternalLinkage:
738 GV->setLinkage(GlobalValue::ExternalLinkage);
740 case LLVMAvailableExternallyLinkage:
741 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
743 case LLVMLinkOnceAnyLinkage:
744 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
746 case LLVMLinkOnceODRLinkage:
747 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
749 case LLVMWeakAnyLinkage:
750 GV->setLinkage(GlobalValue::WeakAnyLinkage);
752 case LLVMWeakODRLinkage:
753 GV->setLinkage(GlobalValue::WeakODRLinkage);
755 case LLVMAppendingLinkage:
756 GV->setLinkage(GlobalValue::AppendingLinkage);
758 case LLVMInternalLinkage:
759 GV->setLinkage(GlobalValue::InternalLinkage);
761 case LLVMPrivateLinkage:
762 GV->setLinkage(GlobalValue::PrivateLinkage);
764 case LLVMLinkerPrivateLinkage:
765 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
767 case LLVMDLLImportLinkage:
768 GV->setLinkage(GlobalValue::DLLImportLinkage);
770 case LLVMDLLExportLinkage:
771 GV->setLinkage(GlobalValue::DLLExportLinkage);
773 case LLVMExternalWeakLinkage:
774 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
776 case LLVMGhostLinkage:
777 GV->setLinkage(GlobalValue::GhostLinkage);
779 case LLVMCommonLinkage:
780 GV->setLinkage(GlobalValue::CommonLinkage);
785 const char *LLVMGetSection(LLVMValueRef Global) {
786 return unwrap<GlobalValue>(Global)->getSection().c_str();
789 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
790 unwrap<GlobalValue>(Global)->setSection(Section);
793 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
794 return static_cast<LLVMVisibility>(
795 unwrap<GlobalValue>(Global)->getVisibility());
798 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
799 unwrap<GlobalValue>(Global)
800 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
803 unsigned LLVMGetAlignment(LLVMValueRef Global) {
804 return unwrap<GlobalValue>(Global)->getAlignment();
807 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
808 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
811 /*--.. Operations on global variables ......................................--*/
813 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
814 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
815 GlobalValue::ExternalLinkage, 0, Name));
818 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
819 return wrap(unwrap(M)->getNamedGlobal(Name));
822 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
823 Module *Mod = unwrap(M);
824 Module::global_iterator I = Mod->global_begin();
825 if (I == Mod->global_end())
830 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
831 Module *Mod = unwrap(M);
832 Module::global_iterator I = Mod->global_end();
833 if (I == Mod->global_begin())
838 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
839 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
840 Module::global_iterator I = GV;
841 if (++I == GV->getParent()->global_end())
846 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
847 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
848 Module::global_iterator I = GV;
849 if (I == GV->getParent()->global_begin())
854 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
855 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
858 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
859 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
862 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
863 unwrap<GlobalVariable>(GlobalVar)
864 ->setInitializer(unwrap<Constant>(ConstantVal));
867 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
868 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
871 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
872 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
875 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
876 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
879 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
880 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
883 /*--.. Operations on aliases ......................................--*/
885 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
887 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
888 unwrap<Constant>(Aliasee), unwrap (M)));
891 /*--.. Operations on functions .............................................--*/
893 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
894 LLVMTypeRef FunctionTy) {
895 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
896 GlobalValue::ExternalLinkage, Name, unwrap(M)));
899 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
900 return wrap(unwrap(M)->getFunction(Name));
903 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
904 Module *Mod = unwrap(M);
905 Module::iterator I = Mod->begin();
911 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
912 Module *Mod = unwrap(M);
913 Module::iterator I = Mod->end();
914 if (I == Mod->begin())
919 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
920 Function *Func = unwrap<Function>(Fn);
921 Module::iterator I = Func;
922 if (++I == Func->getParent()->end())
927 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
928 Function *Func = unwrap<Function>(Fn);
929 Module::iterator I = Func;
930 if (I == Func->getParent()->begin())
935 void LLVMDeleteFunction(LLVMValueRef Fn) {
936 unwrap<Function>(Fn)->eraseFromParent();
939 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
940 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
941 return F->getIntrinsicID();
945 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
946 return unwrap<Function>(Fn)->getCallingConv();
949 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
950 return unwrap<Function>(Fn)->setCallingConv(CC);
953 const char *LLVMGetGC(LLVMValueRef Fn) {
954 Function *F = unwrap<Function>(Fn);
955 return F->hasGC()? F->getGC() : 0;
958 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
959 Function *F = unwrap<Function>(Fn);
966 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
967 Function *Func = unwrap<Function>(Fn);
968 const AttrListPtr PAL = Func->getAttributes();
969 const AttrListPtr PALnew = PAL.addAttr(0, PA);
970 Func->setAttributes(PALnew);
973 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
974 Function *Func = unwrap<Function>(Fn);
975 const AttrListPtr PAL = Func->getAttributes();
976 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
977 Func->setAttributes(PALnew);
980 /*--.. Operations on parameters ............................................--*/
982 unsigned LLVMCountParams(LLVMValueRef FnRef) {
983 // This function is strictly redundant to
984 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
985 return unwrap<Function>(FnRef)->arg_size();
988 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
989 Function *Fn = unwrap<Function>(FnRef);
990 for (Function::arg_iterator I = Fn->arg_begin(),
991 E = Fn->arg_end(); I != E; I++)
992 *ParamRefs++ = wrap(I);
995 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
996 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1002 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1003 return wrap(unwrap<Argument>(V)->getParent());
1006 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1007 Function *Func = unwrap<Function>(Fn);
1008 Function::arg_iterator I = Func->arg_begin();
1009 if (I == Func->arg_end())
1014 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1015 Function *Func = unwrap<Function>(Fn);
1016 Function::arg_iterator I = Func->arg_end();
1017 if (I == Func->arg_begin())
1022 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1023 Argument *A = unwrap<Argument>(Arg);
1024 Function::arg_iterator I = A;
1025 if (++I == A->getParent()->arg_end())
1030 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1031 Argument *A = unwrap<Argument>(Arg);
1032 Function::arg_iterator I = A;
1033 if (I == A->getParent()->arg_begin())
1038 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1039 unwrap<Argument>(Arg)->addAttr(PA);
1042 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1043 unwrap<Argument>(Arg)->removeAttr(PA);
1046 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1047 unwrap<Argument>(Arg)->addAttr(
1048 Attribute::constructAlignmentFromInt(align));
1051 /*--.. Operations on basic blocks ..........................................--*/
1053 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1054 return wrap(static_cast<Value*>(unwrap(BB)));
1057 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
1058 return isa<BasicBlock>(unwrap(Val));
1061 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1062 return wrap(unwrap<BasicBlock>(Val));
1065 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1066 return wrap(unwrap(BB)->getParent());
1069 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1070 return unwrap<Function>(FnRef)->size();
1073 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1074 Function *Fn = unwrap<Function>(FnRef);
1075 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1076 *BasicBlocksRefs++ = wrap(I);
1079 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1080 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1083 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1084 Function *Func = unwrap<Function>(Fn);
1085 Function::iterator I = Func->begin();
1086 if (I == Func->end())
1091 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1092 Function *Func = unwrap<Function>(Fn);
1093 Function::iterator I = Func->end();
1094 if (I == Func->begin())
1099 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1100 BasicBlock *Block = unwrap(BB);
1101 Function::iterator I = Block;
1102 if (++I == Block->getParent()->end())
1107 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1108 BasicBlock *Block = unwrap(BB);
1109 Function::iterator I = Block;
1110 if (I == Block->getParent()->begin())
1115 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1116 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
1119 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
1121 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
1122 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
1126 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1127 unwrap(BBRef)->eraseFromParent();
1130 /*--.. Operations on instructions ..........................................--*/
1132 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1133 return wrap(unwrap<Instruction>(Inst)->getParent());
1136 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1137 BasicBlock *Block = unwrap(BB);
1138 BasicBlock::iterator I = Block->begin();
1139 if (I == Block->end())
1144 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1145 BasicBlock *Block = unwrap(BB);
1146 BasicBlock::iterator I = Block->end();
1147 if (I == Block->begin())
1152 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1153 Instruction *Instr = unwrap<Instruction>(Inst);
1154 BasicBlock::iterator I = Instr;
1155 if (++I == Instr->getParent()->end())
1160 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1161 Instruction *Instr = unwrap<Instruction>(Inst);
1162 BasicBlock::iterator I = Instr;
1163 if (I == Instr->getParent()->begin())
1168 /*--.. Call and invoke instructions ........................................--*/
1170 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1171 Value *V = unwrap(Instr);
1172 if (CallInst *CI = dyn_cast<CallInst>(V))
1173 return CI->getCallingConv();
1174 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1175 return II->getCallingConv();
1176 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1180 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1181 Value *V = unwrap(Instr);
1182 if (CallInst *CI = dyn_cast<CallInst>(V))
1183 return CI->setCallingConv(CC);
1184 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1185 return II->setCallingConv(CC);
1186 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1189 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1191 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1193 Call.getAttributes().addAttr(index, PA));
1196 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1198 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1200 Call.getAttributes().removeAttr(index, PA));
1203 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1205 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1207 Call.getAttributes().addAttr(index,
1208 Attribute::constructAlignmentFromInt(align)));
1211 /*--.. Operations on call instructions (only) ..............................--*/
1213 int LLVMIsTailCall(LLVMValueRef Call) {
1214 return unwrap<CallInst>(Call)->isTailCall();
1217 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1218 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1221 /*--.. Operations on phi nodes .............................................--*/
1223 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1224 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1225 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1226 for (unsigned I = 0; I != Count; ++I)
1227 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1230 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1231 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1234 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1235 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1238 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1239 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1243 /*===-- Instruction builders ----------------------------------------------===*/
1245 LLVMBuilderRef LLVMCreateBuilder(void) {
1246 return wrap(new IRBuilder<>(getGlobalContext()));
1249 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1250 LLVMValueRef Instr) {
1251 BasicBlock *BB = unwrap(Block);
1252 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1253 unwrap(Builder)->SetInsertPoint(BB, I);
1256 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1257 Instruction *I = unwrap<Instruction>(Instr);
1258 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1261 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1262 BasicBlock *BB = unwrap(Block);
1263 unwrap(Builder)->SetInsertPoint(BB);
1266 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1267 return wrap(unwrap(Builder)->GetInsertBlock());
1270 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1271 unwrap(Builder)->ClearInsertionPoint ();
1274 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1275 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1278 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1279 delete unwrap(Builder);
1282 /*--.. Instruction builders ................................................--*/
1284 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1285 return wrap(unwrap(B)->CreateRetVoid());
1288 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1289 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1292 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1293 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1296 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1297 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1298 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1301 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1302 LLVMBasicBlockRef Else, unsigned NumCases) {
1303 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1306 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1307 LLVMValueRef *Args, unsigned NumArgs,
1308 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1310 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1311 unwrap(Args), unwrap(Args) + NumArgs,
1315 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1316 return wrap(unwrap(B)->CreateUnwind());
1319 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1320 return wrap(unwrap(B)->CreateUnreachable());
1323 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1324 LLVMBasicBlockRef Dest) {
1325 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1328 /*--.. Arithmetic ..........................................................--*/
1330 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1332 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1335 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1337 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1340 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1342 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1345 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1347 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1350 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1352 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1355 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1357 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1360 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1362 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1365 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1367 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1370 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1372 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1375 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1377 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1380 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1382 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1385 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1387 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1390 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1392 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1395 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1397 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1400 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1402 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1405 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1406 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1409 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1410 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1413 /*--.. Memory ..............................................................--*/
1415 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1417 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1420 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1421 LLVMValueRef Val, const char *Name) {
1422 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1425 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1427 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1430 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1431 LLVMValueRef Val, const char *Name) {
1432 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1435 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1436 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1440 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1442 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1445 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1446 LLVMValueRef PointerVal) {
1447 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1450 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1451 LLVMValueRef *Indices, unsigned NumIndices,
1453 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1454 unwrap(Indices) + NumIndices, Name));
1457 /*--.. Casts ...............................................................--*/
1459 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1460 LLVMTypeRef DestTy, const char *Name) {
1461 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1464 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1465 LLVMTypeRef DestTy, const char *Name) {
1466 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1469 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1470 LLVMTypeRef DestTy, const char *Name) {
1471 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1474 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1475 LLVMTypeRef DestTy, const char *Name) {
1476 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1479 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1480 LLVMTypeRef DestTy, const char *Name) {
1481 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1484 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1485 LLVMTypeRef DestTy, const char *Name) {
1486 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1489 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1490 LLVMTypeRef DestTy, const char *Name) {
1491 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1494 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1495 LLVMTypeRef DestTy, const char *Name) {
1496 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1499 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1500 LLVMTypeRef DestTy, const char *Name) {
1501 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1504 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1505 LLVMTypeRef DestTy, const char *Name) {
1506 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1509 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1510 LLVMTypeRef DestTy, const char *Name) {
1511 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1514 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1515 LLVMTypeRef DestTy, const char *Name) {
1516 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1519 /*--.. Comparisons .........................................................--*/
1521 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1522 LLVMValueRef LHS, LLVMValueRef RHS,
1524 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1525 unwrap(LHS), unwrap(RHS), Name));
1528 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1529 LLVMValueRef LHS, LLVMValueRef RHS,
1531 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1532 unwrap(LHS), unwrap(RHS), Name));
1535 /*--.. Miscellaneous instructions ..........................................--*/
1537 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1538 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1541 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1542 LLVMValueRef *Args, unsigned NumArgs,
1544 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1545 unwrap(Args) + NumArgs, Name));
1548 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1549 LLVMValueRef Then, LLVMValueRef Else,
1551 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1555 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1556 LLVMTypeRef Ty, const char *Name) {
1557 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1560 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1561 LLVMValueRef Index, const char *Name) {
1562 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1566 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1567 LLVMValueRef EltVal, LLVMValueRef Index,
1569 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1570 unwrap(Index), Name));
1573 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1574 LLVMValueRef V2, LLVMValueRef Mask,
1576 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1577 unwrap(Mask), Name));
1580 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1581 unsigned Index, const char *Name) {
1582 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1585 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1586 LLVMValueRef EltVal, unsigned Index,
1588 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1593 /*===-- Module providers --------------------------------------------------===*/
1595 LLVMModuleProviderRef
1596 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1597 return wrap(new ExistingModuleProvider(unwrap(M)));
1600 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1605 /*===-- Memory buffers ----------------------------------------------------===*/
1607 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1608 LLVMMemoryBufferRef *OutMemBuf,
1609 char **OutMessage) {
1611 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1612 *OutMemBuf = wrap(MB);
1616 *OutMessage = strdup(Error.c_str());
1620 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1621 char **OutMessage) {
1622 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1623 *OutMemBuf = wrap(MB);
1627 *OutMessage = strdup("stdin is empty.");
1631 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1632 delete unwrap(MemBuf);