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, LLVMContextRef C) {
60 return wrap(new Module(ModuleID, *unwrap(C)));
63 void LLVMDisposeModule(LLVMModuleRef M) {
67 /*--.. Data layout .........................................................--*/
68 const char * LLVMGetDataLayout(LLVMModuleRef M) {
69 return unwrap(M)->getDataLayout().c_str();
72 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
73 unwrap(M)->setDataLayout(Triple);
76 /*--.. Target triple .......................................................--*/
77 const char * LLVMGetTarget(LLVMModuleRef M) {
78 return unwrap(M)->getTargetTriple().c_str();
81 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
82 unwrap(M)->setTargetTriple(Triple);
85 /*--.. Type names ..........................................................--*/
86 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
87 return unwrap(M)->addTypeName(Name, unwrap(Ty));
90 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
93 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
94 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
99 void LLVMDumpModule(LLVMModuleRef M) {
104 /*===-- Operations on types -----------------------------------------------===*/
106 /*--.. Operations on all types (mostly) ....................................--*/
108 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
109 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
112 /*--.. Operations on integer types .........................................--*/
114 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
115 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
116 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
117 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
118 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
120 LLVMTypeRef LLVMIntType(unsigned NumBits) {
121 return wrap(IntegerType::get(NumBits));
124 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
125 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
128 /*--.. Operations on real types ............................................--*/
130 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
131 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
132 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
133 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
134 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
136 /*--.. Operations on function types ........................................--*/
138 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
139 LLVMTypeRef *ParamTypes, unsigned ParamCount,
141 std::vector<const Type*> Tys;
142 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
143 Tys.push_back(unwrap(*I));
145 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
148 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
149 return unwrap<FunctionType>(FunctionTy)->isVarArg();
152 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
153 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
156 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
157 return unwrap<FunctionType>(FunctionTy)->getNumParams();
160 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
161 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
162 for (FunctionType::param_iterator I = Ty->param_begin(),
163 E = Ty->param_end(); I != E; ++I)
167 /*--.. Operations on struct types ..........................................--*/
169 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
170 unsigned ElementCount, int Packed) {
171 std::vector<const Type*> Tys;
172 for (LLVMTypeRef *I = ElementTypes,
173 *E = ElementTypes + ElementCount; I != E; ++I)
174 Tys.push_back(unwrap(*I));
176 return wrap(StructType::get(Tys, Packed != 0));
179 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
180 return unwrap<StructType>(StructTy)->getNumElements();
183 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
184 StructType *Ty = unwrap<StructType>(StructTy);
185 for (FunctionType::param_iterator I = Ty->element_begin(),
186 E = Ty->element_end(); I != E; ++I)
190 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
191 return unwrap<StructType>(StructTy)->isPacked();
194 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
196 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
197 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
200 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
201 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
204 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
205 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
208 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
209 return wrap(unwrap<SequentialType>(Ty)->getElementType());
212 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
213 return unwrap<ArrayType>(ArrayTy)->getNumElements();
216 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
217 return unwrap<PointerType>(PointerTy)->getAddressSpace();
220 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
221 return unwrap<VectorType>(VectorTy)->getNumElements();
224 /*--.. Operations on other types ...........................................--*/
226 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
227 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
229 LLVMTypeRef LLVMOpaqueType(void) {
230 return wrap(llvm::OpaqueType::get());
233 /*--.. Operations on type handles ..........................................--*/
235 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
236 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
239 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
240 delete unwrap(TypeHandle);
243 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
244 return wrap(unwrap(TypeHandle)->get());
247 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
248 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
252 /*===-- Operations on values ----------------------------------------------===*/
254 /*--.. Operations on all values ............................................--*/
256 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
257 return wrap(unwrap(Val)->getType());
260 const char *LLVMGetValueName(LLVMValueRef Val) {
261 return unwrap(Val)->getNameStart();
264 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
265 unwrap(Val)->setName(Name);
268 void LLVMDumpValue(LLVMValueRef Val) {
273 /*--.. Conversion functions ................................................--*/
275 #define LLVM_DEFINE_VALUE_CAST(name) \
276 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
277 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
280 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
283 /*--.. Operations on constants of any type .................................--*/
285 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
286 return wrap(Constant::getNullValue(unwrap(Ty)));
289 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
290 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
293 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
294 return wrap(UndefValue::get(unwrap(Ty)));
297 int LLVMIsConstant(LLVMValueRef Ty) {
298 return isa<Constant>(unwrap(Ty));
301 int LLVMIsNull(LLVMValueRef Val) {
302 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
303 return C->isNullValue();
307 int LLVMIsUndef(LLVMValueRef Val) {
308 return isa<UndefValue>(unwrap(Val));
311 /*--.. Operations on scalar constants ......................................--*/
313 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
315 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
318 static const fltSemantics &SemanticsForType(Type *Ty) {
319 assert(Ty->isFloatingPoint() && "Type is not floating point!");
320 if (Ty == Type::FloatTy)
321 return APFloat::IEEEsingle;
322 if (Ty == Type::DoubleTy)
323 return APFloat::IEEEdouble;
324 if (Ty == Type::X86_FP80Ty)
325 return APFloat::x87DoubleExtended;
326 if (Ty == Type::FP128Ty)
327 return APFloat::IEEEquad;
328 if (Ty == Type::PPC_FP128Ty)
329 return APFloat::PPCDoubleDouble;
330 return APFloat::Bogus;
333 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
336 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
338 return wrap(ConstantFP::get(APN));
341 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
342 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
345 /*--.. Operations on composite constants ...................................--*/
347 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
348 int DontNullTerminate) {
349 /* Inverted the sense of AddNull because ', 0)' is a
350 better mnemonic for null termination than ', 1)'. */
351 return wrap(ConstantArray::get(std::string(Str, Length),
352 DontNullTerminate == 0));
355 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
356 LLVMValueRef *ConstantVals, unsigned Length) {
357 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
358 unwrap<Constant>(ConstantVals, Length),
362 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
364 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
365 Count, Packed != 0));
368 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
369 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
373 /*--.. Constant expressions ................................................--*/
375 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
376 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
379 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
380 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
383 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
384 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
387 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
388 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
391 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
392 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
393 unwrap<Constant>(RHSConstant)));
396 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
397 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
398 unwrap<Constant>(RHSConstant)));
401 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
402 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
403 unwrap<Constant>(RHSConstant)));
406 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
407 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
408 unwrap<Constant>(RHSConstant)));
411 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
412 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
413 unwrap<Constant>(RHSConstant)));
416 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
418 unwrap<Constant>(RHSConstant)));
421 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
422 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
423 unwrap<Constant>(RHSConstant)));
426 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
427 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
428 unwrap<Constant>(RHSConstant)));
431 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
432 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
433 unwrap<Constant>(RHSConstant)));
436 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
437 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
438 unwrap<Constant>(RHSConstant)));
441 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
442 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
443 unwrap<Constant>(RHSConstant)));
446 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
447 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
448 unwrap<Constant>(RHSConstant)));
451 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
452 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
453 return wrap(ConstantExpr::getICmp(Predicate,
454 unwrap<Constant>(LHSConstant),
455 unwrap<Constant>(RHSConstant)));
458 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
459 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
460 return wrap(ConstantExpr::getFCmp(Predicate,
461 unwrap<Constant>(LHSConstant),
462 unwrap<Constant>(RHSConstant)));
465 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
466 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
467 unwrap<Constant>(RHSConstant)));
470 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
471 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
472 unwrap<Constant>(RHSConstant)));
475 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
476 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
477 unwrap<Constant>(RHSConstant)));
480 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
481 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
482 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
483 unwrap<Constant>(ConstantIndices,
488 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
489 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
493 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
494 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
498 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
499 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
503 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
504 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
508 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
509 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
513 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
514 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
518 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
519 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
523 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
524 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
528 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
529 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
533 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
534 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
538 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
539 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
543 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
544 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
548 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
549 LLVMValueRef ConstantIfTrue,
550 LLVMValueRef ConstantIfFalse) {
551 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
552 unwrap<Constant>(ConstantIfTrue),
553 unwrap<Constant>(ConstantIfFalse)));
556 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
557 LLVMValueRef IndexConstant) {
558 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
559 unwrap<Constant>(IndexConstant)));
562 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
563 LLVMValueRef ElementValueConstant,
564 LLVMValueRef IndexConstant) {
565 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
566 unwrap<Constant>(ElementValueConstant),
567 unwrap<Constant>(IndexConstant)));
570 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
571 LLVMValueRef VectorBConstant,
572 LLVMValueRef MaskConstant) {
573 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
574 unwrap<Constant>(VectorBConstant),
575 unwrap<Constant>(MaskConstant)));
578 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
580 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
584 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
585 LLVMValueRef ElementValueConstant,
586 unsigned *IdxList, unsigned NumIdx) {
587 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
588 unwrap<Constant>(ElementValueConstant),
592 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
593 const char *Constraints, int HasSideEffects) {
594 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
595 Constraints, HasSideEffects));
598 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
600 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
601 return wrap(unwrap<GlobalValue>(Global)->getParent());
604 int LLVMIsDeclaration(LLVMValueRef Global) {
605 return unwrap<GlobalValue>(Global)->isDeclaration();
608 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
609 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
612 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
613 unwrap<GlobalValue>(Global)
614 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
617 const char *LLVMGetSection(LLVMValueRef Global) {
618 return unwrap<GlobalValue>(Global)->getSection().c_str();
621 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
622 unwrap<GlobalValue>(Global)->setSection(Section);
625 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
626 return static_cast<LLVMVisibility>(
627 unwrap<GlobalValue>(Global)->getVisibility());
630 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
631 unwrap<GlobalValue>(Global)
632 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
635 unsigned LLVMGetAlignment(LLVMValueRef Global) {
636 return unwrap<GlobalValue>(Global)->getAlignment();
639 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
640 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
643 /*--.. Operations on global variables ......................................--*/
645 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
646 return wrap(new GlobalVariable(unwrap(Ty), false,
647 GlobalValue::ExternalLinkage, 0, Name,
651 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
652 return wrap(unwrap(M)->getNamedGlobal(Name));
655 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
656 Module *Mod = unwrap(M);
657 Module::global_iterator I = Mod->global_begin();
658 if (I == Mod->global_end())
663 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
664 Module *Mod = unwrap(M);
665 Module::global_iterator I = Mod->global_end();
666 if (I == Mod->global_begin())
671 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
672 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
673 Module::global_iterator I = GV;
674 if (++I == GV->getParent()->global_end())
679 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
680 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
681 Module::global_iterator I = GV;
682 if (I == GV->getParent()->global_begin())
687 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
688 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
691 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
692 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
695 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
696 unwrap<GlobalVariable>(GlobalVar)
697 ->setInitializer(unwrap<Constant>(ConstantVal));
700 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
701 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
704 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
705 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
708 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
709 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
712 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
713 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
716 /*--.. Operations on aliases ......................................--*/
718 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
720 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
721 unwrap<Constant>(Aliasee), unwrap (M)));
724 /*--.. Operations on functions .............................................--*/
726 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
727 LLVMTypeRef FunctionTy) {
728 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
729 GlobalValue::ExternalLinkage, Name, unwrap(M)));
732 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
733 return wrap(unwrap(M)->getFunction(Name));
736 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
737 Module *Mod = unwrap(M);
738 Module::iterator I = Mod->begin();
744 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
745 Module *Mod = unwrap(M);
746 Module::iterator I = Mod->end();
747 if (I == Mod->begin())
752 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
753 Function *Func = unwrap<Function>(Fn);
754 Module::iterator I = Func;
755 if (++I == Func->getParent()->end())
760 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
761 Function *Func = unwrap<Function>(Fn);
762 Module::iterator I = Func;
763 if (I == Func->getParent()->begin())
768 void LLVMDeleteFunction(LLVMValueRef Fn) {
769 unwrap<Function>(Fn)->eraseFromParent();
772 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
773 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
774 return F->getIntrinsicID();
778 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
779 return unwrap<Function>(Fn)->getCallingConv();
782 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
783 return unwrap<Function>(Fn)->setCallingConv(CC);
786 const char *LLVMGetGC(LLVMValueRef Fn) {
787 Function *F = unwrap<Function>(Fn);
788 return F->hasGC()? F->getGC() : 0;
791 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
792 Function *F = unwrap<Function>(Fn);
799 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
800 Function *Func = unwrap<Function>(Fn);
801 const AttrListPtr PAL = Func->getAttributes();
802 const AttrListPtr PALnew = PAL.addAttr(0, PA);
803 Func->setAttributes(PALnew);
806 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
807 Function *Func = unwrap<Function>(Fn);
808 const AttrListPtr PAL = Func->getAttributes();
809 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
810 Func->setAttributes(PALnew);
813 /*--.. Operations on parameters ............................................--*/
815 unsigned LLVMCountParams(LLVMValueRef FnRef) {
816 // This function is strictly redundant to
817 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
818 return unwrap<Function>(FnRef)->arg_size();
821 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
822 Function *Fn = unwrap<Function>(FnRef);
823 for (Function::arg_iterator I = Fn->arg_begin(),
824 E = Fn->arg_end(); I != E; I++)
825 *ParamRefs++ = wrap(I);
828 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
829 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
835 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
836 return wrap(unwrap<Argument>(V)->getParent());
839 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
840 Function *Func = unwrap<Function>(Fn);
841 Function::arg_iterator I = Func->arg_begin();
842 if (I == Func->arg_end())
847 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
848 Function *Func = unwrap<Function>(Fn);
849 Function::arg_iterator I = Func->arg_end();
850 if (I == Func->arg_begin())
855 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
856 Argument *A = unwrap<Argument>(Arg);
857 Function::arg_iterator I = A;
858 if (++I == A->getParent()->arg_end())
863 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
864 Argument *A = unwrap<Argument>(Arg);
865 Function::arg_iterator I = A;
866 if (I == A->getParent()->arg_begin())
871 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
872 unwrap<Argument>(Arg)->addAttr(PA);
875 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
876 unwrap<Argument>(Arg)->removeAttr(PA);
879 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
880 unwrap<Argument>(Arg)->addAttr(
881 Attribute::constructAlignmentFromInt(align));
884 /*--.. Operations on basic blocks ..........................................--*/
886 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
887 return wrap(static_cast<Value*>(unwrap(BB)));
890 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
891 return isa<BasicBlock>(unwrap(Val));
894 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
895 return wrap(unwrap<BasicBlock>(Val));
898 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
899 return wrap(unwrap(BB)->getParent());
902 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
903 return unwrap<Function>(FnRef)->size();
906 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
907 Function *Fn = unwrap<Function>(FnRef);
908 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
909 *BasicBlocksRefs++ = wrap(I);
912 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
913 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
916 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
917 Function *Func = unwrap<Function>(Fn);
918 Function::iterator I = Func->begin();
919 if (I == Func->end())
924 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
925 Function *Func = unwrap<Function>(Fn);
926 Function::iterator I = Func->end();
927 if (I == Func->begin())
932 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
933 BasicBlock *Block = unwrap(BB);
934 Function::iterator I = Block;
935 if (++I == Block->getParent()->end())
940 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
941 BasicBlock *Block = unwrap(BB);
942 Function::iterator I = Block;
943 if (I == Block->getParent()->begin())
948 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
949 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
952 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
954 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
955 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
959 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
960 unwrap(BBRef)->eraseFromParent();
963 /*--.. Operations on instructions ..........................................--*/
965 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
966 return wrap(unwrap<Instruction>(Inst)->getParent());
969 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
970 BasicBlock *Block = unwrap(BB);
971 BasicBlock::iterator I = Block->begin();
972 if (I == Block->end())
977 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
978 BasicBlock *Block = unwrap(BB);
979 BasicBlock::iterator I = Block->end();
980 if (I == Block->begin())
985 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
986 Instruction *Instr = unwrap<Instruction>(Inst);
987 BasicBlock::iterator I = Instr;
988 if (++I == Instr->getParent()->end())
993 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
994 Instruction *Instr = unwrap<Instruction>(Inst);
995 BasicBlock::iterator I = Instr;
996 if (I == Instr->getParent()->begin())
1001 /*--.. Call and invoke instructions ........................................--*/
1003 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1004 Value *V = unwrap(Instr);
1005 if (CallInst *CI = dyn_cast<CallInst>(V))
1006 return CI->getCallingConv();
1007 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1008 return II->getCallingConv();
1009 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
1013 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1014 Value *V = unwrap(Instr);
1015 if (CallInst *CI = dyn_cast<CallInst>(V))
1016 return CI->setCallingConv(CC);
1017 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1018 return II->setCallingConv(CC);
1019 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1022 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1024 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1026 Call.getAttributes().addAttr(index, PA));
1029 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1031 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1033 Call.getAttributes().removeAttr(index, PA));
1036 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1038 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1040 Call.getAttributes().addAttr(index,
1041 Attribute::constructAlignmentFromInt(align)));
1044 /*--.. Operations on call instructions (only) ..............................--*/
1046 int LLVMIsTailCall(LLVMValueRef Call) {
1047 return unwrap<CallInst>(Call)->isTailCall();
1050 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1051 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1054 /*--.. Operations on phi nodes .............................................--*/
1056 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1057 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1058 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1059 for (unsigned I = 0; I != Count; ++I)
1060 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1063 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1064 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1067 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1068 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1071 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1072 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1076 /*===-- Instruction builders ----------------------------------------------===*/
1078 LLVMBuilderRef LLVMCreateBuilder(void) {
1079 return wrap(new IRBuilder<>());
1082 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1083 LLVMValueRef Instr) {
1084 BasicBlock *BB = unwrap(Block);
1085 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1086 unwrap(Builder)->SetInsertPoint(BB, I);
1089 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1090 Instruction *I = unwrap<Instruction>(Instr);
1091 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1094 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1095 BasicBlock *BB = unwrap(Block);
1096 unwrap(Builder)->SetInsertPoint(BB);
1099 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1100 return wrap(unwrap(Builder)->GetInsertBlock());
1103 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1104 unwrap(Builder)->ClearInsertionPoint ();
1107 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1108 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1111 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1112 delete unwrap(Builder);
1115 /*--.. Instruction builders ................................................--*/
1117 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1118 return wrap(unwrap(B)->CreateRetVoid());
1121 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1122 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1125 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1126 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1129 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1130 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1131 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1134 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1135 LLVMBasicBlockRef Else, unsigned NumCases) {
1136 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1139 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1140 LLVMValueRef *Args, unsigned NumArgs,
1141 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1143 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1144 unwrap(Args), unwrap(Args) + NumArgs,
1148 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1149 return wrap(unwrap(B)->CreateUnwind());
1152 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1153 return wrap(unwrap(B)->CreateUnreachable());
1156 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1157 LLVMBasicBlockRef Dest) {
1158 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1161 /*--.. Arithmetic ..........................................................--*/
1163 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1165 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1168 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1170 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1173 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1175 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1178 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1180 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1183 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1185 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1188 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1190 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1193 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1195 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1198 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1200 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1203 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1205 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1208 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1210 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1213 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1215 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1218 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1220 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1223 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1225 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1228 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1230 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1233 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1235 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1238 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1239 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1242 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1243 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1246 /*--.. Memory ..............................................................--*/
1248 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1250 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1253 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1254 LLVMValueRef Val, const char *Name) {
1255 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1258 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1260 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1263 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1264 LLVMValueRef Val, const char *Name) {
1265 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1268 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1269 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1273 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1275 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1278 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1279 LLVMValueRef PointerVal) {
1280 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1283 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1284 LLVMValueRef *Indices, unsigned NumIndices,
1286 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1287 unwrap(Indices) + NumIndices, Name));
1290 /*--.. Casts ...............................................................--*/
1292 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1293 LLVMTypeRef DestTy, const char *Name) {
1294 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1297 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1298 LLVMTypeRef DestTy, const char *Name) {
1299 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1302 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1303 LLVMTypeRef DestTy, const char *Name) {
1304 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1307 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1308 LLVMTypeRef DestTy, const char *Name) {
1309 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1312 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1313 LLVMTypeRef DestTy, const char *Name) {
1314 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1317 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1318 LLVMTypeRef DestTy, const char *Name) {
1319 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1322 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1323 LLVMTypeRef DestTy, const char *Name) {
1324 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1327 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1328 LLVMTypeRef DestTy, const char *Name) {
1329 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1332 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1333 LLVMTypeRef DestTy, const char *Name) {
1334 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1337 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1338 LLVMTypeRef DestTy, const char *Name) {
1339 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1342 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1343 LLVMTypeRef DestTy, const char *Name) {
1344 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1347 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1348 LLVMTypeRef DestTy, const char *Name) {
1349 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1352 /*--.. Comparisons .........................................................--*/
1354 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1355 LLVMValueRef LHS, LLVMValueRef RHS,
1357 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1358 unwrap(LHS), unwrap(RHS), Name));
1361 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1362 LLVMValueRef LHS, LLVMValueRef RHS,
1364 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1365 unwrap(LHS), unwrap(RHS), Name));
1368 /*--.. Miscellaneous instructions ..........................................--*/
1370 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1371 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1374 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1375 LLVMValueRef *Args, unsigned NumArgs,
1377 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1378 unwrap(Args) + NumArgs, Name));
1381 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1382 LLVMValueRef Then, LLVMValueRef Else,
1384 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1388 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1389 LLVMTypeRef Ty, const char *Name) {
1390 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1393 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1394 LLVMValueRef Index, const char *Name) {
1395 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1399 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1400 LLVMValueRef EltVal, LLVMValueRef Index,
1402 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1403 unwrap(Index), Name));
1406 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1407 LLVMValueRef V2, LLVMValueRef Mask,
1409 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1410 unwrap(Mask), Name));
1413 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1414 unsigned Index, const char *Name) {
1415 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1418 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1419 LLVMValueRef EltVal, unsigned Index,
1421 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1426 /*===-- Module providers --------------------------------------------------===*/
1428 LLVMModuleProviderRef
1429 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1430 return wrap(new ExistingModuleProvider(unwrap(M)));
1433 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1438 /*===-- Memory buffers ----------------------------------------------------===*/
1440 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1441 LLVMMemoryBufferRef *OutMemBuf,
1442 char **OutMessage) {
1444 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1445 *OutMemBuf = wrap(MB);
1449 *OutMessage = strdup(Error.c_str());
1453 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1454 char **OutMessage) {
1455 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1456 *OutMemBuf = wrap(MB);
1460 *OutMessage = strdup("stdin is empty.");
1464 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1465 delete unwrap(MemBuf);