1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
35 /*===-- Error handling ----------------------------------------------------===*/
37 void LLVMDisposeMessage(char *Message) {
42 /*===-- Operations on contexts --------------------------------------------===*/
44 LLVMContextRef LLVMContextCreate() {
45 return wrap(new LLVMContext());
48 LLVMContextRef LLVMGetGlobalContext() {
49 return wrap(&getGlobalContext());
52 void LLVMContextDispose(LLVMContextRef C) {
57 /*===-- Operations on modules ---------------------------------------------===*/
59 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
60 return wrap(new Module(ModuleID, getGlobalContext()));
63 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
65 return wrap(new Module(ModuleID, *unwrap(C)));
68 void LLVMDisposeModule(LLVMModuleRef M) {
72 /*--.. Data layout .........................................................--*/
73 const char * LLVMGetDataLayout(LLVMModuleRef M) {
74 return unwrap(M)->getDataLayout().c_str();
77 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
78 unwrap(M)->setDataLayout(Triple);
81 /*--.. Target triple .......................................................--*/
82 const char * LLVMGetTarget(LLVMModuleRef M) {
83 return unwrap(M)->getTargetTriple().c_str();
86 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
87 unwrap(M)->setTargetTriple(Triple);
90 /*--.. Type names ..........................................................--*/
91 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
92 return unwrap(M)->addTypeName(Name, unwrap(Ty));
95 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
99 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
104 void LLVMDumpModule(LLVMModuleRef M) {
109 /*===-- Operations on types -----------------------------------------------===*/
111 /*--.. Operations on all types (mostly) ....................................--*/
113 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
114 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
117 /*--.. Operations on integer types .........................................--*/
119 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
120 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
121 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
122 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
123 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
125 LLVMTypeRef LLVMIntType(unsigned NumBits) {
126 return wrap(IntegerType::get(NumBits));
129 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
130 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
133 /*--.. Operations on real types ............................................--*/
135 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
136 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
137 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
138 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
139 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
141 /*--.. Operations on function types ........................................--*/
143 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
144 LLVMTypeRef *ParamTypes, unsigned ParamCount,
146 std::vector<const Type*> Tys;
147 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
148 Tys.push_back(unwrap(*I));
150 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
153 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
154 return unwrap<FunctionType>(FunctionTy)->isVarArg();
157 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
158 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
161 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
162 return unwrap<FunctionType>(FunctionTy)->getNumParams();
165 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
166 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
167 for (FunctionType::param_iterator I = Ty->param_begin(),
168 E = Ty->param_end(); I != E; ++I)
172 /*--.. Operations on struct types ..........................................--*/
174 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
175 unsigned ElementCount, int Packed) {
176 std::vector<const Type*> Tys;
177 for (LLVMTypeRef *I = ElementTypes,
178 *E = ElementTypes + ElementCount; I != E; ++I)
179 Tys.push_back(unwrap(*I));
181 return wrap(StructType::get(Tys, Packed != 0));
184 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
185 return unwrap<StructType>(StructTy)->getNumElements();
188 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
189 StructType *Ty = unwrap<StructType>(StructTy);
190 for (FunctionType::param_iterator I = Ty->element_begin(),
191 E = Ty->element_end(); I != E; ++I)
195 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
196 return unwrap<StructType>(StructTy)->isPacked();
199 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
201 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
202 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
205 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
206 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
209 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
210 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
213 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
214 return wrap(unwrap<SequentialType>(Ty)->getElementType());
217 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
218 return unwrap<ArrayType>(ArrayTy)->getNumElements();
221 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
222 return unwrap<PointerType>(PointerTy)->getAddressSpace();
225 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
226 return unwrap<VectorType>(VectorTy)->getNumElements();
229 /*--.. Operations on other types ...........................................--*/
231 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
232 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
234 LLVMTypeRef LLVMOpaqueType(void) {
235 return wrap(llvm::OpaqueType::get());
238 /*--.. Operations on type handles ..........................................--*/
240 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
241 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
244 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
245 delete unwrap(TypeHandle);
248 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
249 return wrap(unwrap(TypeHandle)->get());
252 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
253 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
257 /*===-- Operations on values ----------------------------------------------===*/
259 /*--.. Operations on all values ............................................--*/
261 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
262 return wrap(unwrap(Val)->getType());
265 const char *LLVMGetValueName(LLVMValueRef Val) {
266 return unwrap(Val)->getNameStart();
269 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
270 unwrap(Val)->setName(Name);
273 void LLVMDumpValue(LLVMValueRef Val) {
278 /*--.. Conversion functions ................................................--*/
280 #define LLVM_DEFINE_VALUE_CAST(name) \
281 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
282 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
285 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
288 /*--.. Operations on constants of any type .................................--*/
290 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
291 return wrap(Constant::getNullValue(unwrap(Ty)));
294 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
295 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
298 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
299 return wrap(UndefValue::get(unwrap(Ty)));
302 int LLVMIsConstant(LLVMValueRef Ty) {
303 return isa<Constant>(unwrap(Ty));
306 int LLVMIsNull(LLVMValueRef Val) {
307 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
308 return C->isNullValue();
312 int LLVMIsUndef(LLVMValueRef Val) {
313 return isa<UndefValue>(unwrap(Val));
316 /*--.. Operations on scalar constants ......................................--*/
318 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
320 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
323 static const fltSemantics &SemanticsForType(Type *Ty) {
324 assert(Ty->isFloatingPoint() && "Type is not floating point!");
325 if (Ty == Type::FloatTy)
326 return APFloat::IEEEsingle;
327 if (Ty == Type::DoubleTy)
328 return APFloat::IEEEdouble;
329 if (Ty == Type::X86_FP80Ty)
330 return APFloat::x87DoubleExtended;
331 if (Ty == Type::FP128Ty)
332 return APFloat::IEEEquad;
333 if (Ty == Type::PPC_FP128Ty)
334 return APFloat::PPCDoubleDouble;
335 return APFloat::Bogus;
338 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
341 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
343 return wrap(ConstantFP::get(APN));
346 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
347 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
350 /*--.. Operations on composite constants ...................................--*/
352 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
353 int DontNullTerminate) {
354 /* Inverted the sense of AddNull because ', 0)' is a
355 better mnemonic for null termination than ', 1)'. */
356 return wrap(ConstantArray::get(std::string(Str, Length),
357 DontNullTerminate == 0));
360 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
361 LLVMValueRef *ConstantVals, unsigned Length) {
362 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
363 unwrap<Constant>(ConstantVals, Length),
367 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
369 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
370 Count, Packed != 0));
373 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
374 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
378 /*--.. Constant expressions ................................................--*/
380 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
381 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
384 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
385 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
388 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
389 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
392 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
393 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
396 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
397 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
398 unwrap<Constant>(RHSConstant)));
401 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
402 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
403 unwrap<Constant>(RHSConstant)));
406 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
407 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
408 unwrap<Constant>(RHSConstant)));
411 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
412 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
413 unwrap<Constant>(RHSConstant)));
416 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
418 unwrap<Constant>(RHSConstant)));
421 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
422 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
423 unwrap<Constant>(RHSConstant)));
426 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
427 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
428 unwrap<Constant>(RHSConstant)));
431 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
432 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
433 unwrap<Constant>(RHSConstant)));
436 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
437 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
438 unwrap<Constant>(RHSConstant)));
441 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
442 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
443 unwrap<Constant>(RHSConstant)));
446 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
447 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
448 unwrap<Constant>(RHSConstant)));
451 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
452 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
453 unwrap<Constant>(RHSConstant)));
456 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
457 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
458 return wrap(ConstantExpr::getICmp(Predicate,
459 unwrap<Constant>(LHSConstant),
460 unwrap<Constant>(RHSConstant)));
463 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
464 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
465 return wrap(ConstantExpr::getFCmp(Predicate,
466 unwrap<Constant>(LHSConstant),
467 unwrap<Constant>(RHSConstant)));
470 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
471 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
472 unwrap<Constant>(RHSConstant)));
475 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
476 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
477 unwrap<Constant>(RHSConstant)));
480 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
481 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
482 unwrap<Constant>(RHSConstant)));
485 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
486 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
487 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
488 unwrap<Constant>(ConstantIndices,
493 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
494 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
498 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
499 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
503 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
504 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
508 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
509 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
513 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
514 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
518 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
519 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
523 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
524 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
528 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
529 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
533 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
534 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
538 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
539 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
543 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
544 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
548 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
549 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
553 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
554 LLVMValueRef ConstantIfTrue,
555 LLVMValueRef ConstantIfFalse) {
556 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
557 unwrap<Constant>(ConstantIfTrue),
558 unwrap<Constant>(ConstantIfFalse)));
561 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
562 LLVMValueRef IndexConstant) {
563 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
564 unwrap<Constant>(IndexConstant)));
567 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
568 LLVMValueRef ElementValueConstant,
569 LLVMValueRef IndexConstant) {
570 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
571 unwrap<Constant>(ElementValueConstant),
572 unwrap<Constant>(IndexConstant)));
575 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
576 LLVMValueRef VectorBConstant,
577 LLVMValueRef MaskConstant) {
578 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
579 unwrap<Constant>(VectorBConstant),
580 unwrap<Constant>(MaskConstant)));
583 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
585 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
589 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
590 LLVMValueRef ElementValueConstant,
591 unsigned *IdxList, unsigned NumIdx) {
592 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
593 unwrap<Constant>(ElementValueConstant),
597 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
598 const char *Constraints, int HasSideEffects) {
599 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
600 Constraints, HasSideEffects));
603 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
605 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
606 return wrap(unwrap<GlobalValue>(Global)->getParent());
609 int LLVMIsDeclaration(LLVMValueRef Global) {
610 return unwrap<GlobalValue>(Global)->isDeclaration();
613 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
614 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
617 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
618 unwrap<GlobalValue>(Global)
619 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
622 const char *LLVMGetSection(LLVMValueRef Global) {
623 return unwrap<GlobalValue>(Global)->getSection().c_str();
626 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
627 unwrap<GlobalValue>(Global)->setSection(Section);
630 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
631 return static_cast<LLVMVisibility>(
632 unwrap<GlobalValue>(Global)->getVisibility());
635 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
636 unwrap<GlobalValue>(Global)
637 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
640 unsigned LLVMGetAlignment(LLVMValueRef Global) {
641 return unwrap<GlobalValue>(Global)->getAlignment();
644 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
645 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
648 /*--.. Operations on global variables ......................................--*/
650 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
651 return wrap(new GlobalVariable(unwrap(Ty), false,
652 GlobalValue::ExternalLinkage, 0, Name,
656 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
657 return wrap(unwrap(M)->getNamedGlobal(Name));
660 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
661 Module *Mod = unwrap(M);
662 Module::global_iterator I = Mod->global_begin();
663 if (I == Mod->global_end())
668 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
669 Module *Mod = unwrap(M);
670 Module::global_iterator I = Mod->global_end();
671 if (I == Mod->global_begin())
676 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
677 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
678 Module::global_iterator I = GV;
679 if (++I == GV->getParent()->global_end())
684 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
685 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
686 Module::global_iterator I = GV;
687 if (I == GV->getParent()->global_begin())
692 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
693 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
696 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
697 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
700 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
701 unwrap<GlobalVariable>(GlobalVar)
702 ->setInitializer(unwrap<Constant>(ConstantVal));
705 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
706 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
709 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
710 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
713 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
714 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
717 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
718 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
721 /*--.. Operations on aliases ......................................--*/
723 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
725 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
726 unwrap<Constant>(Aliasee), unwrap (M)));
729 /*--.. Operations on functions .............................................--*/
731 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
732 LLVMTypeRef FunctionTy) {
733 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
734 GlobalValue::ExternalLinkage, Name, unwrap(M)));
737 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
738 return wrap(unwrap(M)->getFunction(Name));
741 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
742 Module *Mod = unwrap(M);
743 Module::iterator I = Mod->begin();
749 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
750 Module *Mod = unwrap(M);
751 Module::iterator I = Mod->end();
752 if (I == Mod->begin())
757 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
758 Function *Func = unwrap<Function>(Fn);
759 Module::iterator I = Func;
760 if (++I == Func->getParent()->end())
765 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
766 Function *Func = unwrap<Function>(Fn);
767 Module::iterator I = Func;
768 if (I == Func->getParent()->begin())
773 void LLVMDeleteFunction(LLVMValueRef Fn) {
774 unwrap<Function>(Fn)->eraseFromParent();
777 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
778 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
779 return F->getIntrinsicID();
783 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
784 return unwrap<Function>(Fn)->getCallingConv();
787 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
788 return unwrap<Function>(Fn)->setCallingConv(CC);
791 const char *LLVMGetGC(LLVMValueRef Fn) {
792 Function *F = unwrap<Function>(Fn);
793 return F->hasGC()? F->getGC() : 0;
796 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
797 Function *F = unwrap<Function>(Fn);
804 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
805 Function *Func = unwrap<Function>(Fn);
806 const AttrListPtr PAL = Func->getAttributes();
807 const AttrListPtr PALnew = PAL.addAttr(0, PA);
808 Func->setAttributes(PALnew);
811 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
812 Function *Func = unwrap<Function>(Fn);
813 const AttrListPtr PAL = Func->getAttributes();
814 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
815 Func->setAttributes(PALnew);
818 /*--.. Operations on parameters ............................................--*/
820 unsigned LLVMCountParams(LLVMValueRef FnRef) {
821 // This function is strictly redundant to
822 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
823 return unwrap<Function>(FnRef)->arg_size();
826 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
827 Function *Fn = unwrap<Function>(FnRef);
828 for (Function::arg_iterator I = Fn->arg_begin(),
829 E = Fn->arg_end(); I != E; I++)
830 *ParamRefs++ = wrap(I);
833 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
834 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
840 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
841 return wrap(unwrap<Argument>(V)->getParent());
844 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
845 Function *Func = unwrap<Function>(Fn);
846 Function::arg_iterator I = Func->arg_begin();
847 if (I == Func->arg_end())
852 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
853 Function *Func = unwrap<Function>(Fn);
854 Function::arg_iterator I = Func->arg_end();
855 if (I == Func->arg_begin())
860 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
861 Argument *A = unwrap<Argument>(Arg);
862 Function::arg_iterator I = A;
863 if (++I == A->getParent()->arg_end())
868 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
869 Argument *A = unwrap<Argument>(Arg);
870 Function::arg_iterator I = A;
871 if (I == A->getParent()->arg_begin())
876 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
877 unwrap<Argument>(Arg)->addAttr(PA);
880 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
881 unwrap<Argument>(Arg)->removeAttr(PA);
884 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
885 unwrap<Argument>(Arg)->addAttr(
886 Attribute::constructAlignmentFromInt(align));
889 /*--.. Operations on basic blocks ..........................................--*/
891 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
892 return wrap(static_cast<Value*>(unwrap(BB)));
895 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
896 return isa<BasicBlock>(unwrap(Val));
899 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
900 return wrap(unwrap<BasicBlock>(Val));
903 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
904 return wrap(unwrap(BB)->getParent());
907 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
908 return unwrap<Function>(FnRef)->size();
911 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
912 Function *Fn = unwrap<Function>(FnRef);
913 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
914 *BasicBlocksRefs++ = wrap(I);
917 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
918 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
921 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
922 Function *Func = unwrap<Function>(Fn);
923 Function::iterator I = Func->begin();
924 if (I == Func->end())
929 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
930 Function *Func = unwrap<Function>(Fn);
931 Function::iterator I = Func->end();
932 if (I == Func->begin())
937 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
938 BasicBlock *Block = unwrap(BB);
939 Function::iterator I = Block;
940 if (++I == Block->getParent()->end())
945 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
946 BasicBlock *Block = unwrap(BB);
947 Function::iterator I = Block;
948 if (I == Block->getParent()->begin())
953 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
954 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
957 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
959 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
960 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
964 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
965 unwrap(BBRef)->eraseFromParent();
968 /*--.. Operations on instructions ..........................................--*/
970 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
971 return wrap(unwrap<Instruction>(Inst)->getParent());
974 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
975 BasicBlock *Block = unwrap(BB);
976 BasicBlock::iterator I = Block->begin();
977 if (I == Block->end())
982 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
983 BasicBlock *Block = unwrap(BB);
984 BasicBlock::iterator I = Block->end();
985 if (I == Block->begin())
990 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
991 Instruction *Instr = unwrap<Instruction>(Inst);
992 BasicBlock::iterator I = Instr;
993 if (++I == Instr->getParent()->end())
998 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
999 Instruction *Instr = unwrap<Instruction>(Inst);
1000 BasicBlock::iterator I = Instr;
1001 if (I == Instr->getParent()->begin())
1006 /*--.. Call and invoke instructions ........................................--*/
1008 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1009 Value *V = unwrap(Instr);
1010 if (CallInst *CI = dyn_cast<CallInst>(V))
1011 return CI->getCallingConv();
1012 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1013 return II->getCallingConv();
1014 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
1018 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1019 Value *V = unwrap(Instr);
1020 if (CallInst *CI = dyn_cast<CallInst>(V))
1021 return CI->setCallingConv(CC);
1022 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1023 return II->setCallingConv(CC);
1024 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1027 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1029 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1031 Call.getAttributes().addAttr(index, PA));
1034 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1036 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1038 Call.getAttributes().removeAttr(index, PA));
1041 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1043 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1045 Call.getAttributes().addAttr(index,
1046 Attribute::constructAlignmentFromInt(align)));
1049 /*--.. Operations on call instructions (only) ..............................--*/
1051 int LLVMIsTailCall(LLVMValueRef Call) {
1052 return unwrap<CallInst>(Call)->isTailCall();
1055 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1056 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1059 /*--.. Operations on phi nodes .............................................--*/
1061 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1062 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1063 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1064 for (unsigned I = 0; I != Count; ++I)
1065 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1068 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1069 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1072 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1073 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1076 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1077 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1081 /*===-- Instruction builders ----------------------------------------------===*/
1083 LLVMBuilderRef LLVMCreateBuilder(void) {
1084 return wrap(new IRBuilder<>());
1087 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1088 LLVMValueRef Instr) {
1089 BasicBlock *BB = unwrap(Block);
1090 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1091 unwrap(Builder)->SetInsertPoint(BB, I);
1094 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1095 Instruction *I = unwrap<Instruction>(Instr);
1096 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1099 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1100 BasicBlock *BB = unwrap(Block);
1101 unwrap(Builder)->SetInsertPoint(BB);
1104 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1105 return wrap(unwrap(Builder)->GetInsertBlock());
1108 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1109 unwrap(Builder)->ClearInsertionPoint ();
1112 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1113 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1116 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1117 delete unwrap(Builder);
1120 /*--.. Instruction builders ................................................--*/
1122 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1123 return wrap(unwrap(B)->CreateRetVoid());
1126 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1127 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1130 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1131 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1134 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1135 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1136 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1139 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1140 LLVMBasicBlockRef Else, unsigned NumCases) {
1141 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1144 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1145 LLVMValueRef *Args, unsigned NumArgs,
1146 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1148 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1149 unwrap(Args), unwrap(Args) + NumArgs,
1153 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1154 return wrap(unwrap(B)->CreateUnwind());
1157 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1158 return wrap(unwrap(B)->CreateUnreachable());
1161 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1162 LLVMBasicBlockRef Dest) {
1163 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1166 /*--.. Arithmetic ..........................................................--*/
1168 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1170 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1173 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1175 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1178 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1180 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1183 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1185 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1188 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1190 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1193 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1195 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1198 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1200 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1203 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1205 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1208 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1210 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1213 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1215 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1218 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1220 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1223 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1225 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1228 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1230 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1233 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1235 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1238 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1240 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1243 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1244 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1247 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1248 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1251 /*--.. Memory ..............................................................--*/
1253 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1255 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1258 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1259 LLVMValueRef Val, const char *Name) {
1260 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1263 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1265 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1268 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1269 LLVMValueRef Val, const char *Name) {
1270 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1273 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1274 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1278 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1280 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1283 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1284 LLVMValueRef PointerVal) {
1285 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1288 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1289 LLVMValueRef *Indices, unsigned NumIndices,
1291 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1292 unwrap(Indices) + NumIndices, Name));
1295 /*--.. Casts ...............................................................--*/
1297 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1298 LLVMTypeRef DestTy, const char *Name) {
1299 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1302 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1303 LLVMTypeRef DestTy, const char *Name) {
1304 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1307 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1308 LLVMTypeRef DestTy, const char *Name) {
1309 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1312 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1313 LLVMTypeRef DestTy, const char *Name) {
1314 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1317 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1318 LLVMTypeRef DestTy, const char *Name) {
1319 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1322 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1323 LLVMTypeRef DestTy, const char *Name) {
1324 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1327 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1328 LLVMTypeRef DestTy, const char *Name) {
1329 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1332 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1333 LLVMTypeRef DestTy, const char *Name) {
1334 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1337 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1338 LLVMTypeRef DestTy, const char *Name) {
1339 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1342 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1343 LLVMTypeRef DestTy, const char *Name) {
1344 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1347 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1348 LLVMTypeRef DestTy, const char *Name) {
1349 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1352 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1353 LLVMTypeRef DestTy, const char *Name) {
1354 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1357 /*--.. Comparisons .........................................................--*/
1359 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1360 LLVMValueRef LHS, LLVMValueRef RHS,
1362 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1363 unwrap(LHS), unwrap(RHS), Name));
1366 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1367 LLVMValueRef LHS, LLVMValueRef RHS,
1369 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1370 unwrap(LHS), unwrap(RHS), Name));
1373 /*--.. Miscellaneous instructions ..........................................--*/
1375 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1376 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1379 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1380 LLVMValueRef *Args, unsigned NumArgs,
1382 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1383 unwrap(Args) + NumArgs, Name));
1386 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1387 LLVMValueRef Then, LLVMValueRef Else,
1389 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1393 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1394 LLVMTypeRef Ty, const char *Name) {
1395 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1398 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1399 LLVMValueRef Index, const char *Name) {
1400 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1404 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1405 LLVMValueRef EltVal, LLVMValueRef Index,
1407 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1408 unwrap(Index), Name));
1411 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1412 LLVMValueRef V2, LLVMValueRef Mask,
1414 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1415 unwrap(Mask), Name));
1418 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1419 unsigned Index, const char *Name) {
1420 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1423 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1424 LLVMValueRef EltVal, unsigned Index,
1426 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1431 /*===-- Module providers --------------------------------------------------===*/
1433 LLVMModuleProviderRef
1434 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1435 return wrap(new ExistingModuleProvider(unwrap(M)));
1438 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1443 /*===-- Memory buffers ----------------------------------------------------===*/
1445 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1446 LLVMMemoryBufferRef *OutMemBuf,
1447 char **OutMessage) {
1449 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1450 *OutMemBuf = wrap(MB);
1454 *OutMessage = strdup(Error.c_str());
1458 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1459 char **OutMessage) {
1460 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1461 *OutMemBuf = wrap(MB);
1465 *OutMessage = strdup("stdin is empty.");
1469 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1470 delete unwrap(MemBuf);