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/TypeSymbolTable.h"
22 #include "llvm/ModuleProvider.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/CallSite.h"
34 /*===-- Error handling ----------------------------------------------------===*/
36 void LLVMDisposeMessage(char *Message
) {
41 /*===-- Operations on modules ---------------------------------------------===*/
43 LLVMModuleRef
LLVMModuleCreateWithName(const char *ModuleID
) {
44 return wrap(new Module(ModuleID
));
47 void LLVMDisposeModule(LLVMModuleRef M
) {
51 /*--.. Data layout .........................................................--*/
52 const char * LLVMGetDataLayout(LLVMModuleRef M
) {
53 return unwrap(M
)->getDataLayout().c_str();
56 void LLVMSetDataLayout(LLVMModuleRef M
, const char *Triple
) {
57 unwrap(M
)->setDataLayout(Triple
);
60 /*--.. Target triple .......................................................--*/
61 const char * LLVMGetTarget(LLVMModuleRef M
) {
62 return unwrap(M
)->getTargetTriple().c_str();
65 void LLVMSetTarget(LLVMModuleRef M
, const char *Triple
) {
66 unwrap(M
)->setTargetTriple(Triple
);
69 /*--.. Type names ..........................................................--*/
70 int LLVMAddTypeName(LLVMModuleRef M
, const char *Name
, LLVMTypeRef Ty
) {
71 return unwrap(M
)->addTypeName(Name
, unwrap(Ty
));
74 void LLVMDeleteTypeName(LLVMModuleRef M
, const char *Name
) {
77 TypeSymbolTable
&TST
= unwrap(M
)->getTypeSymbolTable();
78 for (TypeSymbolTable::iterator I
= TST
.begin(), E
= TST
.end(); I
!= E
; ++I
)
83 void LLVMDumpModule(LLVMModuleRef M
) {
88 /*===-- Operations on types -----------------------------------------------===*/
90 /*--.. Operations on all types (mostly) ....................................--*/
92 LLVMTypeKind
LLVMGetTypeKind(LLVMTypeRef Ty
) {
93 return static_cast<LLVMTypeKind
>(unwrap(Ty
)->getTypeID());
96 /*--.. Operations on integer types .........................................--*/
98 LLVMTypeRef
LLVMInt1Type(void) { return (LLVMTypeRef
) Type::Int1Ty
; }
99 LLVMTypeRef
LLVMInt8Type(void) { return (LLVMTypeRef
) Type::Int8Ty
; }
100 LLVMTypeRef
LLVMInt16Type(void) { return (LLVMTypeRef
) Type::Int16Ty
; }
101 LLVMTypeRef
LLVMInt32Type(void) { return (LLVMTypeRef
) Type::Int32Ty
; }
102 LLVMTypeRef
LLVMInt64Type(void) { return (LLVMTypeRef
) Type::Int64Ty
; }
104 LLVMTypeRef
LLVMIntType(unsigned NumBits
) {
105 return wrap(IntegerType::get(NumBits
));
108 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy
) {
109 return unwrap
<IntegerType
>(IntegerTy
)->getBitWidth();
112 /*--.. Operations on real types ............................................--*/
114 LLVMTypeRef
LLVMFloatType(void) { return (LLVMTypeRef
) Type::FloatTy
; }
115 LLVMTypeRef
LLVMDoubleType(void) { return (LLVMTypeRef
) Type::DoubleTy
; }
116 LLVMTypeRef
LLVMX86FP80Type(void) { return (LLVMTypeRef
) Type::X86_FP80Ty
; }
117 LLVMTypeRef
LLVMFP128Type(void) { return (LLVMTypeRef
) Type::FP128Ty
; }
118 LLVMTypeRef
LLVMPPCFP128Type(void) { return (LLVMTypeRef
) Type::PPC_FP128Ty
; }
120 /*--.. Operations on function types ........................................--*/
122 LLVMTypeRef
LLVMFunctionType(LLVMTypeRef ReturnType
,
123 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
,
125 std::vector
<const Type
*> Tys
;
126 for (LLVMTypeRef
*I
= ParamTypes
, *E
= ParamTypes
+ ParamCount
; I
!= E
; ++I
)
127 Tys
.push_back(unwrap(*I
));
129 return wrap(FunctionType::get(unwrap(ReturnType
), Tys
, IsVarArg
!= 0));
132 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy
) {
133 return unwrap
<FunctionType
>(FunctionTy
)->isVarArg();
136 LLVMTypeRef
LLVMGetReturnType(LLVMTypeRef FunctionTy
) {
137 return wrap(unwrap
<FunctionType
>(FunctionTy
)->getReturnType());
140 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy
) {
141 return unwrap
<FunctionType
>(FunctionTy
)->getNumParams();
144 void LLVMGetParamTypes(LLVMTypeRef FunctionTy
, LLVMTypeRef
*Dest
) {
145 FunctionType
*Ty
= unwrap
<FunctionType
>(FunctionTy
);
146 for (FunctionType::param_iterator I
= Ty
->param_begin(),
147 E
= Ty
->param_end(); I
!= E
; ++I
)
151 /*--.. Operations on struct types ..........................................--*/
153 LLVMTypeRef
LLVMStructType(LLVMTypeRef
*ElementTypes
,
154 unsigned ElementCount
, int Packed
) {
155 std::vector
<const Type
*> Tys
;
156 for (LLVMTypeRef
*I
= ElementTypes
,
157 *E
= ElementTypes
+ ElementCount
; I
!= E
; ++I
)
158 Tys
.push_back(unwrap(*I
));
160 return wrap(StructType::get(Tys
, Packed
!= 0));
163 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy
) {
164 return unwrap
<StructType
>(StructTy
)->getNumElements();
167 void LLVMGetStructElementTypes(LLVMTypeRef StructTy
, LLVMTypeRef
*Dest
) {
168 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
169 for (FunctionType::param_iterator I
= Ty
->element_begin(),
170 E
= Ty
->element_end(); I
!= E
; ++I
)
174 int LLVMIsPackedStruct(LLVMTypeRef StructTy
) {
175 return unwrap
<StructType
>(StructTy
)->isPacked();
178 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
180 LLVMTypeRef
LLVMArrayType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
181 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
184 LLVMTypeRef
LLVMPointerType(LLVMTypeRef ElementType
, unsigned AddressSpace
) {
185 return wrap(PointerType::get(unwrap(ElementType
), AddressSpace
));
188 LLVMTypeRef
LLVMVectorType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
189 return wrap(VectorType::get(unwrap(ElementType
), ElementCount
));
192 LLVMTypeRef
LLVMGetElementType(LLVMTypeRef Ty
) {
193 return wrap(unwrap
<SequentialType
>(Ty
)->getElementType());
196 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy
) {
197 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
200 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy
) {
201 return unwrap
<PointerType
>(PointerTy
)->getAddressSpace();
204 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy
) {
205 return unwrap
<VectorType
>(VectorTy
)->getNumElements();
208 /*--.. Operations on other types ...........................................--*/
210 LLVMTypeRef
LLVMVoidType(void) { return (LLVMTypeRef
) Type::VoidTy
; }
211 LLVMTypeRef
LLVMLabelType(void) { return (LLVMTypeRef
) Type::LabelTy
; }
213 LLVMTypeRef
LLVMOpaqueType(void) {
214 return wrap(llvm::OpaqueType::get());
217 /*--.. Operations on type handles ..........................................--*/
219 LLVMTypeHandleRef
LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy
) {
220 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy
)));
223 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle
) {
224 delete unwrap(TypeHandle
);
227 LLVMTypeRef
LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle
) {
228 return wrap(unwrap(TypeHandle
)->get());
231 void LLVMRefineType(LLVMTypeRef AbstractTy
, LLVMTypeRef ConcreteTy
) {
232 unwrap
<DerivedType
>(AbstractTy
)->refineAbstractTypeTo(unwrap(ConcreteTy
));
236 /*===-- Operations on values ----------------------------------------------===*/
238 /*--.. Operations on all values ............................................--*/
240 LLVMTypeRef
LLVMTypeOf(LLVMValueRef Val
) {
241 return wrap(unwrap(Val
)->getType());
244 const char *LLVMGetValueName(LLVMValueRef Val
) {
245 return unwrap(Val
)->getNameStart();
248 void LLVMSetValueName(LLVMValueRef Val
, const char *Name
) {
249 unwrap(Val
)->setName(Name
);
252 void LLVMDumpValue(LLVMValueRef Val
) {
257 /*--.. Conversion functions ................................................--*/
259 #define LLVM_DEFINE_VALUE_CAST(name) \
260 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
261 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
264 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST
)
267 /*--.. Operations on constants of any type .................................--*/
269 LLVMValueRef
LLVMConstNull(LLVMTypeRef Ty
) {
270 return wrap(Constant::getNullValue(unwrap(Ty
)));
273 LLVMValueRef
LLVMConstAllOnes(LLVMTypeRef Ty
) {
274 return wrap(Constant::getAllOnesValue(unwrap(Ty
)));
277 LLVMValueRef
LLVMGetUndef(LLVMTypeRef Ty
) {
278 return wrap(UndefValue::get(unwrap(Ty
)));
281 int LLVMIsConstant(LLVMValueRef Ty
) {
282 return isa
<Constant
>(unwrap(Ty
));
285 int LLVMIsNull(LLVMValueRef Val
) {
286 if (Constant
*C
= dyn_cast
<Constant
>(unwrap(Val
)))
287 return C
->isNullValue();
291 int LLVMIsUndef(LLVMValueRef Val
) {
292 return isa
<UndefValue
>(unwrap(Val
));
295 /*--.. Operations on scalar constants ......................................--*/
297 LLVMValueRef
LLVMConstInt(LLVMTypeRef IntTy
, unsigned long long N
,
299 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), N
, SignExtend
!= 0));
302 static const fltSemantics
&SemanticsForType(Type
*Ty
) {
303 assert(Ty
->isFloatingPoint() && "Type is not floating point!");
304 if (Ty
== Type::FloatTy
)
305 return APFloat::IEEEsingle
;
306 if (Ty
== Type::DoubleTy
)
307 return APFloat::IEEEdouble
;
308 if (Ty
== Type::X86_FP80Ty
)
309 return APFloat::x87DoubleExtended
;
310 if (Ty
== Type::FP128Ty
)
311 return APFloat::IEEEquad
;
312 if (Ty
== Type::PPC_FP128Ty
)
313 return APFloat::PPCDoubleDouble
;
314 return APFloat::Bogus
;
317 LLVMValueRef
LLVMConstReal(LLVMTypeRef RealTy
, double N
) {
320 APN
.convert(SemanticsForType(unwrap(RealTy
)), APFloat::rmNearestTiesToEven
,
322 return wrap(ConstantFP::get(APN
));
325 LLVMValueRef
LLVMConstRealOfString(LLVMTypeRef RealTy
, const char *Text
) {
326 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy
)), Text
)));
329 /*--.. Operations on composite constants ...................................--*/
331 LLVMValueRef
LLVMConstString(const char *Str
, unsigned Length
,
332 int DontNullTerminate
) {
333 /* Inverted the sense of AddNull because ', 0)' is a
334 better mnemonic for null termination than ', 1)'. */
335 return wrap(ConstantArray::get(std::string(Str
, Length
),
336 DontNullTerminate
== 0));
339 LLVMValueRef
LLVMConstArray(LLVMTypeRef ElementTy
,
340 LLVMValueRef
*ConstantVals
, unsigned Length
) {
341 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
),
342 unwrap
<Constant
>(ConstantVals
, Length
),
346 LLVMValueRef
LLVMConstStruct(LLVMValueRef
*ConstantVals
, unsigned Count
,
348 return wrap(ConstantStruct::get(unwrap
<Constant
>(ConstantVals
, Count
),
349 Count
, Packed
!= 0));
352 LLVMValueRef
LLVMConstVector(LLVMValueRef
*ScalarConstantVals
, unsigned Size
) {
353 return wrap(ConstantVector::get(unwrap
<Constant
>(ScalarConstantVals
, Size
),
357 /*--.. Constant expressions ................................................--*/
359 LLVMValueRef
LLVMSizeOf(LLVMTypeRef Ty
) {
360 return wrap(ConstantExpr::getSizeOf(unwrap(Ty
)));
363 LLVMValueRef
LLVMConstNeg(LLVMValueRef ConstantVal
) {
364 return wrap(ConstantExpr::getNeg(unwrap
<Constant
>(ConstantVal
)));
367 LLVMValueRef
LLVMConstNot(LLVMValueRef ConstantVal
) {
368 return wrap(ConstantExpr::getNot(unwrap
<Constant
>(ConstantVal
)));
371 LLVMValueRef
LLVMConstAdd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
372 return wrap(ConstantExpr::getAdd(unwrap
<Constant
>(LHSConstant
),
373 unwrap
<Constant
>(RHSConstant
)));
376 LLVMValueRef
LLVMConstSub(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
377 return wrap(ConstantExpr::getSub(unwrap
<Constant
>(LHSConstant
),
378 unwrap
<Constant
>(RHSConstant
)));
381 LLVMValueRef
LLVMConstMul(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
382 return wrap(ConstantExpr::getMul(unwrap
<Constant
>(LHSConstant
),
383 unwrap
<Constant
>(RHSConstant
)));
386 LLVMValueRef
LLVMConstUDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
387 return wrap(ConstantExpr::getUDiv(unwrap
<Constant
>(LHSConstant
),
388 unwrap
<Constant
>(RHSConstant
)));
391 LLVMValueRef
LLVMConstSDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
392 return wrap(ConstantExpr::getSDiv(unwrap
<Constant
>(LHSConstant
),
393 unwrap
<Constant
>(RHSConstant
)));
396 LLVMValueRef
LLVMConstFDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
397 return wrap(ConstantExpr::getFDiv(unwrap
<Constant
>(LHSConstant
),
398 unwrap
<Constant
>(RHSConstant
)));
401 LLVMValueRef
LLVMConstURem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
402 return wrap(ConstantExpr::getURem(unwrap
<Constant
>(LHSConstant
),
403 unwrap
<Constant
>(RHSConstant
)));
406 LLVMValueRef
LLVMConstSRem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
407 return wrap(ConstantExpr::getSRem(unwrap
<Constant
>(LHSConstant
),
408 unwrap
<Constant
>(RHSConstant
)));
411 LLVMValueRef
LLVMConstFRem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
412 return wrap(ConstantExpr::getFRem(unwrap
<Constant
>(LHSConstant
),
413 unwrap
<Constant
>(RHSConstant
)));
416 LLVMValueRef
LLVMConstAnd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
417 return wrap(ConstantExpr::getAnd(unwrap
<Constant
>(LHSConstant
),
418 unwrap
<Constant
>(RHSConstant
)));
421 LLVMValueRef
LLVMConstOr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
422 return wrap(ConstantExpr::getOr(unwrap
<Constant
>(LHSConstant
),
423 unwrap
<Constant
>(RHSConstant
)));
426 LLVMValueRef
LLVMConstXor(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
427 return wrap(ConstantExpr::getXor(unwrap
<Constant
>(LHSConstant
),
428 unwrap
<Constant
>(RHSConstant
)));
431 LLVMValueRef
LLVMConstICmp(LLVMIntPredicate Predicate
,
432 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
433 return wrap(ConstantExpr::getICmp(Predicate
,
434 unwrap
<Constant
>(LHSConstant
),
435 unwrap
<Constant
>(RHSConstant
)));
438 LLVMValueRef
LLVMConstFCmp(LLVMRealPredicate Predicate
,
439 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
440 return wrap(ConstantExpr::getFCmp(Predicate
,
441 unwrap
<Constant
>(LHSConstant
),
442 unwrap
<Constant
>(RHSConstant
)));
445 LLVMValueRef
LLVMConstShl(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
446 return wrap(ConstantExpr::getShl(unwrap
<Constant
>(LHSConstant
),
447 unwrap
<Constant
>(RHSConstant
)));
450 LLVMValueRef
LLVMConstLShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
451 return wrap(ConstantExpr::getLShr(unwrap
<Constant
>(LHSConstant
),
452 unwrap
<Constant
>(RHSConstant
)));
455 LLVMValueRef
LLVMConstAShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
456 return wrap(ConstantExpr::getAShr(unwrap
<Constant
>(LHSConstant
),
457 unwrap
<Constant
>(RHSConstant
)));
460 LLVMValueRef
LLVMConstGEP(LLVMValueRef ConstantVal
,
461 LLVMValueRef
*ConstantIndices
, unsigned NumIndices
) {
462 return wrap(ConstantExpr::getGetElementPtr(unwrap
<Constant
>(ConstantVal
),
463 unwrap
<Constant
>(ConstantIndices
,
468 LLVMValueRef
LLVMConstTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
469 return wrap(ConstantExpr::getTrunc(unwrap
<Constant
>(ConstantVal
),
473 LLVMValueRef
LLVMConstSExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
474 return wrap(ConstantExpr::getSExt(unwrap
<Constant
>(ConstantVal
),
478 LLVMValueRef
LLVMConstZExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
479 return wrap(ConstantExpr::getZExt(unwrap
<Constant
>(ConstantVal
),
483 LLVMValueRef
LLVMConstFPTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
484 return wrap(ConstantExpr::getFPTrunc(unwrap
<Constant
>(ConstantVal
),
488 LLVMValueRef
LLVMConstFPExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
489 return wrap(ConstantExpr::getFPExtend(unwrap
<Constant
>(ConstantVal
),
493 LLVMValueRef
LLVMConstUIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
494 return wrap(ConstantExpr::getUIToFP(unwrap
<Constant
>(ConstantVal
),
498 LLVMValueRef
LLVMConstSIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
499 return wrap(ConstantExpr::getSIToFP(unwrap
<Constant
>(ConstantVal
),
503 LLVMValueRef
LLVMConstFPToUI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
504 return wrap(ConstantExpr::getFPToUI(unwrap
<Constant
>(ConstantVal
),
508 LLVMValueRef
LLVMConstFPToSI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
509 return wrap(ConstantExpr::getFPToSI(unwrap
<Constant
>(ConstantVal
),
513 LLVMValueRef
LLVMConstPtrToInt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
514 return wrap(ConstantExpr::getPtrToInt(unwrap
<Constant
>(ConstantVal
),
518 LLVMValueRef
LLVMConstIntToPtr(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
519 return wrap(ConstantExpr::getIntToPtr(unwrap
<Constant
>(ConstantVal
),
523 LLVMValueRef
LLVMConstBitCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
524 return wrap(ConstantExpr::getBitCast(unwrap
<Constant
>(ConstantVal
),
528 LLVMValueRef
LLVMConstSelect(LLVMValueRef ConstantCondition
,
529 LLVMValueRef ConstantIfTrue
,
530 LLVMValueRef ConstantIfFalse
) {
531 return wrap(ConstantExpr::getSelect(unwrap
<Constant
>(ConstantCondition
),
532 unwrap
<Constant
>(ConstantIfTrue
),
533 unwrap
<Constant
>(ConstantIfFalse
)));
536 LLVMValueRef
LLVMConstExtractElement(LLVMValueRef VectorConstant
,
537 LLVMValueRef IndexConstant
) {
538 return wrap(ConstantExpr::getExtractElement(unwrap
<Constant
>(VectorConstant
),
539 unwrap
<Constant
>(IndexConstant
)));
542 LLVMValueRef
LLVMConstInsertElement(LLVMValueRef VectorConstant
,
543 LLVMValueRef ElementValueConstant
,
544 LLVMValueRef IndexConstant
) {
545 return wrap(ConstantExpr::getInsertElement(unwrap
<Constant
>(VectorConstant
),
546 unwrap
<Constant
>(ElementValueConstant
),
547 unwrap
<Constant
>(IndexConstant
)));
550 LLVMValueRef
LLVMConstShuffleVector(LLVMValueRef VectorAConstant
,
551 LLVMValueRef VectorBConstant
,
552 LLVMValueRef MaskConstant
) {
553 return wrap(ConstantExpr::getShuffleVector(unwrap
<Constant
>(VectorAConstant
),
554 unwrap
<Constant
>(VectorBConstant
),
555 unwrap
<Constant
>(MaskConstant
)));
558 LLVMValueRef
LLVMConstExtractValue(LLVMValueRef AggConstant
, unsigned *IdxList
,
560 return wrap(ConstantExpr::getExtractValue(unwrap
<Constant
>(AggConstant
),
564 LLVMValueRef
LLVMConstInsertValue(LLVMValueRef AggConstant
,
565 LLVMValueRef ElementValueConstant
,
566 unsigned *IdxList
, unsigned NumIdx
) {
567 return wrap(ConstantExpr::getInsertValue(unwrap
<Constant
>(AggConstant
),
568 unwrap
<Constant
>(ElementValueConstant
),
572 LLVMValueRef
LLVMConstInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
573 const char *Constraints
, int HasSideEffects
) {
574 return wrap(InlineAsm::get(dyn_cast
<FunctionType
>(unwrap(Ty
)), AsmString
,
575 Constraints
, HasSideEffects
));
578 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
580 LLVMModuleRef
LLVMGetGlobalParent(LLVMValueRef Global
) {
581 return wrap(unwrap
<GlobalValue
>(Global
)->getParent());
584 int LLVMIsDeclaration(LLVMValueRef Global
) {
585 return unwrap
<GlobalValue
>(Global
)->isDeclaration();
588 LLVMLinkage
LLVMGetLinkage(LLVMValueRef Global
) {
589 return static_cast<LLVMLinkage
>(unwrap
<GlobalValue
>(Global
)->getLinkage());
592 void LLVMSetLinkage(LLVMValueRef Global
, LLVMLinkage Linkage
) {
593 unwrap
<GlobalValue
>(Global
)
594 ->setLinkage(static_cast<GlobalValue::LinkageTypes
>(Linkage
));
597 const char *LLVMGetSection(LLVMValueRef Global
) {
598 return unwrap
<GlobalValue
>(Global
)->getSection().c_str();
601 void LLVMSetSection(LLVMValueRef Global
, const char *Section
) {
602 unwrap
<GlobalValue
>(Global
)->setSection(Section
);
605 LLVMVisibility
LLVMGetVisibility(LLVMValueRef Global
) {
606 return static_cast<LLVMVisibility
>(
607 unwrap
<GlobalValue
>(Global
)->getVisibility());
610 void LLVMSetVisibility(LLVMValueRef Global
, LLVMVisibility Viz
) {
611 unwrap
<GlobalValue
>(Global
)
612 ->setVisibility(static_cast<GlobalValue::VisibilityTypes
>(Viz
));
615 unsigned LLVMGetAlignment(LLVMValueRef Global
) {
616 return unwrap
<GlobalValue
>(Global
)->getAlignment();
619 void LLVMSetAlignment(LLVMValueRef Global
, unsigned Bytes
) {
620 unwrap
<GlobalValue
>(Global
)->setAlignment(Bytes
);
623 /*--.. Operations on global variables ......................................--*/
625 LLVMValueRef
LLVMAddGlobal(LLVMModuleRef M
, LLVMTypeRef Ty
, const char *Name
) {
626 return wrap(new GlobalVariable(unwrap(Ty
), false,
627 GlobalValue::ExternalLinkage
, 0, Name
,
631 LLVMValueRef
LLVMGetNamedGlobal(LLVMModuleRef M
, const char *Name
) {
632 return wrap(unwrap(M
)->getNamedGlobal(Name
));
635 LLVMValueRef
LLVMGetFirstGlobal(LLVMModuleRef M
) {
636 Module
*Mod
= unwrap(M
);
637 Module::global_iterator I
= Mod
->global_begin();
638 if (I
== Mod
->global_end())
643 LLVMValueRef
LLVMGetLastGlobal(LLVMModuleRef M
) {
644 Module
*Mod
= unwrap(M
);
645 Module::global_iterator I
= Mod
->global_end();
646 if (I
== Mod
->global_begin())
651 LLVMValueRef
LLVMGetNextGlobal(LLVMValueRef GlobalVar
) {
652 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
653 Module::global_iterator I
= GV
;
654 if (++I
== GV
->getParent()->global_end())
659 LLVMValueRef
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar
) {
660 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
661 Module::global_iterator I
= GV
;
662 if (I
== GV
->getParent()->global_begin())
667 void LLVMDeleteGlobal(LLVMValueRef GlobalVar
) {
668 unwrap
<GlobalVariable
>(GlobalVar
)->eraseFromParent();
671 LLVMValueRef
LLVMGetInitializer(LLVMValueRef GlobalVar
) {
672 return wrap(unwrap
<GlobalVariable
>(GlobalVar
)->getInitializer());
675 void LLVMSetInitializer(LLVMValueRef GlobalVar
, LLVMValueRef ConstantVal
) {
676 unwrap
<GlobalVariable
>(GlobalVar
)
677 ->setInitializer(unwrap
<Constant
>(ConstantVal
));
680 int LLVMIsThreadLocal(LLVMValueRef GlobalVar
) {
681 return unwrap
<GlobalVariable
>(GlobalVar
)->isThreadLocal();
684 void LLVMSetThreadLocal(LLVMValueRef GlobalVar
, int IsThreadLocal
) {
685 unwrap
<GlobalVariable
>(GlobalVar
)->setThreadLocal(IsThreadLocal
!= 0);
688 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar
) {
689 return unwrap
<GlobalVariable
>(GlobalVar
)->isConstant();
692 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar
, int IsConstant
) {
693 unwrap
<GlobalVariable
>(GlobalVar
)->setConstant(IsConstant
!= 0);
696 /*--.. Operations on aliases ......................................--*/
698 LLVMValueRef
LLVMAddAlias(LLVMModuleRef M
, LLVMTypeRef Ty
, LLVMValueRef Aliasee
,
700 return wrap(new GlobalAlias(unwrap(Ty
), GlobalValue::ExternalLinkage
, Name
,
701 unwrap
<Constant
>(Aliasee
), unwrap (M
)));
704 /*--.. Operations on functions .............................................--*/
706 LLVMValueRef
LLVMAddFunction(LLVMModuleRef M
, const char *Name
,
707 LLVMTypeRef FunctionTy
) {
708 return wrap(Function::Create(unwrap
<FunctionType
>(FunctionTy
),
709 GlobalValue::ExternalLinkage
, Name
, unwrap(M
)));
712 LLVMValueRef
LLVMGetNamedFunction(LLVMModuleRef M
, const char *Name
) {
713 return wrap(unwrap(M
)->getFunction(Name
));
716 LLVMValueRef
LLVMGetFirstFunction(LLVMModuleRef M
) {
717 Module
*Mod
= unwrap(M
);
718 Module::iterator I
= Mod
->begin();
724 LLVMValueRef
LLVMGetLastFunction(LLVMModuleRef M
) {
725 Module
*Mod
= unwrap(M
);
726 Module::iterator I
= Mod
->end();
727 if (I
== Mod
->begin())
732 LLVMValueRef
LLVMGetNextFunction(LLVMValueRef Fn
) {
733 Function
*Func
= unwrap
<Function
>(Fn
);
734 Module::iterator I
= Func
;
735 if (++I
== Func
->getParent()->end())
740 LLVMValueRef
LLVMGetPreviousFunction(LLVMValueRef Fn
) {
741 Function
*Func
= unwrap
<Function
>(Fn
);
742 Module::iterator I
= Func
;
743 if (I
== Func
->getParent()->begin())
748 void LLVMDeleteFunction(LLVMValueRef Fn
) {
749 unwrap
<Function
>(Fn
)->eraseFromParent();
752 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn
) {
753 if (Function
*F
= dyn_cast
<Function
>(unwrap(Fn
)))
754 return F
->getIntrinsicID();
758 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn
) {
759 return unwrap
<Function
>(Fn
)->getCallingConv();
762 void LLVMSetFunctionCallConv(LLVMValueRef Fn
, unsigned CC
) {
763 return unwrap
<Function
>(Fn
)->setCallingConv(CC
);
766 const char *LLVMGetGC(LLVMValueRef Fn
) {
767 Function
*F
= unwrap
<Function
>(Fn
);
768 return F
->hasGC()? F
->getGC() : 0;
771 void LLVMSetGC(LLVMValueRef Fn
, const char *GC
) {
772 Function
*F
= unwrap
<Function
>(Fn
);
779 /*--.. Operations on parameters ............................................--*/
781 unsigned LLVMCountParams(LLVMValueRef FnRef
) {
782 // This function is strictly redundant to
783 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
784 return unwrap
<Function
>(FnRef
)->arg_size();
787 void LLVMGetParams(LLVMValueRef FnRef
, LLVMValueRef
*ParamRefs
) {
788 Function
*Fn
= unwrap
<Function
>(FnRef
);
789 for (Function::arg_iterator I
= Fn
->arg_begin(),
790 E
= Fn
->arg_end(); I
!= E
; I
++)
791 *ParamRefs
++ = wrap(I
);
794 LLVMValueRef
LLVMGetParam(LLVMValueRef FnRef
, unsigned index
) {
795 Function::arg_iterator AI
= unwrap
<Function
>(FnRef
)->arg_begin();
801 LLVMValueRef
LLVMGetParamParent(LLVMValueRef V
) {
802 return wrap(unwrap
<Argument
>(V
)->getParent());
805 LLVMValueRef
LLVMGetFirstParam(LLVMValueRef Fn
) {
806 Function
*Func
= unwrap
<Function
>(Fn
);
807 Function::arg_iterator I
= Func
->arg_begin();
808 if (I
== Func
->arg_end())
813 LLVMValueRef
LLVMGetLastParam(LLVMValueRef Fn
) {
814 Function
*Func
= unwrap
<Function
>(Fn
);
815 Function::arg_iterator I
= Func
->arg_end();
816 if (I
== Func
->arg_begin())
821 LLVMValueRef
LLVMGetNextParam(LLVMValueRef Arg
) {
822 Argument
*A
= unwrap
<Argument
>(Arg
);
823 Function::arg_iterator I
= A
;
824 if (++I
== A
->getParent()->arg_end())
829 LLVMValueRef
LLVMGetPreviousParam(LLVMValueRef Arg
) {
830 Argument
*A
= unwrap
<Argument
>(Arg
);
831 Function::arg_iterator I
= A
;
832 if (I
== A
->getParent()->arg_begin())
837 void LLVMAddAttribute(LLVMValueRef Arg
, LLVMAttribute PA
) {
838 unwrap
<Argument
>(Arg
)->addAttr(PA
);
841 void LLVMRemoveAttribute(LLVMValueRef Arg
, LLVMAttribute PA
) {
842 unwrap
<Argument
>(Arg
)->removeAttr(PA
);
845 void LLVMSetParamAlignment(LLVMValueRef Arg
, unsigned align
) {
846 unwrap
<Argument
>(Arg
)->addAttr(
847 Attribute::constructAlignmentFromInt(align
));
850 /*--.. Operations on basic blocks ..........................................--*/
852 LLVMValueRef
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB
) {
853 return wrap(static_cast<Value
*>(unwrap(BB
)));
856 int LLVMValueIsBasicBlock(LLVMValueRef Val
) {
857 return isa
<BasicBlock
>(unwrap(Val
));
860 LLVMBasicBlockRef
LLVMValueAsBasicBlock(LLVMValueRef Val
) {
861 return wrap(unwrap
<BasicBlock
>(Val
));
864 LLVMValueRef
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB
) {
865 return wrap(unwrap(BB
)->getParent());
868 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef
) {
869 return unwrap
<Function
>(FnRef
)->size();
872 void LLVMGetBasicBlocks(LLVMValueRef FnRef
, LLVMBasicBlockRef
*BasicBlocksRefs
){
873 Function
*Fn
= unwrap
<Function
>(FnRef
);
874 for (Function::iterator I
= Fn
->begin(), E
= Fn
->end(); I
!= E
; I
++)
875 *BasicBlocksRefs
++ = wrap(I
);
878 LLVMBasicBlockRef
LLVMGetEntryBasicBlock(LLVMValueRef Fn
) {
879 return wrap(&unwrap
<Function
>(Fn
)->getEntryBlock());
882 LLVMBasicBlockRef
LLVMGetFirstBasicBlock(LLVMValueRef Fn
) {
883 Function
*Func
= unwrap
<Function
>(Fn
);
884 Function::iterator I
= Func
->begin();
885 if (I
== Func
->end())
890 LLVMBasicBlockRef
LLVMGetLastBasicBlock(LLVMValueRef Fn
) {
891 Function
*Func
= unwrap
<Function
>(Fn
);
892 Function::iterator I
= Func
->end();
893 if (I
== Func
->begin())
898 LLVMBasicBlockRef
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB
) {
899 BasicBlock
*Block
= unwrap(BB
);
900 Function::iterator I
= Block
;
901 if (++I
== Block
->getParent()->end())
906 LLVMBasicBlockRef
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB
) {
907 BasicBlock
*Block
= unwrap(BB
);
908 Function::iterator I
= Block
;
909 if (I
== Block
->getParent()->begin())
914 LLVMBasicBlockRef
LLVMAppendBasicBlock(LLVMValueRef FnRef
, const char *Name
) {
915 return wrap(BasicBlock::Create(Name
, unwrap
<Function
>(FnRef
)));
918 LLVMBasicBlockRef
LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef
,
920 BasicBlock
*InsertBeforeBB
= unwrap(InsertBeforeBBRef
);
921 return wrap(BasicBlock::Create(Name
, InsertBeforeBB
->getParent(),
925 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef
) {
926 unwrap(BBRef
)->eraseFromParent();
929 /*--.. Operations on instructions ..........................................--*/
931 LLVMBasicBlockRef
LLVMGetInstructionParent(LLVMValueRef Inst
) {
932 return wrap(unwrap
<Instruction
>(Inst
)->getParent());
935 LLVMValueRef
LLVMGetFirstInstruction(LLVMBasicBlockRef BB
) {
936 BasicBlock
*Block
= unwrap(BB
);
937 BasicBlock::iterator I
= Block
->begin();
938 if (I
== Block
->end())
943 LLVMValueRef
LLVMGetLastInstruction(LLVMBasicBlockRef BB
) {
944 BasicBlock
*Block
= unwrap(BB
);
945 BasicBlock::iterator I
= Block
->end();
946 if (I
== Block
->begin())
951 LLVMValueRef
LLVMGetNextInstruction(LLVMValueRef Inst
) {
952 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
953 BasicBlock::iterator I
= Instr
;
954 if (++I
== Instr
->getParent()->end())
959 LLVMValueRef
LLVMGetPreviousInstruction(LLVMValueRef Inst
) {
960 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
961 BasicBlock::iterator I
= Instr
;
962 if (I
== Instr
->getParent()->begin())
967 /*--.. Call and invoke instructions ........................................--*/
969 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr
) {
970 Value
*V
= unwrap(Instr
);
971 if (CallInst
*CI
= dyn_cast
<CallInst
>(V
))
972 return CI
->getCallingConv();
973 else if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(V
))
974 return II
->getCallingConv();
975 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
979 void LLVMSetInstructionCallConv(LLVMValueRef Instr
, unsigned CC
) {
980 Value
*V
= unwrap(Instr
);
981 if (CallInst
*CI
= dyn_cast
<CallInst
>(V
))
982 return CI
->setCallingConv(CC
);
983 else if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(V
))
984 return II
->setCallingConv(CC
);
985 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
988 void LLVMAddInstrAttribute(LLVMValueRef Instr
, unsigned index
,
990 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
992 Call
.getAttributes().addAttr(index
, PA
));
995 void LLVMRemoveInstrAttribute(LLVMValueRef Instr
, unsigned index
,
997 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
999 Call
.getAttributes().removeAttr(index
, PA
));
1002 void LLVMSetInstrParamAlignment(LLVMValueRef Instr
, unsigned index
,
1004 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
1006 Call
.getAttributes().addAttr(index
,
1007 Attribute::constructAlignmentFromInt(align
)));
1010 /*--.. Operations on call instructions (only) ..............................--*/
1012 int LLVMIsTailCall(LLVMValueRef Call
) {
1013 return unwrap
<CallInst
>(Call
)->isTailCall();
1016 void LLVMSetTailCall(LLVMValueRef Call
, int isTailCall
) {
1017 unwrap
<CallInst
>(Call
)->setTailCall(isTailCall
);
1020 /*--.. Operations on phi nodes .............................................--*/
1022 void LLVMAddIncoming(LLVMValueRef PhiNode
, LLVMValueRef
*IncomingValues
,
1023 LLVMBasicBlockRef
*IncomingBlocks
, unsigned Count
) {
1024 PHINode
*PhiVal
= unwrap
<PHINode
>(PhiNode
);
1025 for (unsigned I
= 0; I
!= Count
; ++I
)
1026 PhiVal
->addIncoming(unwrap(IncomingValues
[I
]), unwrap(IncomingBlocks
[I
]));
1029 unsigned LLVMCountIncoming(LLVMValueRef PhiNode
) {
1030 return unwrap
<PHINode
>(PhiNode
)->getNumIncomingValues();
1033 LLVMValueRef
LLVMGetIncomingValue(LLVMValueRef PhiNode
, unsigned Index
) {
1034 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingValue(Index
));
1037 LLVMBasicBlockRef
LLVMGetIncomingBlock(LLVMValueRef PhiNode
, unsigned Index
) {
1038 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingBlock(Index
));
1042 /*===-- Instruction builders ----------------------------------------------===*/
1044 LLVMBuilderRef
LLVMCreateBuilder(void) {
1045 return wrap(new IRBuilder
<>());
1048 void LLVMPositionBuilder(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
,
1049 LLVMValueRef Instr
) {
1050 BasicBlock
*BB
= unwrap(Block
);
1051 Instruction
*I
= Instr
? unwrap
<Instruction
>(Instr
) : (Instruction
*) BB
->end();
1052 unwrap(Builder
)->SetInsertPoint(BB
, I
);
1055 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
1056 Instruction
*I
= unwrap
<Instruction
>(Instr
);
1057 unwrap(Builder
)->SetInsertPoint(I
->getParent(), I
);
1060 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
) {
1061 BasicBlock
*BB
= unwrap(Block
);
1062 unwrap(Builder
)->SetInsertPoint(BB
);
1065 LLVMBasicBlockRef
LLVMGetInsertBlock(LLVMBuilderRef Builder
) {
1066 return wrap(unwrap(Builder
)->GetInsertBlock());
1069 void LLVMClearInsertionPosition(LLVMBuilderRef Builder
) {
1070 unwrap(Builder
)->ClearInsertionPoint ();
1073 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
1074 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
));
1077 void LLVMDisposeBuilder(LLVMBuilderRef Builder
) {
1078 delete unwrap(Builder
);
1081 /*--.. Instruction builders ................................................--*/
1083 LLVMValueRef
LLVMBuildRetVoid(LLVMBuilderRef B
) {
1084 return wrap(unwrap(B
)->CreateRetVoid());
1087 LLVMValueRef
LLVMBuildRet(LLVMBuilderRef B
, LLVMValueRef V
) {
1088 return wrap(unwrap(B
)->CreateRet(unwrap(V
)));
1091 LLVMValueRef
LLVMBuildBr(LLVMBuilderRef B
, LLVMBasicBlockRef Dest
) {
1092 return wrap(unwrap(B
)->CreateBr(unwrap(Dest
)));
1095 LLVMValueRef
LLVMBuildCondBr(LLVMBuilderRef B
, LLVMValueRef If
,
1096 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Else
) {
1097 return wrap(unwrap(B
)->CreateCondBr(unwrap(If
), unwrap(Then
), unwrap(Else
)));
1100 LLVMValueRef
LLVMBuildSwitch(LLVMBuilderRef B
, LLVMValueRef V
,
1101 LLVMBasicBlockRef Else
, unsigned NumCases
) {
1102 return wrap(unwrap(B
)->CreateSwitch(unwrap(V
), unwrap(Else
), NumCases
));
1105 LLVMValueRef
LLVMBuildInvoke(LLVMBuilderRef B
, LLVMValueRef Fn
,
1106 LLVMValueRef
*Args
, unsigned NumArgs
,
1107 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
1109 return wrap(unwrap(B
)->CreateInvoke(unwrap(Fn
), unwrap(Then
), unwrap(Catch
),
1110 unwrap(Args
), unwrap(Args
) + NumArgs
,
1114 LLVMValueRef
LLVMBuildUnwind(LLVMBuilderRef B
) {
1115 return wrap(unwrap(B
)->CreateUnwind());
1118 LLVMValueRef
LLVMBuildUnreachable(LLVMBuilderRef B
) {
1119 return wrap(unwrap(B
)->CreateUnreachable());
1122 void LLVMAddCase(LLVMValueRef Switch
, LLVMValueRef OnVal
,
1123 LLVMBasicBlockRef Dest
) {
1124 unwrap
<SwitchInst
>(Switch
)->addCase(unwrap
<ConstantInt
>(OnVal
), unwrap(Dest
));
1127 /*--.. Arithmetic ..........................................................--*/
1129 LLVMValueRef
LLVMBuildAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1131 return wrap(unwrap(B
)->CreateAdd(unwrap(LHS
), unwrap(RHS
), Name
));
1134 LLVMValueRef
LLVMBuildSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1136 return wrap(unwrap(B
)->CreateSub(unwrap(LHS
), unwrap(RHS
), Name
));
1139 LLVMValueRef
LLVMBuildMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1141 return wrap(unwrap(B
)->CreateMul(unwrap(LHS
), unwrap(RHS
), Name
));
1144 LLVMValueRef
LLVMBuildUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1146 return wrap(unwrap(B
)->CreateUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
1149 LLVMValueRef
LLVMBuildSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1151 return wrap(unwrap(B
)->CreateSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
1154 LLVMValueRef
LLVMBuildFDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1156 return wrap(unwrap(B
)->CreateFDiv(unwrap(LHS
), unwrap(RHS
), Name
));
1159 LLVMValueRef
LLVMBuildURem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1161 return wrap(unwrap(B
)->CreateURem(unwrap(LHS
), unwrap(RHS
), Name
));
1164 LLVMValueRef
LLVMBuildSRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1166 return wrap(unwrap(B
)->CreateSRem(unwrap(LHS
), unwrap(RHS
), Name
));
1169 LLVMValueRef
LLVMBuildFRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1171 return wrap(unwrap(B
)->CreateFRem(unwrap(LHS
), unwrap(RHS
), Name
));
1174 LLVMValueRef
LLVMBuildShl(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1176 return wrap(unwrap(B
)->CreateShl(unwrap(LHS
), unwrap(RHS
), Name
));
1179 LLVMValueRef
LLVMBuildLShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1181 return wrap(unwrap(B
)->CreateLShr(unwrap(LHS
), unwrap(RHS
), Name
));
1184 LLVMValueRef
LLVMBuildAShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1186 return wrap(unwrap(B
)->CreateAShr(unwrap(LHS
), unwrap(RHS
), Name
));
1189 LLVMValueRef
LLVMBuildAnd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1191 return wrap(unwrap(B
)->CreateAnd(unwrap(LHS
), unwrap(RHS
), Name
));
1194 LLVMValueRef
LLVMBuildOr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1196 return wrap(unwrap(B
)->CreateOr(unwrap(LHS
), unwrap(RHS
), Name
));
1199 LLVMValueRef
LLVMBuildXor(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1201 return wrap(unwrap(B
)->CreateXor(unwrap(LHS
), unwrap(RHS
), Name
));
1204 LLVMValueRef
LLVMBuildNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
1205 return wrap(unwrap(B
)->CreateNeg(unwrap(V
), Name
));
1208 LLVMValueRef
LLVMBuildNot(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
1209 return wrap(unwrap(B
)->CreateNot(unwrap(V
), Name
));
1212 /*--.. Memory ..............................................................--*/
1214 LLVMValueRef
LLVMBuildMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1216 return wrap(unwrap(B
)->CreateMalloc(unwrap(Ty
), 0, Name
));
1219 LLVMValueRef
LLVMBuildArrayMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1220 LLVMValueRef Val
, const char *Name
) {
1221 return wrap(unwrap(B
)->CreateMalloc(unwrap(Ty
), unwrap(Val
), Name
));
1224 LLVMValueRef
LLVMBuildAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1226 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), 0, Name
));
1229 LLVMValueRef
LLVMBuildArrayAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1230 LLVMValueRef Val
, const char *Name
) {
1231 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), unwrap(Val
), Name
));
1234 LLVMValueRef
LLVMBuildFree(LLVMBuilderRef B
, LLVMValueRef PointerVal
) {
1235 return wrap(unwrap(B
)->CreateFree(unwrap(PointerVal
)));
1239 LLVMValueRef
LLVMBuildLoad(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
1241 return wrap(unwrap(B
)->CreateLoad(unwrap(PointerVal
), Name
));
1244 LLVMValueRef
LLVMBuildStore(LLVMBuilderRef B
, LLVMValueRef Val
,
1245 LLVMValueRef PointerVal
) {
1246 return wrap(unwrap(B
)->CreateStore(unwrap(Val
), unwrap(PointerVal
)));
1249 LLVMValueRef
LLVMBuildGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
1250 LLVMValueRef
*Indices
, unsigned NumIndices
,
1252 return wrap(unwrap(B
)->CreateGEP(unwrap(Pointer
), unwrap(Indices
),
1253 unwrap(Indices
) + NumIndices
, Name
));
1256 /*--.. Casts ...............................................................--*/
1258 LLVMValueRef
LLVMBuildTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
1259 LLVMTypeRef DestTy
, const char *Name
) {
1260 return wrap(unwrap(B
)->CreateTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
1263 LLVMValueRef
LLVMBuildZExt(LLVMBuilderRef B
, LLVMValueRef Val
,
1264 LLVMTypeRef DestTy
, const char *Name
) {
1265 return wrap(unwrap(B
)->CreateZExt(unwrap(Val
), unwrap(DestTy
), Name
));
1268 LLVMValueRef
LLVMBuildSExt(LLVMBuilderRef B
, LLVMValueRef Val
,
1269 LLVMTypeRef DestTy
, const char *Name
) {
1270 return wrap(unwrap(B
)->CreateSExt(unwrap(Val
), unwrap(DestTy
), Name
));
1273 LLVMValueRef
LLVMBuildFPToUI(LLVMBuilderRef B
, LLVMValueRef Val
,
1274 LLVMTypeRef DestTy
, const char *Name
) {
1275 return wrap(unwrap(B
)->CreateFPToUI(unwrap(Val
), unwrap(DestTy
), Name
));
1278 LLVMValueRef
LLVMBuildFPToSI(LLVMBuilderRef B
, LLVMValueRef Val
,
1279 LLVMTypeRef DestTy
, const char *Name
) {
1280 return wrap(unwrap(B
)->CreateFPToSI(unwrap(Val
), unwrap(DestTy
), Name
));
1283 LLVMValueRef
LLVMBuildUIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
1284 LLVMTypeRef DestTy
, const char *Name
) {
1285 return wrap(unwrap(B
)->CreateUIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
1288 LLVMValueRef
LLVMBuildSIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
1289 LLVMTypeRef DestTy
, const char *Name
) {
1290 return wrap(unwrap(B
)->CreateSIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
1293 LLVMValueRef
LLVMBuildFPTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
1294 LLVMTypeRef DestTy
, const char *Name
) {
1295 return wrap(unwrap(B
)->CreateFPTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
1298 LLVMValueRef
LLVMBuildFPExt(LLVMBuilderRef B
, LLVMValueRef Val
,
1299 LLVMTypeRef DestTy
, const char *Name
) {
1300 return wrap(unwrap(B
)->CreateFPExt(unwrap(Val
), unwrap(DestTy
), Name
));
1303 LLVMValueRef
LLVMBuildPtrToInt(LLVMBuilderRef B
, LLVMValueRef Val
,
1304 LLVMTypeRef DestTy
, const char *Name
) {
1305 return wrap(unwrap(B
)->CreatePtrToInt(unwrap(Val
), unwrap(DestTy
), Name
));
1308 LLVMValueRef
LLVMBuildIntToPtr(LLVMBuilderRef B
, LLVMValueRef Val
,
1309 LLVMTypeRef DestTy
, const char *Name
) {
1310 return wrap(unwrap(B
)->CreateIntToPtr(unwrap(Val
), unwrap(DestTy
), Name
));
1313 LLVMValueRef
LLVMBuildBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
1314 LLVMTypeRef DestTy
, const char *Name
) {
1315 return wrap(unwrap(B
)->CreateBitCast(unwrap(Val
), unwrap(DestTy
), Name
));
1318 /*--.. Comparisons .........................................................--*/
1320 LLVMValueRef
LLVMBuildICmp(LLVMBuilderRef B
, LLVMIntPredicate Op
,
1321 LLVMValueRef LHS
, LLVMValueRef RHS
,
1323 return wrap(unwrap(B
)->CreateICmp(static_cast<ICmpInst::Predicate
>(Op
),
1324 unwrap(LHS
), unwrap(RHS
), Name
));
1327 LLVMValueRef
LLVMBuildFCmp(LLVMBuilderRef B
, LLVMRealPredicate Op
,
1328 LLVMValueRef LHS
, LLVMValueRef RHS
,
1330 return wrap(unwrap(B
)->CreateFCmp(static_cast<FCmpInst::Predicate
>(Op
),
1331 unwrap(LHS
), unwrap(RHS
), Name
));
1334 /*--.. Miscellaneous instructions ..........................................--*/
1336 LLVMValueRef
LLVMBuildPhi(LLVMBuilderRef B
, LLVMTypeRef Ty
, const char *Name
) {
1337 return wrap(unwrap(B
)->CreatePHI(unwrap(Ty
), Name
));
1340 LLVMValueRef
LLVMBuildCall(LLVMBuilderRef B
, LLVMValueRef Fn
,
1341 LLVMValueRef
*Args
, unsigned NumArgs
,
1343 return wrap(unwrap(B
)->CreateCall(unwrap(Fn
), unwrap(Args
),
1344 unwrap(Args
) + NumArgs
, Name
));
1347 LLVMValueRef
LLVMBuildSelect(LLVMBuilderRef B
, LLVMValueRef If
,
1348 LLVMValueRef Then
, LLVMValueRef Else
,
1350 return wrap(unwrap(B
)->CreateSelect(unwrap(If
), unwrap(Then
), unwrap(Else
),
1354 LLVMValueRef
LLVMBuildVAArg(LLVMBuilderRef B
, LLVMValueRef List
,
1355 LLVMTypeRef Ty
, const char *Name
) {
1356 return wrap(unwrap(B
)->CreateVAArg(unwrap(List
), unwrap(Ty
), Name
));
1359 LLVMValueRef
LLVMBuildExtractElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
1360 LLVMValueRef Index
, const char *Name
) {
1361 return wrap(unwrap(B
)->CreateExtractElement(unwrap(VecVal
), unwrap(Index
),
1365 LLVMValueRef
LLVMBuildInsertElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
1366 LLVMValueRef EltVal
, LLVMValueRef Index
,
1368 return wrap(unwrap(B
)->CreateInsertElement(unwrap(VecVal
), unwrap(EltVal
),
1369 unwrap(Index
), Name
));
1372 LLVMValueRef
LLVMBuildShuffleVector(LLVMBuilderRef B
, LLVMValueRef V1
,
1373 LLVMValueRef V2
, LLVMValueRef Mask
,
1375 return wrap(unwrap(B
)->CreateShuffleVector(unwrap(V1
), unwrap(V2
),
1376 unwrap(Mask
), Name
));
1379 LLVMValueRef
LLVMBuildExtractValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
1380 unsigned Index
, const char *Name
) {
1381 return wrap(unwrap(B
)->CreateExtractValue(unwrap(AggVal
), Index
, Name
));
1384 LLVMValueRef
LLVMBuildInsertValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
1385 LLVMValueRef EltVal
, unsigned Index
,
1387 return wrap(unwrap(B
)->CreateInsertValue(unwrap(AggVal
), unwrap(EltVal
),
1392 /*===-- Module providers --------------------------------------------------===*/
1394 LLVMModuleProviderRef
1395 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M
) {
1396 return wrap(new ExistingModuleProvider(unwrap(M
)));
1399 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP
) {
1404 /*===-- Memory buffers ----------------------------------------------------===*/
1406 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path
,
1407 LLVMMemoryBufferRef
*OutMemBuf
,
1408 char **OutMessage
) {
1410 if (MemoryBuffer
*MB
= MemoryBuffer::getFile(Path
, &Error
)) {
1411 *OutMemBuf
= wrap(MB
);
1415 *OutMessage
= strdup(Error
.c_str());
1419 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef
*OutMemBuf
,
1420 char **OutMessage
) {
1421 if (MemoryBuffer
*MB
= MemoryBuffer::getSTDIN()) {
1422 *OutMemBuf
= wrap(MB
);
1426 *OutMessage
= strdup("stdin is empty.");
1430 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf
) {
1431 delete unwrap(MemBuf
);