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"
35 /*===-- Error handling ----------------------------------------------------===*/
37 void LLVMDisposeMessage(char *Message) {
42 /*===-- Operations on contexts --------------------------------------------===*/
44 LLVMContextRef LLVMContextCreate() {
45 return wrap(new LLVMContext());
48 LLVMContextRef LLVMGetGlobalContext() {
49 return wrap(&getGlobalContext());
52 void LLVMContextDispose(LLVMContextRef C) {
57 /*===-- Operations on modules ---------------------------------------------===*/
59 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
60 return wrap(new Module(ModuleID, getGlobalContext()));
63 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
65 return wrap(new Module(ModuleID, *unwrap(C)));
68 void LLVMDisposeModule(LLVMModuleRef M) {
72 /*--.. Data layout .........................................................--*/
73 const char * LLVMGetDataLayout(LLVMModuleRef M) {
74 return unwrap(M)->getDataLayout().c_str();
77 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
78 unwrap(M)->setDataLayout(Triple);
81 /*--.. Target triple .......................................................--*/
82 const char * LLVMGetTarget(LLVMModuleRef M) {
83 return unwrap(M)->getTargetTriple().c_str();
86 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
87 unwrap(M)->setTargetTriple(Triple);
90 /*--.. Type names ..........................................................--*/
91 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
92 return unwrap(M)->addTypeName(Name, unwrap(Ty));
95 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
99 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
104 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106 return wrap(unwrap(M)->getTypeByName(N));
109 void LLVMDumpModule(LLVMModuleRef M) {
114 /*===-- Operations on types -----------------------------------------------===*/
116 /*--.. Operations on all types (mostly) ....................................--*/
118 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
119 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
122 /*--.. Operations on integer types .........................................--*/
124 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
125 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
126 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
127 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
128 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
130 LLVMTypeRef LLVMIntType(unsigned NumBits) {
131 return wrap(IntegerType::get(NumBits));
134 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
135 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
138 /*--.. Operations on real types ............................................--*/
140 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
141 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
142 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
143 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
144 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
146 /*--.. Operations on function types ........................................--*/
148 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
149 LLVMTypeRef *ParamTypes, unsigned ParamCount,
151 std::vector<const Type*> Tys;
152 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
153 Tys.push_back(unwrap(*I));
155 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
158 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
159 return unwrap<FunctionType>(FunctionTy)->isVarArg();
162 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
163 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
166 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
167 return unwrap<FunctionType>(FunctionTy)->getNumParams();
170 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
171 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
172 for (FunctionType::param_iterator I = Ty->param_begin(),
173 E = Ty->param_end(); I != E; ++I)
177 /*--.. Operations on struct types ..........................................--*/
179 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
180 unsigned ElementCount, int Packed) {
181 std::vector<const Type*> Tys;
182 for (LLVMTypeRef *I = ElementTypes,
183 *E = ElementTypes + ElementCount; I != E; ++I)
184 Tys.push_back(unwrap(*I));
186 return wrap(StructType::get(Tys, Packed != 0));
189 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
190 return unwrap<StructType>(StructTy)->getNumElements();
193 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
194 StructType *Ty = unwrap<StructType>(StructTy);
195 for (FunctionType::param_iterator I = Ty->element_begin(),
196 E = Ty->element_end(); I != E; ++I)
200 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
201 return unwrap<StructType>(StructTy)->isPacked();
204 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
206 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
207 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
210 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
211 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
214 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
215 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
218 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
219 return wrap(unwrap<SequentialType>(Ty)->getElementType());
222 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
223 return unwrap<ArrayType>(ArrayTy)->getNumElements();
226 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
227 return unwrap<PointerType>(PointerTy)->getAddressSpace();
230 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
231 return unwrap<VectorType>(VectorTy)->getNumElements();
234 /*--.. Operations on other types ...........................................--*/
236 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
237 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
239 LLVMTypeRef LLVMOpaqueType(void) {
240 return wrap(llvm::OpaqueType::get());
243 /*--.. Operations on type handles ..........................................--*/
245 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
246 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
249 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
250 delete unwrap(TypeHandle);
253 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
254 return wrap(unwrap(TypeHandle)->get());
257 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
258 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
262 /*===-- Operations on values ----------------------------------------------===*/
264 /*--.. Operations on all values ............................................--*/
266 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
267 return wrap(unwrap(Val)->getType());
270 const char *LLVMGetValueName(LLVMValueRef Val) {
271 return unwrap(Val)->getNameStart();
274 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
275 unwrap(Val)->setName(Name);
278 void LLVMDumpValue(LLVMValueRef Val) {
283 /*--.. Conversion functions ................................................--*/
285 #define LLVM_DEFINE_VALUE_CAST(name) \
286 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
287 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
290 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
293 /*--.. Operations on constants of any type .................................--*/
295 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
296 return wrap(Constant::getNullValue(unwrap(Ty)));
299 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
300 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
303 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
304 return wrap(UndefValue::get(unwrap(Ty)));
307 int LLVMIsConstant(LLVMValueRef Ty) {
308 return isa<Constant>(unwrap(Ty));
311 int LLVMIsNull(LLVMValueRef Val) {
312 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
313 return C->isNullValue();
317 int LLVMIsUndef(LLVMValueRef Val) {
318 return isa<UndefValue>(unwrap(Val));
321 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
322 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
325 /*--.. Operations on scalar constants ......................................--*/
327 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
329 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
332 static const fltSemantics &SemanticsForType(Type *Ty) {
333 assert(Ty->isFloatingPoint() && "Type is not floating point!");
334 if (Ty == Type::FloatTy)
335 return APFloat::IEEEsingle;
336 if (Ty == Type::DoubleTy)
337 return APFloat::IEEEdouble;
338 if (Ty == Type::X86_FP80Ty)
339 return APFloat::x87DoubleExtended;
340 if (Ty == Type::FP128Ty)
341 return APFloat::IEEEquad;
342 if (Ty == Type::PPC_FP128Ty)
343 return APFloat::PPCDoubleDouble;
344 return APFloat::Bogus;
347 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
350 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
352 return wrap(ConstantFP::get(APN));
355 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
356 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
359 /*--.. Operations on composite constants ...................................--*/
361 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
362 int DontNullTerminate) {
363 /* Inverted the sense of AddNull because ', 0)' is a
364 better mnemonic for null termination than ', 1)'. */
365 return wrap(ConstantArray::get(std::string(Str, Length),
366 DontNullTerminate == 0));
369 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
370 LLVMValueRef *ConstantVals, unsigned Length) {
371 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
372 unwrap<Constant>(ConstantVals, Length),
376 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
378 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
379 Count, Packed != 0));
382 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
383 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
387 /*--.. Constant expressions ................................................--*/
389 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
390 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
393 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
394 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
397 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
398 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
401 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
402 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
405 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
407 unwrap<Constant>(RHSConstant)));
410 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
412 unwrap<Constant>(RHSConstant)));
415 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
416 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
417 unwrap<Constant>(RHSConstant)));
420 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
421 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
422 unwrap<Constant>(RHSConstant)));
425 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
426 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
427 unwrap<Constant>(RHSConstant)));
430 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
431 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
432 unwrap<Constant>(RHSConstant)));
435 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
436 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
437 unwrap<Constant>(RHSConstant)));
440 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
441 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
442 unwrap<Constant>(RHSConstant)));
445 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
446 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
447 unwrap<Constant>(RHSConstant)));
450 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
451 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
452 unwrap<Constant>(RHSConstant)));
455 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
456 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
457 unwrap<Constant>(RHSConstant)));
460 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
461 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
462 unwrap<Constant>(RHSConstant)));
465 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
466 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
467 return wrap(ConstantExpr::getICmp(Predicate,
468 unwrap<Constant>(LHSConstant),
469 unwrap<Constant>(RHSConstant)));
472 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
473 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
474 return wrap(ConstantExpr::getFCmp(Predicate,
475 unwrap<Constant>(LHSConstant),
476 unwrap<Constant>(RHSConstant)));
479 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
480 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
481 unwrap<Constant>(RHSConstant)));
484 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
485 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
486 unwrap<Constant>(RHSConstant)));
489 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
490 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
491 unwrap<Constant>(RHSConstant)));
494 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
495 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
496 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
497 unwrap<Constant>(ConstantIndices,
502 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
507 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
512 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
513 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
517 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
518 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
522 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
523 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
527 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
528 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
532 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
533 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
537 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
538 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
542 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
543 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
547 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
548 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
552 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
553 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
557 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
558 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
562 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
563 LLVMValueRef ConstantIfTrue,
564 LLVMValueRef ConstantIfFalse) {
565 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
566 unwrap<Constant>(ConstantIfTrue),
567 unwrap<Constant>(ConstantIfFalse)));
570 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
571 LLVMValueRef IndexConstant) {
572 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
573 unwrap<Constant>(IndexConstant)));
576 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
577 LLVMValueRef ElementValueConstant,
578 LLVMValueRef IndexConstant) {
579 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
580 unwrap<Constant>(ElementValueConstant),
581 unwrap<Constant>(IndexConstant)));
584 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
585 LLVMValueRef VectorBConstant,
586 LLVMValueRef MaskConstant) {
587 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
588 unwrap<Constant>(VectorBConstant),
589 unwrap<Constant>(MaskConstant)));
592 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
594 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
598 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
599 LLVMValueRef ElementValueConstant,
600 unsigned *IdxList, unsigned NumIdx) {
601 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
602 unwrap<Constant>(ElementValueConstant),
606 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
607 const char *Constraints, int HasSideEffects) {
608 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
609 Constraints, HasSideEffects));
612 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
614 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
615 return wrap(unwrap<GlobalValue>(Global)->getParent());
618 int LLVMIsDeclaration(LLVMValueRef Global) {
619 return unwrap<GlobalValue>(Global)->isDeclaration();
622 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
623 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
626 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
627 unwrap<GlobalValue>(Global)
628 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
631 const char *LLVMGetSection(LLVMValueRef Global) {
632 return unwrap<GlobalValue>(Global)->getSection().c_str();
635 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
636 unwrap<GlobalValue>(Global)->setSection(Section);
639 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
640 return static_cast<LLVMVisibility>(
641 unwrap<GlobalValue>(Global)->getVisibility());
644 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
645 unwrap<GlobalValue>(Global)
646 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
649 unsigned LLVMGetAlignment(LLVMValueRef Global) {
650 return unwrap<GlobalValue>(Global)->getAlignment();
653 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
654 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
657 /*--.. Operations on global variables ......................................--*/
659 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
660 return wrap(new GlobalVariable(unwrap(Ty), false,
661 GlobalValue::ExternalLinkage, 0, Name,
665 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
666 return wrap(unwrap(M)->getNamedGlobal(Name));
669 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
670 Module *Mod = unwrap(M);
671 Module::global_iterator I = Mod->global_begin();
672 if (I == Mod->global_end())
677 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
678 Module *Mod = unwrap(M);
679 Module::global_iterator I = Mod->global_end();
680 if (I == Mod->global_begin())
685 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
686 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
687 Module::global_iterator I = GV;
688 if (++I == GV->getParent()->global_end())
693 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
694 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
695 Module::global_iterator I = GV;
696 if (I == GV->getParent()->global_begin())
701 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
702 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
705 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
706 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
709 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
710 unwrap<GlobalVariable>(GlobalVar)
711 ->setInitializer(unwrap<Constant>(ConstantVal));
714 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
715 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
718 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
719 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
722 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
723 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
726 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
727 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
730 /*--.. Operations on aliases ......................................--*/
732 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
734 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
735 unwrap<Constant>(Aliasee), unwrap (M)));
738 /*--.. Operations on functions .............................................--*/
740 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
741 LLVMTypeRef FunctionTy) {
742 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
743 GlobalValue::ExternalLinkage, Name, unwrap(M)));
746 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
747 return wrap(unwrap(M)->getFunction(Name));
750 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
751 Module *Mod = unwrap(M);
752 Module::iterator I = Mod->begin();
758 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
759 Module *Mod = unwrap(M);
760 Module::iterator I = Mod->end();
761 if (I == Mod->begin())
766 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
767 Function *Func = unwrap<Function>(Fn);
768 Module::iterator I = Func;
769 if (++I == Func->getParent()->end())
774 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
775 Function *Func = unwrap<Function>(Fn);
776 Module::iterator I = Func;
777 if (I == Func->getParent()->begin())
782 void LLVMDeleteFunction(LLVMValueRef Fn) {
783 unwrap<Function>(Fn)->eraseFromParent();
786 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
787 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
788 return F->getIntrinsicID();
792 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
793 return unwrap<Function>(Fn)->getCallingConv();
796 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
797 return unwrap<Function>(Fn)->setCallingConv(CC);
800 const char *LLVMGetGC(LLVMValueRef Fn) {
801 Function *F = unwrap<Function>(Fn);
802 return F->hasGC()? F->getGC() : 0;
805 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
806 Function *F = unwrap<Function>(Fn);
813 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
814 Function *Func = unwrap<Function>(Fn);
815 const AttrListPtr PAL = Func->getAttributes();
816 const AttrListPtr PALnew = PAL.addAttr(0, PA);
817 Func->setAttributes(PALnew);
820 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
821 Function *Func = unwrap<Function>(Fn);
822 const AttrListPtr PAL = Func->getAttributes();
823 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
824 Func->setAttributes(PALnew);
827 /*--.. Operations on parameters ............................................--*/
829 unsigned LLVMCountParams(LLVMValueRef FnRef) {
830 // This function is strictly redundant to
831 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
832 return unwrap<Function>(FnRef)->arg_size();
835 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
836 Function *Fn = unwrap<Function>(FnRef);
837 for (Function::arg_iterator I = Fn->arg_begin(),
838 E = Fn->arg_end(); I != E; I++)
839 *ParamRefs++ = wrap(I);
842 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
843 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
849 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
850 return wrap(unwrap<Argument>(V)->getParent());
853 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
854 Function *Func = unwrap<Function>(Fn);
855 Function::arg_iterator I = Func->arg_begin();
856 if (I == Func->arg_end())
861 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
862 Function *Func = unwrap<Function>(Fn);
863 Function::arg_iterator I = Func->arg_end();
864 if (I == Func->arg_begin())
869 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
870 Argument *A = unwrap<Argument>(Arg);
871 Function::arg_iterator I = A;
872 if (++I == A->getParent()->arg_end())
877 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
878 Argument *A = unwrap<Argument>(Arg);
879 Function::arg_iterator I = A;
880 if (I == A->getParent()->arg_begin())
885 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
886 unwrap<Argument>(Arg)->addAttr(PA);
889 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
890 unwrap<Argument>(Arg)->removeAttr(PA);
893 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
894 unwrap<Argument>(Arg)->addAttr(
895 Attribute::constructAlignmentFromInt(align));
898 /*--.. Operations on basic blocks ..........................................--*/
900 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
901 return wrap(static_cast<Value*>(unwrap(BB)));
904 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
905 return isa<BasicBlock>(unwrap(Val));
908 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
909 return wrap(unwrap<BasicBlock>(Val));
912 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
913 return wrap(unwrap(BB)->getParent());
916 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
917 return unwrap<Function>(FnRef)->size();
920 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
921 Function *Fn = unwrap<Function>(FnRef);
922 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
923 *BasicBlocksRefs++ = wrap(I);
926 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
927 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
930 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
931 Function *Func = unwrap<Function>(Fn);
932 Function::iterator I = Func->begin();
933 if (I == Func->end())
938 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
939 Function *Func = unwrap<Function>(Fn);
940 Function::iterator I = Func->end();
941 if (I == Func->begin())
946 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
947 BasicBlock *Block = unwrap(BB);
948 Function::iterator I = Block;
949 if (++I == Block->getParent()->end())
954 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
955 BasicBlock *Block = unwrap(BB);
956 Function::iterator I = Block;
957 if (I == Block->getParent()->begin())
962 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
963 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
966 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
968 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
969 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
973 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
974 unwrap(BBRef)->eraseFromParent();
977 /*--.. Operations on instructions ..........................................--*/
979 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
980 return wrap(unwrap<Instruction>(Inst)->getParent());
983 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
984 BasicBlock *Block = unwrap(BB);
985 BasicBlock::iterator I = Block->begin();
986 if (I == Block->end())
991 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
992 BasicBlock *Block = unwrap(BB);
993 BasicBlock::iterator I = Block->end();
994 if (I == Block->begin())
999 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1000 Instruction *Instr = unwrap<Instruction>(Inst);
1001 BasicBlock::iterator I = Instr;
1002 if (++I == Instr->getParent()->end())
1007 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1008 Instruction *Instr = unwrap<Instruction>(Inst);
1009 BasicBlock::iterator I = Instr;
1010 if (I == Instr->getParent()->begin())
1015 /*--.. Call and invoke instructions ........................................--*/
1017 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1018 Value *V = unwrap(Instr);
1019 if (CallInst *CI = dyn_cast<CallInst>(V))
1020 return CI->getCallingConv();
1021 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1022 return II->getCallingConv();
1023 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
1027 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1028 Value *V = unwrap(Instr);
1029 if (CallInst *CI = dyn_cast<CallInst>(V))
1030 return CI->setCallingConv(CC);
1031 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1032 return II->setCallingConv(CC);
1033 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1036 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1038 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1040 Call.getAttributes().addAttr(index, PA));
1043 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1045 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1047 Call.getAttributes().removeAttr(index, PA));
1050 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1052 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1054 Call.getAttributes().addAttr(index,
1055 Attribute::constructAlignmentFromInt(align)));
1058 /*--.. Operations on call instructions (only) ..............................--*/
1060 int LLVMIsTailCall(LLVMValueRef Call) {
1061 return unwrap<CallInst>(Call)->isTailCall();
1064 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1065 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1068 /*--.. Operations on phi nodes .............................................--*/
1070 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1071 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1072 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1073 for (unsigned I = 0; I != Count; ++I)
1074 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1077 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1078 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1081 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1082 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1085 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1086 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1090 /*===-- Instruction builders ----------------------------------------------===*/
1092 LLVMBuilderRef LLVMCreateBuilder(void) {
1093 return wrap(new IRBuilder<>());
1096 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1097 LLVMValueRef Instr) {
1098 BasicBlock *BB = unwrap(Block);
1099 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1100 unwrap(Builder)->SetInsertPoint(BB, I);
1103 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1104 Instruction *I = unwrap<Instruction>(Instr);
1105 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1108 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1109 BasicBlock *BB = unwrap(Block);
1110 unwrap(Builder)->SetInsertPoint(BB);
1113 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1114 return wrap(unwrap(Builder)->GetInsertBlock());
1117 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1118 unwrap(Builder)->ClearInsertionPoint ();
1121 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1122 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1125 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1126 delete unwrap(Builder);
1129 /*--.. Instruction builders ................................................--*/
1131 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1132 return wrap(unwrap(B)->CreateRetVoid());
1135 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1136 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1139 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1140 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1143 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1144 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1145 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1148 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1149 LLVMBasicBlockRef Else, unsigned NumCases) {
1150 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1153 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1154 LLVMValueRef *Args, unsigned NumArgs,
1155 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1157 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1158 unwrap(Args), unwrap(Args) + NumArgs,
1162 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1163 return wrap(unwrap(B)->CreateUnwind());
1166 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1167 return wrap(unwrap(B)->CreateUnreachable());
1170 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1171 LLVMBasicBlockRef Dest) {
1172 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1175 /*--.. Arithmetic ..........................................................--*/
1177 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1179 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1182 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1184 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1187 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1189 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1192 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1194 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1197 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1199 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1202 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1204 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1207 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1209 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1212 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1214 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1217 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1219 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1222 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1224 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1227 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1229 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1232 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1234 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1237 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1239 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1242 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1244 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1247 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1249 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1252 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1253 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1256 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1257 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1260 /*--.. Memory ..............................................................--*/
1262 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1264 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1267 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1268 LLVMValueRef Val, const char *Name) {
1269 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1272 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1274 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1277 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1278 LLVMValueRef Val, const char *Name) {
1279 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1282 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1283 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1287 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1289 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1292 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1293 LLVMValueRef PointerVal) {
1294 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1297 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1298 LLVMValueRef *Indices, unsigned NumIndices,
1300 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1301 unwrap(Indices) + NumIndices, Name));
1304 /*--.. Casts ...............................................................--*/
1306 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1307 LLVMTypeRef DestTy, const char *Name) {
1308 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1311 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1312 LLVMTypeRef DestTy, const char *Name) {
1313 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1316 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1317 LLVMTypeRef DestTy, const char *Name) {
1318 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1321 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1322 LLVMTypeRef DestTy, const char *Name) {
1323 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1326 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1327 LLVMTypeRef DestTy, const char *Name) {
1328 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1331 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1332 LLVMTypeRef DestTy, const char *Name) {
1333 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1336 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1337 LLVMTypeRef DestTy, const char *Name) {
1338 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1341 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1342 LLVMTypeRef DestTy, const char *Name) {
1343 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1346 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1347 LLVMTypeRef DestTy, const char *Name) {
1348 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1351 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1352 LLVMTypeRef DestTy, const char *Name) {
1353 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1356 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1357 LLVMTypeRef DestTy, const char *Name) {
1358 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1361 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1362 LLVMTypeRef DestTy, const char *Name) {
1363 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1366 /*--.. Comparisons .........................................................--*/
1368 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1369 LLVMValueRef LHS, LLVMValueRef RHS,
1371 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1372 unwrap(LHS), unwrap(RHS), Name));
1375 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1376 LLVMValueRef LHS, LLVMValueRef RHS,
1378 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1379 unwrap(LHS), unwrap(RHS), Name));
1382 /*--.. Miscellaneous instructions ..........................................--*/
1384 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1385 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1388 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1389 LLVMValueRef *Args, unsigned NumArgs,
1391 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1392 unwrap(Args) + NumArgs, Name));
1395 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1396 LLVMValueRef Then, LLVMValueRef Else,
1398 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1402 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1403 LLVMTypeRef Ty, const char *Name) {
1404 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1407 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1408 LLVMValueRef Index, const char *Name) {
1409 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1413 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1414 LLVMValueRef EltVal, LLVMValueRef Index,
1416 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1417 unwrap(Index), Name));
1420 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1421 LLVMValueRef V2, LLVMValueRef Mask,
1423 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1424 unwrap(Mask), Name));
1427 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1428 unsigned Index, const char *Name) {
1429 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1432 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1433 LLVMValueRef EltVal, unsigned Index,
1435 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1440 /*===-- Module providers --------------------------------------------------===*/
1442 LLVMModuleProviderRef
1443 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1444 return wrap(new ExistingModuleProvider(unwrap(M)));
1447 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1452 /*===-- Memory buffers ----------------------------------------------------===*/
1454 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1455 LLVMMemoryBufferRef *OutMemBuf,
1456 char **OutMessage) {
1458 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1459 *OutMemBuf = wrap(MB);
1463 *OutMessage = strdup(Error.c_str());
1467 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1468 char **OutMessage) {
1469 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1470 *OutMemBuf = wrap(MB);
1474 *OutMessage = strdup("stdin is empty.");
1478 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1479 delete unwrap(MemBuf);