1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/ErrorHandling.h"
36 /*===-- Error handling ----------------------------------------------------===*/
38 void LLVMDisposeMessage(char *Message
) {
43 /*===-- Operations on contexts --------------------------------------------===*/
45 LLVMContextRef
LLVMContextCreate() {
46 return wrap(new LLVMContext());
49 LLVMContextRef
LLVMGetGlobalContext() {
50 return wrap(&getGlobalContext());
53 void LLVMContextDispose(LLVMContextRef C
) {
58 /*===-- Operations on modules ---------------------------------------------===*/
60 LLVMModuleRef
LLVMModuleCreateWithName(const char *ModuleID
) {
61 return wrap(new Module(ModuleID
, getGlobalContext()));
64 LLVMModuleRef
LLVMModuleCreateWithNameInContext(const char *ModuleID
,
66 return wrap(new Module(ModuleID
, *unwrap(C
)));
69 void LLVMDisposeModule(LLVMModuleRef M
) {
73 /*--.. Data layout .........................................................--*/
74 const char * LLVMGetDataLayout(LLVMModuleRef M
) {
75 return unwrap(M
)->getDataLayout().c_str();
78 void LLVMSetDataLayout(LLVMModuleRef M
, const char *Triple
) {
79 unwrap(M
)->setDataLayout(Triple
);
82 /*--.. Target triple .......................................................--*/
83 const char * LLVMGetTarget(LLVMModuleRef M
) {
84 return unwrap(M
)->getTargetTriple().c_str();
87 void LLVMSetTarget(LLVMModuleRef M
, const char *Triple
) {
88 unwrap(M
)->setTargetTriple(Triple
);
91 /*--.. Type names ..........................................................--*/
92 int LLVMAddTypeName(LLVMModuleRef M
, const char *Name
, LLVMTypeRef Ty
) {
93 return unwrap(M
)->addTypeName(Name
, unwrap(Ty
));
96 void LLVMDeleteTypeName(LLVMModuleRef M
, const char *Name
) {
99 TypeSymbolTable
&TST
= unwrap(M
)->getTypeSymbolTable();
100 for (TypeSymbolTable::iterator I
= TST
.begin(), E
= TST
.end(); I
!= E
; ++I
)
105 LLVMTypeRef
LLVMGetTypeByName(LLVMModuleRef M
, const char *Name
) {
107 return wrap(unwrap(M
)->getTypeByName(N
));
110 void LLVMDumpModule(LLVMModuleRef M
) {
115 /*===-- Operations on types -----------------------------------------------===*/
117 /*--.. Operations on all types (mostly) ....................................--*/
119 LLVMTypeKind
LLVMGetTypeKind(LLVMTypeRef Ty
) {
120 switch (unwrap(Ty
)->getTypeID()) {
122 assert(false && "Unhandled TypeID.");
124 return LLVMVoidTypeKind
;
125 case Type::FloatTyID
:
126 return LLVMFloatTypeKind
;
127 case Type::DoubleTyID
:
128 return LLVMDoubleTypeKind
;
129 case Type::X86_FP80TyID
:
130 return LLVMX86_FP80TypeKind
;
131 case Type::FP128TyID
:
132 return LLVMFP128TypeKind
;
133 case Type::PPC_FP128TyID
:
134 return LLVMPPC_FP128TypeKind
;
135 case Type::LabelTyID
:
136 return LLVMLabelTypeKind
;
137 case Type::MetadataTyID
:
138 return LLVMMetadataTypeKind
;
139 case Type::IntegerTyID
:
140 return LLVMIntegerTypeKind
;
141 case Type::FunctionTyID
:
142 return LLVMFunctionTypeKind
;
143 case Type::StructTyID
:
144 return LLVMStructTypeKind
;
145 case Type::ArrayTyID
:
146 return LLVMArrayTypeKind
;
147 case Type::PointerTyID
:
148 return LLVMPointerTypeKind
;
149 case Type::OpaqueTyID
:
150 return LLVMOpaqueTypeKind
;
151 case Type::VectorTyID
:
152 return LLVMVectorTypeKind
;
156 /*--.. Operations on integer types .........................................--*/
158 LLVMTypeRef
LLVMInt1Type(void) { return (LLVMTypeRef
) Type::Int1Ty
; }
159 LLVMTypeRef
LLVMInt8Type(void) { return (LLVMTypeRef
) Type::Int8Ty
; }
160 LLVMTypeRef
LLVMInt16Type(void) { return (LLVMTypeRef
) Type::Int16Ty
; }
161 LLVMTypeRef
LLVMInt32Type(void) { return (LLVMTypeRef
) Type::Int32Ty
; }
162 LLVMTypeRef
LLVMInt64Type(void) { return (LLVMTypeRef
) Type::Int64Ty
; }
164 LLVMTypeRef
LLVMIntType(unsigned NumBits
) {
165 return wrap(IntegerType::get(NumBits
));
168 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy
) {
169 return unwrap
<IntegerType
>(IntegerTy
)->getBitWidth();
172 /*--.. Operations on real types ............................................--*/
174 LLVMTypeRef
LLVMFloatType(void) { return (LLVMTypeRef
) Type::FloatTy
; }
175 LLVMTypeRef
LLVMDoubleType(void) { return (LLVMTypeRef
) Type::DoubleTy
; }
176 LLVMTypeRef
LLVMX86FP80Type(void) { return (LLVMTypeRef
) Type::X86_FP80Ty
; }
177 LLVMTypeRef
LLVMFP128Type(void) { return (LLVMTypeRef
) Type::FP128Ty
; }
178 LLVMTypeRef
LLVMPPCFP128Type(void) { return (LLVMTypeRef
) Type::PPC_FP128Ty
; }
180 /*--.. Operations on function types ........................................--*/
182 LLVMTypeRef
LLVMFunctionType(LLVMTypeRef ReturnType
,
183 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
,
185 std::vector
<const Type
*> Tys
;
186 for (LLVMTypeRef
*I
= ParamTypes
, *E
= ParamTypes
+ ParamCount
; I
!= E
; ++I
)
187 Tys
.push_back(unwrap(*I
));
189 return wrap(FunctionType::get(unwrap(ReturnType
), Tys
, IsVarArg
!= 0));
192 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy
) {
193 return unwrap
<FunctionType
>(FunctionTy
)->isVarArg();
196 LLVMTypeRef
LLVMGetReturnType(LLVMTypeRef FunctionTy
) {
197 return wrap(unwrap
<FunctionType
>(FunctionTy
)->getReturnType());
200 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy
) {
201 return unwrap
<FunctionType
>(FunctionTy
)->getNumParams();
204 void LLVMGetParamTypes(LLVMTypeRef FunctionTy
, LLVMTypeRef
*Dest
) {
205 FunctionType
*Ty
= unwrap
<FunctionType
>(FunctionTy
);
206 for (FunctionType::param_iterator I
= Ty
->param_begin(),
207 E
= Ty
->param_end(); I
!= E
; ++I
)
211 /*--.. Operations on struct types ..........................................--*/
213 LLVMTypeRef
LLVMStructType(LLVMTypeRef
*ElementTypes
,
214 unsigned ElementCount
, int Packed
) {
215 std::vector
<const Type
*> Tys
;
216 for (LLVMTypeRef
*I
= ElementTypes
,
217 *E
= ElementTypes
+ ElementCount
; I
!= E
; ++I
)
218 Tys
.push_back(unwrap(*I
));
220 return wrap(StructType::get(Tys
, Packed
!= 0));
223 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy
) {
224 return unwrap
<StructType
>(StructTy
)->getNumElements();
227 void LLVMGetStructElementTypes(LLVMTypeRef StructTy
, LLVMTypeRef
*Dest
) {
228 StructType
*Ty
= unwrap
<StructType
>(StructTy
);
229 for (FunctionType::param_iterator I
= Ty
->element_begin(),
230 E
= Ty
->element_end(); I
!= E
; ++I
)
234 int LLVMIsPackedStruct(LLVMTypeRef StructTy
) {
235 return unwrap
<StructType
>(StructTy
)->isPacked();
238 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
240 LLVMTypeRef
LLVMArrayType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
241 return wrap(ArrayType::get(unwrap(ElementType
), ElementCount
));
244 LLVMTypeRef
LLVMPointerType(LLVMTypeRef ElementType
, unsigned AddressSpace
) {
245 return wrap(PointerType::get(unwrap(ElementType
), AddressSpace
));
248 LLVMTypeRef
LLVMVectorType(LLVMTypeRef ElementType
, unsigned ElementCount
) {
249 return wrap(VectorType::get(unwrap(ElementType
), ElementCount
));
252 LLVMTypeRef
LLVMGetElementType(LLVMTypeRef Ty
) {
253 return wrap(unwrap
<SequentialType
>(Ty
)->getElementType());
256 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy
) {
257 return unwrap
<ArrayType
>(ArrayTy
)->getNumElements();
260 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy
) {
261 return unwrap
<PointerType
>(PointerTy
)->getAddressSpace();
264 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy
) {
265 return unwrap
<VectorType
>(VectorTy
)->getNumElements();
268 /*--.. Operations on other types ...........................................--*/
270 LLVMTypeRef
LLVMVoidType(void) { return (LLVMTypeRef
) Type::VoidTy
; }
271 LLVMTypeRef
LLVMLabelType(void) { return (LLVMTypeRef
) Type::LabelTy
; }
273 LLVMTypeRef
LLVMOpaqueType(void) {
274 return wrap(OpaqueType::get());
277 /*--.. Operations on type handles ..........................................--*/
279 LLVMTypeHandleRef
LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy
) {
280 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy
)));
283 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle
) {
284 delete unwrap(TypeHandle
);
287 LLVMTypeRef
LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle
) {
288 return wrap(unwrap(TypeHandle
)->get());
291 void LLVMRefineType(LLVMTypeRef AbstractTy
, LLVMTypeRef ConcreteTy
) {
292 unwrap
<DerivedType
>(AbstractTy
)->refineAbstractTypeTo(unwrap(ConcreteTy
));
296 /*===-- Operations on values ----------------------------------------------===*/
298 /*--.. Operations on all values ............................................--*/
300 LLVMTypeRef
LLVMTypeOf(LLVMValueRef Val
) {
301 return wrap(unwrap(Val
)->getType());
304 const char *LLVMGetValueName(LLVMValueRef Val
) {
305 return unwrap(Val
)->getName().data();
308 void LLVMSetValueName(LLVMValueRef Val
, const char *Name
) {
309 unwrap(Val
)->setName(Name
);
312 void LLVMDumpValue(LLVMValueRef Val
) {
317 /*--.. Conversion functions ................................................--*/
319 #define LLVM_DEFINE_VALUE_CAST(name) \
320 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
321 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
324 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST
)
327 /*--.. Operations on constants of any type .................................--*/
329 LLVMValueRef
LLVMConstNull(LLVMTypeRef Ty
) {
330 return wrap(Constant::getNullValue(unwrap(Ty
)));
333 LLVMValueRef
LLVMConstAllOnes(LLVMTypeRef Ty
) {
334 return wrap(Constant::getAllOnesValue(unwrap(Ty
)));
337 LLVMValueRef
LLVMGetUndef(LLVMTypeRef Ty
) {
338 return wrap(UndefValue::get(unwrap(Ty
)));
341 int LLVMIsConstant(LLVMValueRef Ty
) {
342 return isa
<Constant
>(unwrap(Ty
));
345 int LLVMIsNull(LLVMValueRef Val
) {
346 if (Constant
*C
= dyn_cast
<Constant
>(unwrap(Val
)))
347 return C
->isNullValue();
351 int LLVMIsUndef(LLVMValueRef Val
) {
352 return isa
<UndefValue
>(unwrap(Val
));
355 LLVMValueRef
LLVMConstPointerNull(LLVMTypeRef Ty
) {
357 wrap(ConstantPointerNull::get(unwrap
<PointerType
>(Ty
)));
360 /*--.. Operations on scalar constants ......................................--*/
362 LLVMValueRef
LLVMConstInt(LLVMTypeRef IntTy
, unsigned long long N
,
364 return wrap(ConstantInt::get(unwrap
<IntegerType
>(IntTy
), N
, SignExtend
!= 0));
367 static const fltSemantics
&SemanticsForType(Type
*Ty
) {
368 assert(Ty
->isFloatingPoint() && "Type is not floating point!");
369 if (Ty
== Type::FloatTy
)
370 return APFloat::IEEEsingle
;
371 if (Ty
== Type::DoubleTy
)
372 return APFloat::IEEEdouble
;
373 if (Ty
== Type::X86_FP80Ty
)
374 return APFloat::x87DoubleExtended
;
375 if (Ty
== Type::FP128Ty
)
376 return APFloat::IEEEquad
;
377 if (Ty
== Type::PPC_FP128Ty
)
378 return APFloat::PPCDoubleDouble
;
379 return APFloat::Bogus
;
382 LLVMValueRef
LLVMConstReal(LLVMTypeRef RealTy
, double N
) {
385 APN
.convert(SemanticsForType(unwrap(RealTy
)), APFloat::rmNearestTiesToEven
,
387 return wrap(ConstantFP::get(getGlobalContext(), APN
));
390 LLVMValueRef
LLVMConstRealOfString(LLVMTypeRef RealTy
, const char *Text
) {
391 return wrap(ConstantFP::get(getGlobalContext(),
392 APFloat(SemanticsForType(unwrap(RealTy
)), Text
)));
395 /*--.. Operations on composite constants ...................................--*/
397 LLVMValueRef
LLVMConstString(const char *Str
, unsigned Length
,
398 int DontNullTerminate
) {
399 /* Inverted the sense of AddNull because ', 0)' is a
400 better mnemonic for null termination than ', 1)'. */
401 return wrap(ConstantArray::get(std::string(Str
, Length
),
402 DontNullTerminate
== 0));
405 LLVMValueRef
LLVMConstArray(LLVMTypeRef ElementTy
,
406 LLVMValueRef
*ConstantVals
, unsigned Length
) {
407 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy
), Length
),
408 unwrap
<Constant
>(ConstantVals
, Length
),
412 LLVMValueRef
LLVMConstStruct(LLVMValueRef
*ConstantVals
, unsigned Count
,
414 return wrap(ConstantStruct::get(unwrap
<Constant
>(ConstantVals
, Count
),
415 Count
, Packed
!= 0));
418 LLVMValueRef
LLVMConstVector(LLVMValueRef
*ScalarConstantVals
, unsigned Size
) {
419 return wrap(ConstantVector::get(
420 unwrap
<Constant
>(ScalarConstantVals
, Size
), Size
));
423 /*--.. Constant expressions ................................................--*/
425 LLVMValueRef
LLVMAlignOf(LLVMTypeRef Ty
) {
426 return wrap(ConstantExpr::getAlignOf(unwrap(Ty
)));
429 LLVMValueRef
LLVMSizeOf(LLVMTypeRef Ty
) {
430 return wrap(ConstantExpr::getSizeOf(unwrap(Ty
)));
433 LLVMValueRef
LLVMConstNeg(LLVMValueRef ConstantVal
) {
434 return wrap(ConstantExpr::getNeg(
435 unwrap
<Constant
>(ConstantVal
)));
438 LLVMValueRef
LLVMConstNot(LLVMValueRef ConstantVal
) {
439 return wrap(ConstantExpr::getNot(
440 unwrap
<Constant
>(ConstantVal
)));
443 LLVMValueRef
LLVMConstAdd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
444 return wrap(ConstantExpr::getAdd(
445 unwrap
<Constant
>(LHSConstant
),
446 unwrap
<Constant
>(RHSConstant
)));
449 LLVMValueRef
LLVMConstSub(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
450 return wrap(ConstantExpr::getSub(
451 unwrap
<Constant
>(LHSConstant
),
452 unwrap
<Constant
>(RHSConstant
)));
455 LLVMValueRef
LLVMConstMul(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
456 return wrap(ConstantExpr::getMul(
457 unwrap
<Constant
>(LHSConstant
),
458 unwrap
<Constant
>(RHSConstant
)));
461 LLVMValueRef
LLVMConstUDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
462 return wrap(ConstantExpr::getUDiv(
463 unwrap
<Constant
>(LHSConstant
),
464 unwrap
<Constant
>(RHSConstant
)));
467 LLVMValueRef
LLVMConstSDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
468 return wrap(ConstantExpr::getSDiv(
469 unwrap
<Constant
>(LHSConstant
),
470 unwrap
<Constant
>(RHSConstant
)));
473 LLVMValueRef
LLVMConstFDiv(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
474 return wrap(ConstantExpr::getFDiv(
475 unwrap
<Constant
>(LHSConstant
),
476 unwrap
<Constant
>(RHSConstant
)));
479 LLVMValueRef
LLVMConstURem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
480 return wrap(ConstantExpr::getURem(
481 unwrap
<Constant
>(LHSConstant
),
482 unwrap
<Constant
>(RHSConstant
)));
485 LLVMValueRef
LLVMConstSRem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
486 return wrap(ConstantExpr::getSRem(
487 unwrap
<Constant
>(LHSConstant
),
488 unwrap
<Constant
>(RHSConstant
)));
491 LLVMValueRef
LLVMConstFRem(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
492 return wrap(ConstantExpr::getFRem(
493 unwrap
<Constant
>(LHSConstant
),
494 unwrap
<Constant
>(RHSConstant
)));
497 LLVMValueRef
LLVMConstAnd(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
498 return wrap(ConstantExpr::getAnd(
499 unwrap
<Constant
>(LHSConstant
),
500 unwrap
<Constant
>(RHSConstant
)));
503 LLVMValueRef
LLVMConstOr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
504 return wrap(ConstantExpr::getOr(
505 unwrap
<Constant
>(LHSConstant
),
506 unwrap
<Constant
>(RHSConstant
)));
509 LLVMValueRef
LLVMConstXor(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
510 return wrap(ConstantExpr::getXor(
511 unwrap
<Constant
>(LHSConstant
),
512 unwrap
<Constant
>(RHSConstant
)));
515 LLVMValueRef
LLVMConstICmp(LLVMIntPredicate Predicate
,
516 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
517 return wrap(ConstantExpr::getICmp(Predicate
,
518 unwrap
<Constant
>(LHSConstant
),
519 unwrap
<Constant
>(RHSConstant
)));
522 LLVMValueRef
LLVMConstFCmp(LLVMRealPredicate Predicate
,
523 LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
524 return wrap(ConstantExpr::getFCmp(Predicate
,
525 unwrap
<Constant
>(LHSConstant
),
526 unwrap
<Constant
>(RHSConstant
)));
529 LLVMValueRef
LLVMConstShl(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
530 return wrap(ConstantExpr::getShl(
531 unwrap
<Constant
>(LHSConstant
),
532 unwrap
<Constant
>(RHSConstant
)));
535 LLVMValueRef
LLVMConstLShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
536 return wrap(ConstantExpr::getLShr(
537 unwrap
<Constant
>(LHSConstant
),
538 unwrap
<Constant
>(RHSConstant
)));
541 LLVMValueRef
LLVMConstAShr(LLVMValueRef LHSConstant
, LLVMValueRef RHSConstant
) {
542 return wrap(ConstantExpr::getAShr(
543 unwrap
<Constant
>(LHSConstant
),
544 unwrap
<Constant
>(RHSConstant
)));
547 LLVMValueRef
LLVMConstGEP(LLVMValueRef ConstantVal
,
548 LLVMValueRef
*ConstantIndices
, unsigned NumIndices
) {
549 return wrap(ConstantExpr::getGetElementPtr(
550 unwrap
<Constant
>(ConstantVal
),
551 unwrap
<Constant
>(ConstantIndices
,
556 LLVMValueRef
LLVMConstTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
557 return wrap(ConstantExpr::getTrunc(
558 unwrap
<Constant
>(ConstantVal
),
562 LLVMValueRef
LLVMConstSExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
563 return wrap(ConstantExpr::getSExt(
564 unwrap
<Constant
>(ConstantVal
),
568 LLVMValueRef
LLVMConstZExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
569 return wrap(ConstantExpr::getZExt(
570 unwrap
<Constant
>(ConstantVal
),
574 LLVMValueRef
LLVMConstFPTrunc(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
575 return wrap(ConstantExpr::getFPTrunc(
576 unwrap
<Constant
>(ConstantVal
),
580 LLVMValueRef
LLVMConstFPExt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
581 return wrap(ConstantExpr::getFPExtend(
582 unwrap
<Constant
>(ConstantVal
),
586 LLVMValueRef
LLVMConstUIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
587 return wrap(ConstantExpr::getUIToFP(
588 unwrap
<Constant
>(ConstantVal
),
592 LLVMValueRef
LLVMConstSIToFP(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
593 return wrap(ConstantExpr::getSIToFP(unwrap
<Constant
>(ConstantVal
),
597 LLVMValueRef
LLVMConstFPToUI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
598 return wrap(ConstantExpr::getFPToUI(unwrap
<Constant
>(ConstantVal
),
602 LLVMValueRef
LLVMConstFPToSI(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
603 return wrap(ConstantExpr::getFPToSI(
604 unwrap
<Constant
>(ConstantVal
),
608 LLVMValueRef
LLVMConstPtrToInt(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
609 return wrap(ConstantExpr::getPtrToInt(
610 unwrap
<Constant
>(ConstantVal
),
614 LLVMValueRef
LLVMConstIntToPtr(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
615 return wrap(ConstantExpr::getIntToPtr(
616 unwrap
<Constant
>(ConstantVal
),
620 LLVMValueRef
LLVMConstBitCast(LLVMValueRef ConstantVal
, LLVMTypeRef ToType
) {
621 return wrap(ConstantExpr::getBitCast(
622 unwrap
<Constant
>(ConstantVal
),
626 LLVMValueRef
LLVMConstSelect(LLVMValueRef ConstantCondition
,
627 LLVMValueRef ConstantIfTrue
,
628 LLVMValueRef ConstantIfFalse
) {
629 return wrap(ConstantExpr::getSelect(
630 unwrap
<Constant
>(ConstantCondition
),
631 unwrap
<Constant
>(ConstantIfTrue
),
632 unwrap
<Constant
>(ConstantIfFalse
)));
635 LLVMValueRef
LLVMConstExtractElement(LLVMValueRef VectorConstant
,
636 LLVMValueRef IndexConstant
) {
637 return wrap(ConstantExpr::getExtractElement(
638 unwrap
<Constant
>(VectorConstant
),
639 unwrap
<Constant
>(IndexConstant
)));
642 LLVMValueRef
LLVMConstInsertElement(LLVMValueRef VectorConstant
,
643 LLVMValueRef ElementValueConstant
,
644 LLVMValueRef IndexConstant
) {
645 return wrap(ConstantExpr::getInsertElement(
646 unwrap
<Constant
>(VectorConstant
),
647 unwrap
<Constant
>(ElementValueConstant
),
648 unwrap
<Constant
>(IndexConstant
)));
651 LLVMValueRef
LLVMConstShuffleVector(LLVMValueRef VectorAConstant
,
652 LLVMValueRef VectorBConstant
,
653 LLVMValueRef MaskConstant
) {
654 return wrap(ConstantExpr::getShuffleVector(
655 unwrap
<Constant
>(VectorAConstant
),
656 unwrap
<Constant
>(VectorBConstant
),
657 unwrap
<Constant
>(MaskConstant
)));
660 LLVMValueRef
LLVMConstExtractValue(LLVMValueRef AggConstant
, unsigned *IdxList
,
662 return wrap(ConstantExpr::getExtractValue(
663 unwrap
<Constant
>(AggConstant
),
667 LLVMValueRef
LLVMConstInsertValue(LLVMValueRef AggConstant
,
668 LLVMValueRef ElementValueConstant
,
669 unsigned *IdxList
, unsigned NumIdx
) {
670 return wrap(ConstantExpr::getInsertValue(
671 unwrap
<Constant
>(AggConstant
),
672 unwrap
<Constant
>(ElementValueConstant
),
676 LLVMValueRef
LLVMConstInlineAsm(LLVMTypeRef Ty
, const char *AsmString
,
677 const char *Constraints
, int HasSideEffects
) {
678 return wrap(InlineAsm::get(dyn_cast
<FunctionType
>(unwrap(Ty
)), AsmString
,
679 Constraints
, HasSideEffects
));
682 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
684 LLVMModuleRef
LLVMGetGlobalParent(LLVMValueRef Global
) {
685 return wrap(unwrap
<GlobalValue
>(Global
)->getParent());
688 int LLVMIsDeclaration(LLVMValueRef Global
) {
689 return unwrap
<GlobalValue
>(Global
)->isDeclaration();
692 LLVMLinkage
LLVMGetLinkage(LLVMValueRef Global
) {
693 switch (unwrap
<GlobalValue
>(Global
)->getLinkage()) {
695 assert(false && "Unhandled Linkage Type.");
696 case GlobalValue::ExternalLinkage
:
697 return LLVMExternalLinkage
;
698 case GlobalValue::AvailableExternallyLinkage
:
699 return LLVMAvailableExternallyLinkage
;
700 case GlobalValue::LinkOnceAnyLinkage
:
701 return LLVMLinkOnceAnyLinkage
;
702 case GlobalValue::LinkOnceODRLinkage
:
703 return LLVMLinkOnceODRLinkage
;
704 case GlobalValue::WeakAnyLinkage
:
705 return LLVMWeakAnyLinkage
;
706 case GlobalValue::WeakODRLinkage
:
707 return LLVMWeakODRLinkage
;
708 case GlobalValue::AppendingLinkage
:
709 return LLVMAppendingLinkage
;
710 case GlobalValue::InternalLinkage
:
711 return LLVMInternalLinkage
;
712 case GlobalValue::PrivateLinkage
:
713 return LLVMPrivateLinkage
;
714 case GlobalValue::LinkerPrivateLinkage
:
715 return LLVMLinkerPrivateLinkage
;
716 case GlobalValue::DLLImportLinkage
:
717 return LLVMDLLImportLinkage
;
718 case GlobalValue::DLLExportLinkage
:
719 return LLVMDLLExportLinkage
;
720 case GlobalValue::ExternalWeakLinkage
:
721 return LLVMExternalWeakLinkage
;
722 case GlobalValue::GhostLinkage
:
723 return LLVMGhostLinkage
;
724 case GlobalValue::CommonLinkage
:
725 return LLVMCommonLinkage
;
728 // Should never get here.
729 return static_cast<LLVMLinkage
>(0);
732 void LLVMSetLinkage(LLVMValueRef Global
, LLVMLinkage Linkage
) {
733 GlobalValue
*GV
= unwrap
<GlobalValue
>(Global
);
737 assert(false && "Unhandled Linkage Type.");
738 case LLVMExternalLinkage
:
739 GV
->setLinkage(GlobalValue::ExternalLinkage
);
741 case LLVMAvailableExternallyLinkage
:
742 GV
->setLinkage(GlobalValue::AvailableExternallyLinkage
);
744 case LLVMLinkOnceAnyLinkage
:
745 GV
->setLinkage(GlobalValue::LinkOnceAnyLinkage
);
747 case LLVMLinkOnceODRLinkage
:
748 GV
->setLinkage(GlobalValue::LinkOnceODRLinkage
);
750 case LLVMWeakAnyLinkage
:
751 GV
->setLinkage(GlobalValue::WeakAnyLinkage
);
753 case LLVMWeakODRLinkage
:
754 GV
->setLinkage(GlobalValue::WeakODRLinkage
);
756 case LLVMAppendingLinkage
:
757 GV
->setLinkage(GlobalValue::AppendingLinkage
);
759 case LLVMInternalLinkage
:
760 GV
->setLinkage(GlobalValue::InternalLinkage
);
762 case LLVMPrivateLinkage
:
763 GV
->setLinkage(GlobalValue::PrivateLinkage
);
765 case LLVMLinkerPrivateLinkage
:
766 GV
->setLinkage(GlobalValue::LinkerPrivateLinkage
);
768 case LLVMDLLImportLinkage
:
769 GV
->setLinkage(GlobalValue::DLLImportLinkage
);
771 case LLVMDLLExportLinkage
:
772 GV
->setLinkage(GlobalValue::DLLExportLinkage
);
774 case LLVMExternalWeakLinkage
:
775 GV
->setLinkage(GlobalValue::ExternalWeakLinkage
);
777 case LLVMGhostLinkage
:
778 GV
->setLinkage(GlobalValue::GhostLinkage
);
780 case LLVMCommonLinkage
:
781 GV
->setLinkage(GlobalValue::CommonLinkage
);
786 const char *LLVMGetSection(LLVMValueRef Global
) {
787 return unwrap
<GlobalValue
>(Global
)->getSection().c_str();
790 void LLVMSetSection(LLVMValueRef Global
, const char *Section
) {
791 unwrap
<GlobalValue
>(Global
)->setSection(Section
);
794 LLVMVisibility
LLVMGetVisibility(LLVMValueRef Global
) {
795 return static_cast<LLVMVisibility
>(
796 unwrap
<GlobalValue
>(Global
)->getVisibility());
799 void LLVMSetVisibility(LLVMValueRef Global
, LLVMVisibility Viz
) {
800 unwrap
<GlobalValue
>(Global
)
801 ->setVisibility(static_cast<GlobalValue::VisibilityTypes
>(Viz
));
804 unsigned LLVMGetAlignment(LLVMValueRef Global
) {
805 return unwrap
<GlobalValue
>(Global
)->getAlignment();
808 void LLVMSetAlignment(LLVMValueRef Global
, unsigned Bytes
) {
809 unwrap
<GlobalValue
>(Global
)->setAlignment(Bytes
);
812 /*--.. Operations on global variables ......................................--*/
814 LLVMValueRef
LLVMAddGlobal(LLVMModuleRef M
, LLVMTypeRef Ty
, const char *Name
) {
815 return wrap(new GlobalVariable(*unwrap(M
), unwrap(Ty
), false,
816 GlobalValue::ExternalLinkage
, 0, Name
));
819 LLVMValueRef
LLVMGetNamedGlobal(LLVMModuleRef M
, const char *Name
) {
820 return wrap(unwrap(M
)->getNamedGlobal(Name
));
823 LLVMValueRef
LLVMGetFirstGlobal(LLVMModuleRef M
) {
824 Module
*Mod
= unwrap(M
);
825 Module::global_iterator I
= Mod
->global_begin();
826 if (I
== Mod
->global_end())
831 LLVMValueRef
LLVMGetLastGlobal(LLVMModuleRef M
) {
832 Module
*Mod
= unwrap(M
);
833 Module::global_iterator I
= Mod
->global_end();
834 if (I
== Mod
->global_begin())
839 LLVMValueRef
LLVMGetNextGlobal(LLVMValueRef GlobalVar
) {
840 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
841 Module::global_iterator I
= GV
;
842 if (++I
== GV
->getParent()->global_end())
847 LLVMValueRef
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar
) {
848 GlobalVariable
*GV
= unwrap
<GlobalVariable
>(GlobalVar
);
849 Module::global_iterator I
= GV
;
850 if (I
== GV
->getParent()->global_begin())
855 void LLVMDeleteGlobal(LLVMValueRef GlobalVar
) {
856 unwrap
<GlobalVariable
>(GlobalVar
)->eraseFromParent();
859 LLVMValueRef
LLVMGetInitializer(LLVMValueRef GlobalVar
) {
860 return wrap(unwrap
<GlobalVariable
>(GlobalVar
)->getInitializer());
863 void LLVMSetInitializer(LLVMValueRef GlobalVar
, LLVMValueRef ConstantVal
) {
864 unwrap
<GlobalVariable
>(GlobalVar
)
865 ->setInitializer(unwrap
<Constant
>(ConstantVal
));
868 int LLVMIsThreadLocal(LLVMValueRef GlobalVar
) {
869 return unwrap
<GlobalVariable
>(GlobalVar
)->isThreadLocal();
872 void LLVMSetThreadLocal(LLVMValueRef GlobalVar
, int IsThreadLocal
) {
873 unwrap
<GlobalVariable
>(GlobalVar
)->setThreadLocal(IsThreadLocal
!= 0);
876 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar
) {
877 return unwrap
<GlobalVariable
>(GlobalVar
)->isConstant();
880 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar
, int IsConstant
) {
881 unwrap
<GlobalVariable
>(GlobalVar
)->setConstant(IsConstant
!= 0);
884 /*--.. Operations on aliases ......................................--*/
886 LLVMValueRef
LLVMAddAlias(LLVMModuleRef M
, LLVMTypeRef Ty
, LLVMValueRef Aliasee
,
888 return wrap(new GlobalAlias(unwrap(Ty
), GlobalValue::ExternalLinkage
, Name
,
889 unwrap
<Constant
>(Aliasee
), unwrap (M
)));
892 /*--.. Operations on functions .............................................--*/
894 LLVMValueRef
LLVMAddFunction(LLVMModuleRef M
, const char *Name
,
895 LLVMTypeRef FunctionTy
) {
896 return wrap(Function::Create(unwrap
<FunctionType
>(FunctionTy
),
897 GlobalValue::ExternalLinkage
, Name
, unwrap(M
)));
900 LLVMValueRef
LLVMGetNamedFunction(LLVMModuleRef M
, const char *Name
) {
901 return wrap(unwrap(M
)->getFunction(Name
));
904 LLVMValueRef
LLVMGetFirstFunction(LLVMModuleRef M
) {
905 Module
*Mod
= unwrap(M
);
906 Module::iterator I
= Mod
->begin();
912 LLVMValueRef
LLVMGetLastFunction(LLVMModuleRef M
) {
913 Module
*Mod
= unwrap(M
);
914 Module::iterator I
= Mod
->end();
915 if (I
== Mod
->begin())
920 LLVMValueRef
LLVMGetNextFunction(LLVMValueRef Fn
) {
921 Function
*Func
= unwrap
<Function
>(Fn
);
922 Module::iterator I
= Func
;
923 if (++I
== Func
->getParent()->end())
928 LLVMValueRef
LLVMGetPreviousFunction(LLVMValueRef Fn
) {
929 Function
*Func
= unwrap
<Function
>(Fn
);
930 Module::iterator I
= Func
;
931 if (I
== Func
->getParent()->begin())
936 void LLVMDeleteFunction(LLVMValueRef Fn
) {
937 unwrap
<Function
>(Fn
)->eraseFromParent();
940 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn
) {
941 if (Function
*F
= dyn_cast
<Function
>(unwrap(Fn
)))
942 return F
->getIntrinsicID();
946 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn
) {
947 return unwrap
<Function
>(Fn
)->getCallingConv();
950 void LLVMSetFunctionCallConv(LLVMValueRef Fn
, unsigned CC
) {
951 return unwrap
<Function
>(Fn
)->setCallingConv(CC
);
954 const char *LLVMGetGC(LLVMValueRef Fn
) {
955 Function
*F
= unwrap
<Function
>(Fn
);
956 return F
->hasGC()? F
->getGC() : 0;
959 void LLVMSetGC(LLVMValueRef Fn
, const char *GC
) {
960 Function
*F
= unwrap
<Function
>(Fn
);
967 void LLVMAddFunctionAttr(LLVMValueRef Fn
, LLVMAttribute PA
) {
968 Function
*Func
= unwrap
<Function
>(Fn
);
969 const AttrListPtr PAL
= Func
->getAttributes();
970 const AttrListPtr PALnew
= PAL
.addAttr(0, PA
);
971 Func
->setAttributes(PALnew
);
974 void LLVMRemoveFunctionAttr(LLVMValueRef Fn
, LLVMAttribute PA
) {
975 Function
*Func
= unwrap
<Function
>(Fn
);
976 const AttrListPtr PAL
= Func
->getAttributes();
977 const AttrListPtr PALnew
= PAL
.removeAttr(0, PA
);
978 Func
->setAttributes(PALnew
);
981 /*--.. Operations on parameters ............................................--*/
983 unsigned LLVMCountParams(LLVMValueRef FnRef
) {
984 // This function is strictly redundant to
985 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
986 return unwrap
<Function
>(FnRef
)->arg_size();
989 void LLVMGetParams(LLVMValueRef FnRef
, LLVMValueRef
*ParamRefs
) {
990 Function
*Fn
= unwrap
<Function
>(FnRef
);
991 for (Function::arg_iterator I
= Fn
->arg_begin(),
992 E
= Fn
->arg_end(); I
!= E
; I
++)
993 *ParamRefs
++ = wrap(I
);
996 LLVMValueRef
LLVMGetParam(LLVMValueRef FnRef
, unsigned index
) {
997 Function::arg_iterator AI
= unwrap
<Function
>(FnRef
)->arg_begin();
1003 LLVMValueRef
LLVMGetParamParent(LLVMValueRef V
) {
1004 return wrap(unwrap
<Argument
>(V
)->getParent());
1007 LLVMValueRef
LLVMGetFirstParam(LLVMValueRef Fn
) {
1008 Function
*Func
= unwrap
<Function
>(Fn
);
1009 Function::arg_iterator I
= Func
->arg_begin();
1010 if (I
== Func
->arg_end())
1015 LLVMValueRef
LLVMGetLastParam(LLVMValueRef Fn
) {
1016 Function
*Func
= unwrap
<Function
>(Fn
);
1017 Function::arg_iterator I
= Func
->arg_end();
1018 if (I
== Func
->arg_begin())
1023 LLVMValueRef
LLVMGetNextParam(LLVMValueRef Arg
) {
1024 Argument
*A
= unwrap
<Argument
>(Arg
);
1025 Function::arg_iterator I
= A
;
1026 if (++I
== A
->getParent()->arg_end())
1031 LLVMValueRef
LLVMGetPreviousParam(LLVMValueRef Arg
) {
1032 Argument
*A
= unwrap
<Argument
>(Arg
);
1033 Function::arg_iterator I
= A
;
1034 if (I
== A
->getParent()->arg_begin())
1039 void LLVMAddAttribute(LLVMValueRef Arg
, LLVMAttribute PA
) {
1040 unwrap
<Argument
>(Arg
)->addAttr(PA
);
1043 void LLVMRemoveAttribute(LLVMValueRef Arg
, LLVMAttribute PA
) {
1044 unwrap
<Argument
>(Arg
)->removeAttr(PA
);
1047 void LLVMSetParamAlignment(LLVMValueRef Arg
, unsigned align
) {
1048 unwrap
<Argument
>(Arg
)->addAttr(
1049 Attribute::constructAlignmentFromInt(align
));
1052 /*--.. Operations on basic blocks ..........................................--*/
1054 LLVMValueRef
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB
) {
1055 return wrap(static_cast<Value
*>(unwrap(BB
)));
1058 int LLVMValueIsBasicBlock(LLVMValueRef Val
) {
1059 return isa
<BasicBlock
>(unwrap(Val
));
1062 LLVMBasicBlockRef
LLVMValueAsBasicBlock(LLVMValueRef Val
) {
1063 return wrap(unwrap
<BasicBlock
>(Val
));
1066 LLVMValueRef
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB
) {
1067 return wrap(unwrap(BB
)->getParent());
1070 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef
) {
1071 return unwrap
<Function
>(FnRef
)->size();
1074 void LLVMGetBasicBlocks(LLVMValueRef FnRef
, LLVMBasicBlockRef
*BasicBlocksRefs
){
1075 Function
*Fn
= unwrap
<Function
>(FnRef
);
1076 for (Function::iterator I
= Fn
->begin(), E
= Fn
->end(); I
!= E
; I
++)
1077 *BasicBlocksRefs
++ = wrap(I
);
1080 LLVMBasicBlockRef
LLVMGetEntryBasicBlock(LLVMValueRef Fn
) {
1081 return wrap(&unwrap
<Function
>(Fn
)->getEntryBlock());
1084 LLVMBasicBlockRef
LLVMGetFirstBasicBlock(LLVMValueRef Fn
) {
1085 Function
*Func
= unwrap
<Function
>(Fn
);
1086 Function::iterator I
= Func
->begin();
1087 if (I
== Func
->end())
1092 LLVMBasicBlockRef
LLVMGetLastBasicBlock(LLVMValueRef Fn
) {
1093 Function
*Func
= unwrap
<Function
>(Fn
);
1094 Function::iterator I
= Func
->end();
1095 if (I
== Func
->begin())
1100 LLVMBasicBlockRef
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB
) {
1101 BasicBlock
*Block
= unwrap(BB
);
1102 Function::iterator I
= Block
;
1103 if (++I
== Block
->getParent()->end())
1108 LLVMBasicBlockRef
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB
) {
1109 BasicBlock
*Block
= unwrap(BB
);
1110 Function::iterator I
= Block
;
1111 if (I
== Block
->getParent()->begin())
1116 LLVMBasicBlockRef
LLVMAppendBasicBlock(LLVMValueRef FnRef
, const char *Name
) {
1117 return wrap(BasicBlock::Create(Name
, unwrap
<Function
>(FnRef
)));
1120 LLVMBasicBlockRef
LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef
,
1122 BasicBlock
*InsertBeforeBB
= unwrap(InsertBeforeBBRef
);
1123 return wrap(BasicBlock::Create(Name
, InsertBeforeBB
->getParent(),
1127 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef
) {
1128 unwrap(BBRef
)->eraseFromParent();
1131 /*--.. Operations on instructions ..........................................--*/
1133 LLVMBasicBlockRef
LLVMGetInstructionParent(LLVMValueRef Inst
) {
1134 return wrap(unwrap
<Instruction
>(Inst
)->getParent());
1137 LLVMValueRef
LLVMGetFirstInstruction(LLVMBasicBlockRef BB
) {
1138 BasicBlock
*Block
= unwrap(BB
);
1139 BasicBlock::iterator I
= Block
->begin();
1140 if (I
== Block
->end())
1145 LLVMValueRef
LLVMGetLastInstruction(LLVMBasicBlockRef BB
) {
1146 BasicBlock
*Block
= unwrap(BB
);
1147 BasicBlock::iterator I
= Block
->end();
1148 if (I
== Block
->begin())
1153 LLVMValueRef
LLVMGetNextInstruction(LLVMValueRef Inst
) {
1154 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
1155 BasicBlock::iterator I
= Instr
;
1156 if (++I
== Instr
->getParent()->end())
1161 LLVMValueRef
LLVMGetPreviousInstruction(LLVMValueRef Inst
) {
1162 Instruction
*Instr
= unwrap
<Instruction
>(Inst
);
1163 BasicBlock::iterator I
= Instr
;
1164 if (I
== Instr
->getParent()->begin())
1169 /*--.. Call and invoke instructions ........................................--*/
1171 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr
) {
1172 Value
*V
= unwrap(Instr
);
1173 if (CallInst
*CI
= dyn_cast
<CallInst
>(V
))
1174 return CI
->getCallingConv();
1175 else if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(V
))
1176 return II
->getCallingConv();
1177 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1181 void LLVMSetInstructionCallConv(LLVMValueRef Instr
, unsigned CC
) {
1182 Value
*V
= unwrap(Instr
);
1183 if (CallInst
*CI
= dyn_cast
<CallInst
>(V
))
1184 return CI
->setCallingConv(CC
);
1185 else if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(V
))
1186 return II
->setCallingConv(CC
);
1187 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1190 void LLVMAddInstrAttribute(LLVMValueRef Instr
, unsigned index
,
1192 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
1194 Call
.getAttributes().addAttr(index
, PA
));
1197 void LLVMRemoveInstrAttribute(LLVMValueRef Instr
, unsigned index
,
1199 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
1201 Call
.getAttributes().removeAttr(index
, PA
));
1204 void LLVMSetInstrParamAlignment(LLVMValueRef Instr
, unsigned index
,
1206 CallSite Call
= CallSite(unwrap
<Instruction
>(Instr
));
1208 Call
.getAttributes().addAttr(index
,
1209 Attribute::constructAlignmentFromInt(align
)));
1212 /*--.. Operations on call instructions (only) ..............................--*/
1214 int LLVMIsTailCall(LLVMValueRef Call
) {
1215 return unwrap
<CallInst
>(Call
)->isTailCall();
1218 void LLVMSetTailCall(LLVMValueRef Call
, int isTailCall
) {
1219 unwrap
<CallInst
>(Call
)->setTailCall(isTailCall
);
1222 /*--.. Operations on phi nodes .............................................--*/
1224 void LLVMAddIncoming(LLVMValueRef PhiNode
, LLVMValueRef
*IncomingValues
,
1225 LLVMBasicBlockRef
*IncomingBlocks
, unsigned Count
) {
1226 PHINode
*PhiVal
= unwrap
<PHINode
>(PhiNode
);
1227 for (unsigned I
= 0; I
!= Count
; ++I
)
1228 PhiVal
->addIncoming(unwrap(IncomingValues
[I
]), unwrap(IncomingBlocks
[I
]));
1231 unsigned LLVMCountIncoming(LLVMValueRef PhiNode
) {
1232 return unwrap
<PHINode
>(PhiNode
)->getNumIncomingValues();
1235 LLVMValueRef
LLVMGetIncomingValue(LLVMValueRef PhiNode
, unsigned Index
) {
1236 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingValue(Index
));
1239 LLVMBasicBlockRef
LLVMGetIncomingBlock(LLVMValueRef PhiNode
, unsigned Index
) {
1240 return wrap(unwrap
<PHINode
>(PhiNode
)->getIncomingBlock(Index
));
1244 /*===-- Instruction builders ----------------------------------------------===*/
1246 LLVMBuilderRef
LLVMCreateBuilder(void) {
1247 return wrap(new IRBuilder
<>(getGlobalContext()));
1250 void LLVMPositionBuilder(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
,
1251 LLVMValueRef Instr
) {
1252 BasicBlock
*BB
= unwrap(Block
);
1253 Instruction
*I
= Instr
? unwrap
<Instruction
>(Instr
) : (Instruction
*) BB
->end();
1254 unwrap(Builder
)->SetInsertPoint(BB
, I
);
1257 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
1258 Instruction
*I
= unwrap
<Instruction
>(Instr
);
1259 unwrap(Builder
)->SetInsertPoint(I
->getParent(), I
);
1262 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder
, LLVMBasicBlockRef Block
) {
1263 BasicBlock
*BB
= unwrap(Block
);
1264 unwrap(Builder
)->SetInsertPoint(BB
);
1267 LLVMBasicBlockRef
LLVMGetInsertBlock(LLVMBuilderRef Builder
) {
1268 return wrap(unwrap(Builder
)->GetInsertBlock());
1271 void LLVMClearInsertionPosition(LLVMBuilderRef Builder
) {
1272 unwrap(Builder
)->ClearInsertionPoint ();
1275 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder
, LLVMValueRef Instr
) {
1276 unwrap(Builder
)->Insert(unwrap
<Instruction
>(Instr
));
1279 void LLVMDisposeBuilder(LLVMBuilderRef Builder
) {
1280 delete unwrap(Builder
);
1283 /*--.. Instruction builders ................................................--*/
1285 LLVMValueRef
LLVMBuildRetVoid(LLVMBuilderRef B
) {
1286 return wrap(unwrap(B
)->CreateRetVoid());
1289 LLVMValueRef
LLVMBuildRet(LLVMBuilderRef B
, LLVMValueRef V
) {
1290 return wrap(unwrap(B
)->CreateRet(unwrap(V
)));
1293 LLVMValueRef
LLVMBuildBr(LLVMBuilderRef B
, LLVMBasicBlockRef Dest
) {
1294 return wrap(unwrap(B
)->CreateBr(unwrap(Dest
)));
1297 LLVMValueRef
LLVMBuildCondBr(LLVMBuilderRef B
, LLVMValueRef If
,
1298 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Else
) {
1299 return wrap(unwrap(B
)->CreateCondBr(unwrap(If
), unwrap(Then
), unwrap(Else
)));
1302 LLVMValueRef
LLVMBuildSwitch(LLVMBuilderRef B
, LLVMValueRef V
,
1303 LLVMBasicBlockRef Else
, unsigned NumCases
) {
1304 return wrap(unwrap(B
)->CreateSwitch(unwrap(V
), unwrap(Else
), NumCases
));
1307 LLVMValueRef
LLVMBuildInvoke(LLVMBuilderRef B
, LLVMValueRef Fn
,
1308 LLVMValueRef
*Args
, unsigned NumArgs
,
1309 LLVMBasicBlockRef Then
, LLVMBasicBlockRef Catch
,
1311 return wrap(unwrap(B
)->CreateInvoke(unwrap(Fn
), unwrap(Then
), unwrap(Catch
),
1312 unwrap(Args
), unwrap(Args
) + NumArgs
,
1316 LLVMValueRef
LLVMBuildUnwind(LLVMBuilderRef B
) {
1317 return wrap(unwrap(B
)->CreateUnwind());
1320 LLVMValueRef
LLVMBuildUnreachable(LLVMBuilderRef B
) {
1321 return wrap(unwrap(B
)->CreateUnreachable());
1324 void LLVMAddCase(LLVMValueRef Switch
, LLVMValueRef OnVal
,
1325 LLVMBasicBlockRef Dest
) {
1326 unwrap
<SwitchInst
>(Switch
)->addCase(unwrap
<ConstantInt
>(OnVal
), unwrap(Dest
));
1329 /*--.. Arithmetic ..........................................................--*/
1331 LLVMValueRef
LLVMBuildAdd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1333 return wrap(unwrap(B
)->CreateAdd(unwrap(LHS
), unwrap(RHS
), Name
));
1336 LLVMValueRef
LLVMBuildSub(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1338 return wrap(unwrap(B
)->CreateSub(unwrap(LHS
), unwrap(RHS
), Name
));
1341 LLVMValueRef
LLVMBuildMul(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1343 return wrap(unwrap(B
)->CreateMul(unwrap(LHS
), unwrap(RHS
), Name
));
1346 LLVMValueRef
LLVMBuildUDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1348 return wrap(unwrap(B
)->CreateUDiv(unwrap(LHS
), unwrap(RHS
), Name
));
1351 LLVMValueRef
LLVMBuildSDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1353 return wrap(unwrap(B
)->CreateSDiv(unwrap(LHS
), unwrap(RHS
), Name
));
1356 LLVMValueRef
LLVMBuildFDiv(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1358 return wrap(unwrap(B
)->CreateFDiv(unwrap(LHS
), unwrap(RHS
), Name
));
1361 LLVMValueRef
LLVMBuildURem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1363 return wrap(unwrap(B
)->CreateURem(unwrap(LHS
), unwrap(RHS
), Name
));
1366 LLVMValueRef
LLVMBuildSRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1368 return wrap(unwrap(B
)->CreateSRem(unwrap(LHS
), unwrap(RHS
), Name
));
1371 LLVMValueRef
LLVMBuildFRem(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1373 return wrap(unwrap(B
)->CreateFRem(unwrap(LHS
), unwrap(RHS
), Name
));
1376 LLVMValueRef
LLVMBuildShl(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1378 return wrap(unwrap(B
)->CreateShl(unwrap(LHS
), unwrap(RHS
), Name
));
1381 LLVMValueRef
LLVMBuildLShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1383 return wrap(unwrap(B
)->CreateLShr(unwrap(LHS
), unwrap(RHS
), Name
));
1386 LLVMValueRef
LLVMBuildAShr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1388 return wrap(unwrap(B
)->CreateAShr(unwrap(LHS
), unwrap(RHS
), Name
));
1391 LLVMValueRef
LLVMBuildAnd(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1393 return wrap(unwrap(B
)->CreateAnd(unwrap(LHS
), unwrap(RHS
), Name
));
1396 LLVMValueRef
LLVMBuildOr(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1398 return wrap(unwrap(B
)->CreateOr(unwrap(LHS
), unwrap(RHS
), Name
));
1401 LLVMValueRef
LLVMBuildXor(LLVMBuilderRef B
, LLVMValueRef LHS
, LLVMValueRef RHS
,
1403 return wrap(unwrap(B
)->CreateXor(unwrap(LHS
), unwrap(RHS
), Name
));
1406 LLVMValueRef
LLVMBuildNeg(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
1407 return wrap(unwrap(B
)->CreateNeg(unwrap(V
), Name
));
1410 LLVMValueRef
LLVMBuildNot(LLVMBuilderRef B
, LLVMValueRef V
, const char *Name
) {
1411 return wrap(unwrap(B
)->CreateNot(unwrap(V
), Name
));
1414 /*--.. Memory ..............................................................--*/
1416 LLVMValueRef
LLVMBuildMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1418 return wrap(unwrap(B
)->CreateMalloc(unwrap(Ty
), 0, Name
));
1421 LLVMValueRef
LLVMBuildArrayMalloc(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1422 LLVMValueRef Val
, const char *Name
) {
1423 return wrap(unwrap(B
)->CreateMalloc(unwrap(Ty
), unwrap(Val
), Name
));
1426 LLVMValueRef
LLVMBuildAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1428 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), 0, Name
));
1431 LLVMValueRef
LLVMBuildArrayAlloca(LLVMBuilderRef B
, LLVMTypeRef Ty
,
1432 LLVMValueRef Val
, const char *Name
) {
1433 return wrap(unwrap(B
)->CreateAlloca(unwrap(Ty
), unwrap(Val
), Name
));
1436 LLVMValueRef
LLVMBuildFree(LLVMBuilderRef B
, LLVMValueRef PointerVal
) {
1437 return wrap(unwrap(B
)->CreateFree(unwrap(PointerVal
)));
1441 LLVMValueRef
LLVMBuildLoad(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
1443 return wrap(unwrap(B
)->CreateLoad(unwrap(PointerVal
), Name
));
1446 LLVMValueRef
LLVMBuildStore(LLVMBuilderRef B
, LLVMValueRef Val
,
1447 LLVMValueRef PointerVal
) {
1448 return wrap(unwrap(B
)->CreateStore(unwrap(Val
), unwrap(PointerVal
)));
1451 LLVMValueRef
LLVMBuildGEP(LLVMBuilderRef B
, LLVMValueRef Pointer
,
1452 LLVMValueRef
*Indices
, unsigned NumIndices
,
1454 return wrap(unwrap(B
)->CreateGEP(unwrap(Pointer
), unwrap(Indices
),
1455 unwrap(Indices
) + NumIndices
, Name
));
1458 /*--.. Casts ...............................................................--*/
1460 LLVMValueRef
LLVMBuildTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
1461 LLVMTypeRef DestTy
, const char *Name
) {
1462 return wrap(unwrap(B
)->CreateTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
1465 LLVMValueRef
LLVMBuildZExt(LLVMBuilderRef B
, LLVMValueRef Val
,
1466 LLVMTypeRef DestTy
, const char *Name
) {
1467 return wrap(unwrap(B
)->CreateZExt(unwrap(Val
), unwrap(DestTy
), Name
));
1470 LLVMValueRef
LLVMBuildSExt(LLVMBuilderRef B
, LLVMValueRef Val
,
1471 LLVMTypeRef DestTy
, const char *Name
) {
1472 return wrap(unwrap(B
)->CreateSExt(unwrap(Val
), unwrap(DestTy
), Name
));
1475 LLVMValueRef
LLVMBuildFPToUI(LLVMBuilderRef B
, LLVMValueRef Val
,
1476 LLVMTypeRef DestTy
, const char *Name
) {
1477 return wrap(unwrap(B
)->CreateFPToUI(unwrap(Val
), unwrap(DestTy
), Name
));
1480 LLVMValueRef
LLVMBuildFPToSI(LLVMBuilderRef B
, LLVMValueRef Val
,
1481 LLVMTypeRef DestTy
, const char *Name
) {
1482 return wrap(unwrap(B
)->CreateFPToSI(unwrap(Val
), unwrap(DestTy
), Name
));
1485 LLVMValueRef
LLVMBuildUIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
1486 LLVMTypeRef DestTy
, const char *Name
) {
1487 return wrap(unwrap(B
)->CreateUIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
1490 LLVMValueRef
LLVMBuildSIToFP(LLVMBuilderRef B
, LLVMValueRef Val
,
1491 LLVMTypeRef DestTy
, const char *Name
) {
1492 return wrap(unwrap(B
)->CreateSIToFP(unwrap(Val
), unwrap(DestTy
), Name
));
1495 LLVMValueRef
LLVMBuildFPTrunc(LLVMBuilderRef B
, LLVMValueRef Val
,
1496 LLVMTypeRef DestTy
, const char *Name
) {
1497 return wrap(unwrap(B
)->CreateFPTrunc(unwrap(Val
), unwrap(DestTy
), Name
));
1500 LLVMValueRef
LLVMBuildFPExt(LLVMBuilderRef B
, LLVMValueRef Val
,
1501 LLVMTypeRef DestTy
, const char *Name
) {
1502 return wrap(unwrap(B
)->CreateFPExt(unwrap(Val
), unwrap(DestTy
), Name
));
1505 LLVMValueRef
LLVMBuildPtrToInt(LLVMBuilderRef B
, LLVMValueRef Val
,
1506 LLVMTypeRef DestTy
, const char *Name
) {
1507 return wrap(unwrap(B
)->CreatePtrToInt(unwrap(Val
), unwrap(DestTy
), Name
));
1510 LLVMValueRef
LLVMBuildIntToPtr(LLVMBuilderRef B
, LLVMValueRef Val
,
1511 LLVMTypeRef DestTy
, const char *Name
) {
1512 return wrap(unwrap(B
)->CreateIntToPtr(unwrap(Val
), unwrap(DestTy
), Name
));
1515 LLVMValueRef
LLVMBuildBitCast(LLVMBuilderRef B
, LLVMValueRef Val
,
1516 LLVMTypeRef DestTy
, const char *Name
) {
1517 return wrap(unwrap(B
)->CreateBitCast(unwrap(Val
), unwrap(DestTy
), Name
));
1520 /*--.. Comparisons .........................................................--*/
1522 LLVMValueRef
LLVMBuildICmp(LLVMBuilderRef B
, LLVMIntPredicate Op
,
1523 LLVMValueRef LHS
, LLVMValueRef RHS
,
1525 return wrap(unwrap(B
)->CreateICmp(static_cast<ICmpInst::Predicate
>(Op
),
1526 unwrap(LHS
), unwrap(RHS
), Name
));
1529 LLVMValueRef
LLVMBuildFCmp(LLVMBuilderRef B
, LLVMRealPredicate Op
,
1530 LLVMValueRef LHS
, LLVMValueRef RHS
,
1532 return wrap(unwrap(B
)->CreateFCmp(static_cast<FCmpInst::Predicate
>(Op
),
1533 unwrap(LHS
), unwrap(RHS
), Name
));
1536 /*--.. Miscellaneous instructions ..........................................--*/
1538 LLVMValueRef
LLVMBuildPhi(LLVMBuilderRef B
, LLVMTypeRef Ty
, const char *Name
) {
1539 return wrap(unwrap(B
)->CreatePHI(unwrap(Ty
), Name
));
1542 LLVMValueRef
LLVMBuildCall(LLVMBuilderRef B
, LLVMValueRef Fn
,
1543 LLVMValueRef
*Args
, unsigned NumArgs
,
1545 return wrap(unwrap(B
)->CreateCall(unwrap(Fn
), unwrap(Args
),
1546 unwrap(Args
) + NumArgs
, Name
));
1549 LLVMValueRef
LLVMBuildSelect(LLVMBuilderRef B
, LLVMValueRef If
,
1550 LLVMValueRef Then
, LLVMValueRef Else
,
1552 return wrap(unwrap(B
)->CreateSelect(unwrap(If
), unwrap(Then
), unwrap(Else
),
1556 LLVMValueRef
LLVMBuildVAArg(LLVMBuilderRef B
, LLVMValueRef List
,
1557 LLVMTypeRef Ty
, const char *Name
) {
1558 return wrap(unwrap(B
)->CreateVAArg(unwrap(List
), unwrap(Ty
), Name
));
1561 LLVMValueRef
LLVMBuildExtractElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
1562 LLVMValueRef Index
, const char *Name
) {
1563 return wrap(unwrap(B
)->CreateExtractElement(unwrap(VecVal
), unwrap(Index
),
1567 LLVMValueRef
LLVMBuildInsertElement(LLVMBuilderRef B
, LLVMValueRef VecVal
,
1568 LLVMValueRef EltVal
, LLVMValueRef Index
,
1570 return wrap(unwrap(B
)->CreateInsertElement(unwrap(VecVal
), unwrap(EltVal
),
1571 unwrap(Index
), Name
));
1574 LLVMValueRef
LLVMBuildShuffleVector(LLVMBuilderRef B
, LLVMValueRef V1
,
1575 LLVMValueRef V2
, LLVMValueRef Mask
,
1577 return wrap(unwrap(B
)->CreateShuffleVector(unwrap(V1
), unwrap(V2
),
1578 unwrap(Mask
), Name
));
1581 LLVMValueRef
LLVMBuildExtractValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
1582 unsigned Index
, const char *Name
) {
1583 return wrap(unwrap(B
)->CreateExtractValue(unwrap(AggVal
), Index
, Name
));
1586 LLVMValueRef
LLVMBuildInsertValue(LLVMBuilderRef B
, LLVMValueRef AggVal
,
1587 LLVMValueRef EltVal
, unsigned Index
,
1589 return wrap(unwrap(B
)->CreateInsertValue(unwrap(AggVal
), unwrap(EltVal
),
1594 /*===-- Module providers --------------------------------------------------===*/
1596 LLVMModuleProviderRef
1597 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M
) {
1598 return wrap(new ExistingModuleProvider(unwrap(M
)));
1601 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP
) {
1606 /*===-- Memory buffers ----------------------------------------------------===*/
1608 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path
,
1609 LLVMMemoryBufferRef
*OutMemBuf
,
1610 char **OutMessage
) {
1612 if (MemoryBuffer
*MB
= MemoryBuffer::getFile(Path
, &Error
)) {
1613 *OutMemBuf
= wrap(MB
);
1617 *OutMessage
= strdup(Error
.c_str());
1621 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef
*OutMemBuf
,
1622 char **OutMessage
) {
1623 if (MemoryBuffer
*MB
= MemoryBuffer::getSTDIN()) {
1624 *OutMemBuf
= wrap(MB
);
1628 *OutMessage
= strdup("stdin is empty.");
1632 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf
) {
1633 delete unwrap(MemBuf
);