1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
35 /*===-- Error handling ----------------------------------------------------===*/
37 void LLVMDisposeMessage(char *Message) {
42 /*===-- Operations on contexts --------------------------------------------===*/
44 LLVMContextRef LLVMContextCreate() {
45 return wrap(new LLVMContext());
48 void LLVMContextDispose(LLVMContextRef C) {
53 /*===-- Operations on modules ---------------------------------------------===*/
55 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID, LLVMContextRef C) {
56 return wrap(new Module(ModuleID, unwrap(C)));
59 void LLVMDisposeModule(LLVMModuleRef M) {
63 /*--.. Data layout .........................................................--*/
64 const char * LLVMGetDataLayout(LLVMModuleRef M) {
65 return unwrap(M)->getDataLayout().c_str();
68 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
69 unwrap(M)->setDataLayout(Triple);
72 /*--.. Target triple .......................................................--*/
73 const char * LLVMGetTarget(LLVMModuleRef M) {
74 return unwrap(M)->getTargetTriple().c_str();
77 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
78 unwrap(M)->setTargetTriple(Triple);
81 /*--.. Type names ..........................................................--*/
82 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
83 return unwrap(M)->addTypeName(Name, unwrap(Ty));
86 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
89 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
90 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
95 void LLVMDumpModule(LLVMModuleRef M) {
100 /*===-- Operations on types -----------------------------------------------===*/
102 /*--.. Operations on all types (mostly) ....................................--*/
104 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
105 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
108 /*--.. Operations on integer types .........................................--*/
110 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
111 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
112 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
113 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
114 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
116 LLVMTypeRef LLVMIntType(unsigned NumBits) {
117 return wrap(IntegerType::get(NumBits));
120 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
121 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
124 /*--.. Operations on real types ............................................--*/
126 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
127 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
128 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
129 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
130 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
132 /*--.. Operations on function types ........................................--*/
134 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
135 LLVMTypeRef *ParamTypes, unsigned ParamCount,
137 std::vector<const Type*> Tys;
138 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
139 Tys.push_back(unwrap(*I));
141 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
144 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
145 return unwrap<FunctionType>(FunctionTy)->isVarArg();
148 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
149 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
152 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
153 return unwrap<FunctionType>(FunctionTy)->getNumParams();
156 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
157 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
158 for (FunctionType::param_iterator I = Ty->param_begin(),
159 E = Ty->param_end(); I != E; ++I)
163 /*--.. Operations on struct types ..........................................--*/
165 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
166 unsigned ElementCount, int Packed) {
167 std::vector<const Type*> Tys;
168 for (LLVMTypeRef *I = ElementTypes,
169 *E = ElementTypes + ElementCount; I != E; ++I)
170 Tys.push_back(unwrap(*I));
172 return wrap(StructType::get(Tys, Packed != 0));
175 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
176 return unwrap<StructType>(StructTy)->getNumElements();
179 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
180 StructType *Ty = unwrap<StructType>(StructTy);
181 for (FunctionType::param_iterator I = Ty->element_begin(),
182 E = Ty->element_end(); I != E; ++I)
186 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
187 return unwrap<StructType>(StructTy)->isPacked();
190 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
192 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
193 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
196 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
197 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
200 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
201 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
204 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
205 return wrap(unwrap<SequentialType>(Ty)->getElementType());
208 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
209 return unwrap<ArrayType>(ArrayTy)->getNumElements();
212 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
213 return unwrap<PointerType>(PointerTy)->getAddressSpace();
216 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
217 return unwrap<VectorType>(VectorTy)->getNumElements();
220 /*--.. Operations on other types ...........................................--*/
222 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
223 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
225 LLVMTypeRef LLVMOpaqueType(void) {
226 return wrap(llvm::OpaqueType::get());
229 /*--.. Operations on type handles ..........................................--*/
231 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
232 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
235 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
236 delete unwrap(TypeHandle);
239 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
240 return wrap(unwrap(TypeHandle)->get());
243 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
244 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
248 /*===-- Operations on values ----------------------------------------------===*/
250 /*--.. Operations on all values ............................................--*/
252 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
253 return wrap(unwrap(Val)->getType());
256 const char *LLVMGetValueName(LLVMValueRef Val) {
257 return unwrap(Val)->getNameStart();
260 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
261 unwrap(Val)->setName(Name);
264 void LLVMDumpValue(LLVMValueRef Val) {
269 /*--.. Conversion functions ................................................--*/
271 #define LLVM_DEFINE_VALUE_CAST(name) \
272 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
273 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
276 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
279 /*--.. Operations on constants of any type .................................--*/
281 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
282 return wrap(Constant::getNullValue(unwrap(Ty)));
285 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
286 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
289 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
290 return wrap(UndefValue::get(unwrap(Ty)));
293 int LLVMIsConstant(LLVMValueRef Ty) {
294 return isa<Constant>(unwrap(Ty));
297 int LLVMIsNull(LLVMValueRef Val) {
298 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
299 return C->isNullValue();
303 int LLVMIsUndef(LLVMValueRef Val) {
304 return isa<UndefValue>(unwrap(Val));
307 /*--.. Operations on scalar constants ......................................--*/
309 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
311 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
314 static const fltSemantics &SemanticsForType(Type *Ty) {
315 assert(Ty->isFloatingPoint() && "Type is not floating point!");
316 if (Ty == Type::FloatTy)
317 return APFloat::IEEEsingle;
318 if (Ty == Type::DoubleTy)
319 return APFloat::IEEEdouble;
320 if (Ty == Type::X86_FP80Ty)
321 return APFloat::x87DoubleExtended;
322 if (Ty == Type::FP128Ty)
323 return APFloat::IEEEquad;
324 if (Ty == Type::PPC_FP128Ty)
325 return APFloat::PPCDoubleDouble;
326 return APFloat::Bogus;
329 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
332 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
334 return wrap(ConstantFP::get(APN));
337 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
338 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
341 /*--.. Operations on composite constants ...................................--*/
343 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
344 int DontNullTerminate) {
345 /* Inverted the sense of AddNull because ', 0)' is a
346 better mnemonic for null termination than ', 1)'. */
347 return wrap(ConstantArray::get(std::string(Str, Length),
348 DontNullTerminate == 0));
351 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
352 LLVMValueRef *ConstantVals, unsigned Length) {
353 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
354 unwrap<Constant>(ConstantVals, Length),
358 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
360 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
361 Count, Packed != 0));
364 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
365 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
369 /*--.. Constant expressions ................................................--*/
371 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
372 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
375 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
376 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
379 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
380 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
383 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
384 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
387 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
388 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
389 unwrap<Constant>(RHSConstant)));
392 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
393 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
394 unwrap<Constant>(RHSConstant)));
397 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
398 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
399 unwrap<Constant>(RHSConstant)));
402 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
403 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
404 unwrap<Constant>(RHSConstant)));
407 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
408 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
409 unwrap<Constant>(RHSConstant)));
412 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
413 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
414 unwrap<Constant>(RHSConstant)));
417 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
418 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
419 unwrap<Constant>(RHSConstant)));
422 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
423 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
424 unwrap<Constant>(RHSConstant)));
427 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
428 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
429 unwrap<Constant>(RHSConstant)));
432 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
433 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
434 unwrap<Constant>(RHSConstant)));
437 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
438 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
439 unwrap<Constant>(RHSConstant)));
442 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
443 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
444 unwrap<Constant>(RHSConstant)));
447 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
448 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
449 return wrap(ConstantExpr::getICmp(Predicate,
450 unwrap<Constant>(LHSConstant),
451 unwrap<Constant>(RHSConstant)));
454 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
455 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
456 return wrap(ConstantExpr::getFCmp(Predicate,
457 unwrap<Constant>(LHSConstant),
458 unwrap<Constant>(RHSConstant)));
461 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
462 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
463 unwrap<Constant>(RHSConstant)));
466 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
467 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
468 unwrap<Constant>(RHSConstant)));
471 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
472 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
473 unwrap<Constant>(RHSConstant)));
476 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
477 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
478 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
479 unwrap<Constant>(ConstantIndices,
484 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
485 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
489 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
490 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
494 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
495 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
499 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
500 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
504 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
505 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
509 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
510 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
514 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
515 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
519 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
520 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
524 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
525 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
529 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
530 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
534 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
535 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
539 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
540 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
544 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
545 LLVMValueRef ConstantIfTrue,
546 LLVMValueRef ConstantIfFalse) {
547 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
548 unwrap<Constant>(ConstantIfTrue),
549 unwrap<Constant>(ConstantIfFalse)));
552 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
553 LLVMValueRef IndexConstant) {
554 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
555 unwrap<Constant>(IndexConstant)));
558 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
559 LLVMValueRef ElementValueConstant,
560 LLVMValueRef IndexConstant) {
561 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
562 unwrap<Constant>(ElementValueConstant),
563 unwrap<Constant>(IndexConstant)));
566 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
567 LLVMValueRef VectorBConstant,
568 LLVMValueRef MaskConstant) {
569 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
570 unwrap<Constant>(VectorBConstant),
571 unwrap<Constant>(MaskConstant)));
574 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
576 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
580 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
581 LLVMValueRef ElementValueConstant,
582 unsigned *IdxList, unsigned NumIdx) {
583 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
584 unwrap<Constant>(ElementValueConstant),
588 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
589 const char *Constraints, int HasSideEffects) {
590 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
591 Constraints, HasSideEffects));
594 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
596 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
597 return wrap(unwrap<GlobalValue>(Global)->getParent());
600 int LLVMIsDeclaration(LLVMValueRef Global) {
601 return unwrap<GlobalValue>(Global)->isDeclaration();
604 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
605 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
608 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
609 unwrap<GlobalValue>(Global)
610 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
613 const char *LLVMGetSection(LLVMValueRef Global) {
614 return unwrap<GlobalValue>(Global)->getSection().c_str();
617 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
618 unwrap<GlobalValue>(Global)->setSection(Section);
621 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
622 return static_cast<LLVMVisibility>(
623 unwrap<GlobalValue>(Global)->getVisibility());
626 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
627 unwrap<GlobalValue>(Global)
628 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
631 unsigned LLVMGetAlignment(LLVMValueRef Global) {
632 return unwrap<GlobalValue>(Global)->getAlignment();
635 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
636 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
639 /*--.. Operations on global variables ......................................--*/
641 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
642 return wrap(new GlobalVariable(unwrap(Ty), false,
643 GlobalValue::ExternalLinkage, 0, Name,
647 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
648 return wrap(unwrap(M)->getNamedGlobal(Name));
651 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
652 Module *Mod = unwrap(M);
653 Module::global_iterator I = Mod->global_begin();
654 if (I == Mod->global_end())
659 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
660 Module *Mod = unwrap(M);
661 Module::global_iterator I = Mod->global_end();
662 if (I == Mod->global_begin())
667 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
668 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
669 Module::global_iterator I = GV;
670 if (++I == GV->getParent()->global_end())
675 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
676 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
677 Module::global_iterator I = GV;
678 if (I == GV->getParent()->global_begin())
683 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
684 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
687 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
688 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
691 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
692 unwrap<GlobalVariable>(GlobalVar)
693 ->setInitializer(unwrap<Constant>(ConstantVal));
696 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
697 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
700 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
701 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
704 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
705 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
708 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
709 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
712 /*--.. Operations on aliases ......................................--*/
714 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
716 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
717 unwrap<Constant>(Aliasee), unwrap (M)));
720 /*--.. Operations on functions .............................................--*/
722 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
723 LLVMTypeRef FunctionTy) {
724 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
725 GlobalValue::ExternalLinkage, Name, unwrap(M)));
728 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
729 return wrap(unwrap(M)->getFunction(Name));
732 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
733 Module *Mod = unwrap(M);
734 Module::iterator I = Mod->begin();
740 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
741 Module *Mod = unwrap(M);
742 Module::iterator I = Mod->end();
743 if (I == Mod->begin())
748 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
749 Function *Func = unwrap<Function>(Fn);
750 Module::iterator I = Func;
751 if (++I == Func->getParent()->end())
756 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
757 Function *Func = unwrap<Function>(Fn);
758 Module::iterator I = Func;
759 if (I == Func->getParent()->begin())
764 void LLVMDeleteFunction(LLVMValueRef Fn) {
765 unwrap<Function>(Fn)->eraseFromParent();
768 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
769 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
770 return F->getIntrinsicID();
774 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
775 return unwrap<Function>(Fn)->getCallingConv();
778 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
779 return unwrap<Function>(Fn)->setCallingConv(CC);
782 const char *LLVMGetGC(LLVMValueRef Fn) {
783 Function *F = unwrap<Function>(Fn);
784 return F->hasGC()? F->getGC() : 0;
787 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
788 Function *F = unwrap<Function>(Fn);
795 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
796 Function *Func = unwrap<Function>(Fn);
797 const AttrListPtr PAL = Func->getAttributes();
798 const AttrListPtr PALnew = PAL.addAttr(0, PA);
799 Func->setAttributes(PALnew);
802 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
803 Function *Func = unwrap<Function>(Fn);
804 const AttrListPtr PAL = Func->getAttributes();
805 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
806 Func->setAttributes(PALnew);
809 /*--.. Operations on parameters ............................................--*/
811 unsigned LLVMCountParams(LLVMValueRef FnRef) {
812 // This function is strictly redundant to
813 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
814 return unwrap<Function>(FnRef)->arg_size();
817 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
818 Function *Fn = unwrap<Function>(FnRef);
819 for (Function::arg_iterator I = Fn->arg_begin(),
820 E = Fn->arg_end(); I != E; I++)
821 *ParamRefs++ = wrap(I);
824 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
825 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
831 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
832 return wrap(unwrap<Argument>(V)->getParent());
835 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
836 Function *Func = unwrap<Function>(Fn);
837 Function::arg_iterator I = Func->arg_begin();
838 if (I == Func->arg_end())
843 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
844 Function *Func = unwrap<Function>(Fn);
845 Function::arg_iterator I = Func->arg_end();
846 if (I == Func->arg_begin())
851 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
852 Argument *A = unwrap<Argument>(Arg);
853 Function::arg_iterator I = A;
854 if (++I == A->getParent()->arg_end())
859 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
860 Argument *A = unwrap<Argument>(Arg);
861 Function::arg_iterator I = A;
862 if (I == A->getParent()->arg_begin())
867 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
868 unwrap<Argument>(Arg)->addAttr(PA);
871 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
872 unwrap<Argument>(Arg)->removeAttr(PA);
875 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
876 unwrap<Argument>(Arg)->addAttr(
877 Attribute::constructAlignmentFromInt(align));
880 /*--.. Operations on basic blocks ..........................................--*/
882 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
883 return wrap(static_cast<Value*>(unwrap(BB)));
886 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
887 return isa<BasicBlock>(unwrap(Val));
890 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
891 return wrap(unwrap<BasicBlock>(Val));
894 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
895 return wrap(unwrap(BB)->getParent());
898 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
899 return unwrap<Function>(FnRef)->size();
902 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
903 Function *Fn = unwrap<Function>(FnRef);
904 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
905 *BasicBlocksRefs++ = wrap(I);
908 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
909 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
912 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
913 Function *Func = unwrap<Function>(Fn);
914 Function::iterator I = Func->begin();
915 if (I == Func->end())
920 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
921 Function *Func = unwrap<Function>(Fn);
922 Function::iterator I = Func->end();
923 if (I == Func->begin())
928 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
929 BasicBlock *Block = unwrap(BB);
930 Function::iterator I = Block;
931 if (++I == Block->getParent()->end())
936 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
937 BasicBlock *Block = unwrap(BB);
938 Function::iterator I = Block;
939 if (I == Block->getParent()->begin())
944 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
945 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
948 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
950 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
951 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
955 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
956 unwrap(BBRef)->eraseFromParent();
959 /*--.. Operations on instructions ..........................................--*/
961 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
962 return wrap(unwrap<Instruction>(Inst)->getParent());
965 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
966 BasicBlock *Block = unwrap(BB);
967 BasicBlock::iterator I = Block->begin();
968 if (I == Block->end())
973 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
974 BasicBlock *Block = unwrap(BB);
975 BasicBlock::iterator I = Block->end();
976 if (I == Block->begin())
981 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
982 Instruction *Instr = unwrap<Instruction>(Inst);
983 BasicBlock::iterator I = Instr;
984 if (++I == Instr->getParent()->end())
989 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
990 Instruction *Instr = unwrap<Instruction>(Inst);
991 BasicBlock::iterator I = Instr;
992 if (I == Instr->getParent()->begin())
997 /*--.. Call and invoke instructions ........................................--*/
999 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1000 Value *V = unwrap(Instr);
1001 if (CallInst *CI = dyn_cast<CallInst>(V))
1002 return CI->getCallingConv();
1003 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1004 return II->getCallingConv();
1005 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
1009 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1010 Value *V = unwrap(Instr);
1011 if (CallInst *CI = dyn_cast<CallInst>(V))
1012 return CI->setCallingConv(CC);
1013 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1014 return II->setCallingConv(CC);
1015 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1018 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1020 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1022 Call.getAttributes().addAttr(index, PA));
1025 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1027 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1029 Call.getAttributes().removeAttr(index, PA));
1032 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1034 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1036 Call.getAttributes().addAttr(index,
1037 Attribute::constructAlignmentFromInt(align)));
1040 /*--.. Operations on call instructions (only) ..............................--*/
1042 int LLVMIsTailCall(LLVMValueRef Call) {
1043 return unwrap<CallInst>(Call)->isTailCall();
1046 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1047 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1050 /*--.. Operations on phi nodes .............................................--*/
1052 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1053 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1054 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1055 for (unsigned I = 0; I != Count; ++I)
1056 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1059 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1060 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1063 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1064 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1067 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1068 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1072 /*===-- Instruction builders ----------------------------------------------===*/
1074 LLVMBuilderRef LLVMCreateBuilder(void) {
1075 return wrap(new IRBuilder<>());
1078 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1079 LLVMValueRef Instr) {
1080 BasicBlock *BB = unwrap(Block);
1081 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1082 unwrap(Builder)->SetInsertPoint(BB, I);
1085 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1086 Instruction *I = unwrap<Instruction>(Instr);
1087 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1090 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1091 BasicBlock *BB = unwrap(Block);
1092 unwrap(Builder)->SetInsertPoint(BB);
1095 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1096 return wrap(unwrap(Builder)->GetInsertBlock());
1099 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1100 unwrap(Builder)->ClearInsertionPoint ();
1103 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1104 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1107 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1108 delete unwrap(Builder);
1111 /*--.. Instruction builders ................................................--*/
1113 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1114 return wrap(unwrap(B)->CreateRetVoid());
1117 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1118 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1121 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1122 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1125 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1126 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1127 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1130 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1131 LLVMBasicBlockRef Else, unsigned NumCases) {
1132 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1135 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1136 LLVMValueRef *Args, unsigned NumArgs,
1137 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1139 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1140 unwrap(Args), unwrap(Args) + NumArgs,
1144 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1145 return wrap(unwrap(B)->CreateUnwind());
1148 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1149 return wrap(unwrap(B)->CreateUnreachable());
1152 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1153 LLVMBasicBlockRef Dest) {
1154 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1157 /*--.. Arithmetic ..........................................................--*/
1159 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1161 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1164 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1166 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1169 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1171 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1174 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1176 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1179 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1181 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1184 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1186 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1189 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1191 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1194 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1196 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1199 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1201 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1204 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1206 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1209 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1211 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1214 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1216 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1219 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1221 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1224 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1226 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1229 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1231 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1234 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1235 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1238 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1239 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1242 /*--.. Memory ..............................................................--*/
1244 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1246 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1249 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1250 LLVMValueRef Val, const char *Name) {
1251 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1254 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1256 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1259 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1260 LLVMValueRef Val, const char *Name) {
1261 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1264 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1265 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1269 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1271 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1274 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1275 LLVMValueRef PointerVal) {
1276 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1279 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1280 LLVMValueRef *Indices, unsigned NumIndices,
1282 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1283 unwrap(Indices) + NumIndices, Name));
1286 /*--.. Casts ...............................................................--*/
1288 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1289 LLVMTypeRef DestTy, const char *Name) {
1290 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1293 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1294 LLVMTypeRef DestTy, const char *Name) {
1295 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1298 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1299 LLVMTypeRef DestTy, const char *Name) {
1300 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1303 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1304 LLVMTypeRef DestTy, const char *Name) {
1305 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1308 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1309 LLVMTypeRef DestTy, const char *Name) {
1310 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1313 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1314 LLVMTypeRef DestTy, const char *Name) {
1315 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1318 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1319 LLVMTypeRef DestTy, const char *Name) {
1320 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1323 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1324 LLVMTypeRef DestTy, const char *Name) {
1325 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1328 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1329 LLVMTypeRef DestTy, const char *Name) {
1330 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1333 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1334 LLVMTypeRef DestTy, const char *Name) {
1335 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1338 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1339 LLVMTypeRef DestTy, const char *Name) {
1340 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1343 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1344 LLVMTypeRef DestTy, const char *Name) {
1345 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1348 /*--.. Comparisons .........................................................--*/
1350 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1351 LLVMValueRef LHS, LLVMValueRef RHS,
1353 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1354 unwrap(LHS), unwrap(RHS), Name));
1357 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1358 LLVMValueRef LHS, LLVMValueRef RHS,
1360 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1361 unwrap(LHS), unwrap(RHS), Name));
1364 /*--.. Miscellaneous instructions ..........................................--*/
1366 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1367 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1370 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1371 LLVMValueRef *Args, unsigned NumArgs,
1373 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1374 unwrap(Args) + NumArgs, Name));
1377 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1378 LLVMValueRef Then, LLVMValueRef Else,
1380 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1384 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1385 LLVMTypeRef Ty, const char *Name) {
1386 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1389 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1390 LLVMValueRef Index, const char *Name) {
1391 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1395 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1396 LLVMValueRef EltVal, LLVMValueRef Index,
1398 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1399 unwrap(Index), Name));
1402 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1403 LLVMValueRef V2, LLVMValueRef Mask,
1405 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1406 unwrap(Mask), Name));
1409 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1410 unsigned Index, const char *Name) {
1411 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1414 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1415 LLVMValueRef EltVal, unsigned Index,
1417 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1422 /*===-- Module providers --------------------------------------------------===*/
1424 LLVMModuleProviderRef
1425 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1426 return wrap(new ExistingModuleProvider(unwrap(M)));
1429 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1434 /*===-- Memory buffers ----------------------------------------------------===*/
1436 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1437 LLVMMemoryBufferRef *OutMemBuf,
1438 char **OutMessage) {
1440 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1441 *OutMemBuf = wrap(MB);
1445 *OutMessage = strdup(Error.c_str());
1449 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1450 char **OutMessage) {
1451 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1452 *OutMemBuf = wrap(MB);
1456 *OutMessage = strdup("stdin is empty.");
1460 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1461 delete unwrap(MemBuf);