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/TypeSymbolTable.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/Support/MemoryBuffer.h"
30 /*===-- Error handling ----------------------------------------------------===*/
32 void LLVMDisposeMessage(char *Message) {
37 /*===-- Operations on modules ---------------------------------------------===*/
39 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
40 return wrap(new Module(ModuleID));
43 void LLVMDisposeModule(LLVMModuleRef M) {
47 /*--.. Data layout .........................................................--*/
48 const char * LLVMGetDataLayout(LLVMModuleRef M) {
49 return unwrap(M)->getDataLayout().c_str();
52 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
53 unwrap(M)->setDataLayout(Triple);
56 /*--.. Target triple .......................................................--*/
57 const char * LLVMGetTarget(LLVMModuleRef M) {
58 return unwrap(M)->getTargetTriple().c_str();
61 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
62 unwrap(M)->setTargetTriple(Triple);
65 /*--.. Type names ..........................................................--*/
66 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
67 return unwrap(M)->addTypeName(Name, unwrap(Ty));
70 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
73 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
74 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
79 void LLVMDumpModule(LLVMModuleRef M) {
84 /*===-- Operations on types -----------------------------------------------===*/
86 /*--.. Operations on all types (mostly) ....................................--*/
88 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
89 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
92 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
93 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
94 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
97 /*--.. Operations on integer types .........................................--*/
99 LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
100 LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
101 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
102 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
103 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
105 LLVMTypeRef LLVMIntType(unsigned NumBits) {
106 return wrap(IntegerType::get(NumBits));
109 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
110 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
113 /*--.. Operations on real types ............................................--*/
115 LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
116 LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
117 LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
118 LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
119 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
121 /*--.. Operations on function types ........................................--*/
123 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
124 LLVMTypeRef *ParamTypes, unsigned ParamCount,
126 std::vector<const Type*> Tys;
127 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
128 Tys.push_back(unwrap(*I));
130 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
133 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
134 return unwrap<FunctionType>(FunctionTy)->isVarArg();
137 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
138 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
141 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
142 return unwrap<FunctionType>(FunctionTy)->getNumParams();
145 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
146 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
147 for (FunctionType::param_iterator I = Ty->param_begin(),
148 E = Ty->param_end(); I != E; ++I)
152 /*--.. Operations on struct types ..........................................--*/
154 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
155 unsigned ElementCount, int Packed) {
156 std::vector<const Type*> Tys;
157 for (LLVMTypeRef *I = ElementTypes,
158 *E = ElementTypes + ElementCount; I != E; ++I)
159 Tys.push_back(unwrap(*I));
161 return wrap(StructType::get(Tys, Packed != 0));
164 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
165 return unwrap<StructType>(StructTy)->getNumElements();
168 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
169 StructType *Ty = unwrap<StructType>(StructTy);
170 for (FunctionType::param_iterator I = Ty->element_begin(),
171 E = Ty->element_end(); I != E; ++I)
175 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
176 return unwrap<StructType>(StructTy)->isPacked();
179 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
181 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
182 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
185 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
186 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
189 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
190 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
193 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
194 return wrap(unwrap<SequentialType>(Ty)->getElementType());
197 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
198 return unwrap<ArrayType>(ArrayTy)->getNumElements();
201 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
202 return unwrap<PointerType>(PointerTy)->getAddressSpace();
205 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
206 return unwrap<VectorType>(VectorTy)->getNumElements();
209 /*--.. Operations on other types ...........................................--*/
211 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
212 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
214 LLVMTypeRef LLVMOpaqueType() {
215 return wrap(llvm::OpaqueType::get());
218 /*--.. Operations on type handles ..........................................--*/
220 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
221 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
224 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
225 delete unwrap(TypeHandle);
228 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
229 return wrap(unwrap(TypeHandle)->get());
232 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
233 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
237 /*===-- Operations on values ----------------------------------------------===*/
239 /*--.. Operations on all values ............................................--*/
241 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
242 return wrap(unwrap(Val)->getType());
245 const char *LLVMGetValueName(LLVMValueRef Val) {
246 return unwrap(Val)->getNameStart();
249 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
250 unwrap(Val)->setName(Name);
253 void LLVMDumpValue(LLVMValueRef Val) {
257 /*--.. Operations on constants of any type .................................--*/
259 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
260 return wrap(Constant::getNullValue(unwrap(Ty)));
263 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
264 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
267 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
268 return wrap(UndefValue::get(unwrap(Ty)));
271 int LLVMIsConstant(LLVMValueRef Ty) {
272 return isa<Constant>(unwrap(Ty));
275 int LLVMIsNull(LLVMValueRef Val) {
276 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
277 return C->isNullValue();
281 int LLVMIsUndef(LLVMValueRef Val) {
282 return isa<UndefValue>(unwrap(Val));
285 /*--.. Operations on scalar constants ......................................--*/
287 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
289 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
292 static const fltSemantics &SemanticsForType(Type *Ty) {
293 assert(Ty->isFloatingPoint() && "Type is not floating point!");
294 if (Ty == Type::FloatTy)
295 return APFloat::IEEEsingle;
296 if (Ty == Type::DoubleTy)
297 return APFloat::IEEEdouble;
298 if (Ty == Type::X86_FP80Ty)
299 return APFloat::x87DoubleExtended;
300 if (Ty == Type::FP128Ty)
301 return APFloat::IEEEquad;
302 if (Ty == Type::PPC_FP128Ty)
303 return APFloat::PPCDoubleDouble;
304 return APFloat::Bogus;
307 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
309 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
310 return wrap(ConstantFP::get(unwrap(RealTy), APN));
313 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
314 return wrap(ConstantFP::get(unwrap(RealTy),
315 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
318 /*--.. Operations on composite constants ...................................--*/
320 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
321 int DontNullTerminate) {
322 /* Inverted the sense of AddNull because ', 0)' is a
323 better mnemonic for null termination than ', 1)'. */
324 return wrap(ConstantArray::get(std::string(Str, Length),
325 DontNullTerminate == 0));
328 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
329 LLVMValueRef *ConstantVals, unsigned Length) {
330 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
331 unwrap<Constant>(ConstantVals, Length),
335 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
337 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
338 Count, Packed != 0));
341 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
342 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
346 /*--.. Constant expressions ................................................--*/
348 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
349 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
352 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
353 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
356 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
357 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
360 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
361 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
362 unwrap<Constant>(RHSConstant)));
365 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
366 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
367 unwrap<Constant>(RHSConstant)));
370 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
371 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
372 unwrap<Constant>(RHSConstant)));
375 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
377 unwrap<Constant>(RHSConstant)));
380 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
381 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
382 unwrap<Constant>(RHSConstant)));
385 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
386 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
387 unwrap<Constant>(RHSConstant)));
390 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
391 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
392 unwrap<Constant>(RHSConstant)));
395 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
396 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
400 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
401 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
402 unwrap<Constant>(RHSConstant)));
405 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
407 unwrap<Constant>(RHSConstant)));
410 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
412 unwrap<Constant>(RHSConstant)));
415 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
416 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
417 unwrap<Constant>(RHSConstant)));
420 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
421 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
422 return wrap(ConstantExpr::getICmp(Predicate,
423 unwrap<Constant>(LHSConstant),
424 unwrap<Constant>(RHSConstant)));
427 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
428 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
429 return wrap(ConstantExpr::getFCmp(Predicate,
430 unwrap<Constant>(LHSConstant),
431 unwrap<Constant>(RHSConstant)));
434 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
435 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
436 unwrap<Constant>(RHSConstant)));
439 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
441 unwrap<Constant>(RHSConstant)));
444 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
445 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
446 unwrap<Constant>(RHSConstant)));
449 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
450 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
451 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
452 unwrap<Constant>(ConstantIndices,
457 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
458 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
462 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
463 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
467 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
468 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
472 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
473 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
477 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
478 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
482 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
483 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
487 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
488 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
492 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
493 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
497 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
498 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
502 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
507 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
512 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
513 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
517 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
518 LLVMValueRef ConstantIfTrue,
519 LLVMValueRef ConstantIfFalse) {
520 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
521 unwrap<Constant>(ConstantIfTrue),
522 unwrap<Constant>(ConstantIfFalse)));
525 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
526 LLVMValueRef IndexConstant) {
527 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
528 unwrap<Constant>(IndexConstant)));
531 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
532 LLVMValueRef ElementValueConstant,
533 LLVMValueRef IndexConstant) {
534 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
535 unwrap<Constant>(ElementValueConstant),
536 unwrap<Constant>(IndexConstant)));
539 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
540 LLVMValueRef VectorBConstant,
541 LLVMValueRef MaskConstant) {
542 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
543 unwrap<Constant>(VectorBConstant),
544 unwrap<Constant>(MaskConstant)));
547 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
549 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
550 return wrap(unwrap<GlobalValue>(Global)->getParent());
553 int LLVMIsDeclaration(LLVMValueRef Global) {
554 return unwrap<GlobalValue>(Global)->isDeclaration();
557 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
558 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
561 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
562 unwrap<GlobalValue>(Global)
563 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
566 const char *LLVMGetSection(LLVMValueRef Global) {
567 return unwrap<GlobalValue>(Global)->getSection().c_str();
570 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
571 unwrap<GlobalValue>(Global)->setSection(Section);
574 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
575 return static_cast<LLVMVisibility>(
576 unwrap<GlobalValue>(Global)->getVisibility());
579 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
580 unwrap<GlobalValue>(Global)
581 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
584 unsigned LLVMGetAlignment(LLVMValueRef Global) {
585 return unwrap<GlobalValue>(Global)->getAlignment();
588 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
589 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
592 /*--.. Operations on global variables ......................................--*/
594 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
595 return wrap(new GlobalVariable(unwrap(Ty), false,
596 GlobalValue::ExternalLinkage, 0, Name,
600 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
601 return wrap(unwrap(M)->getNamedGlobal(Name));
604 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
605 Module *Mod = unwrap(M);
606 Module::global_iterator I = Mod->global_begin();
607 if (I == Mod->global_end())
612 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
613 Module *Mod = unwrap(M);
614 Module::global_iterator I = Mod->global_end();
615 if (I == Mod->global_begin())
620 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
621 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
622 Module::global_iterator I = GV;
623 if (++I == GV->getParent()->global_end())
628 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
629 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
630 Module::global_iterator I = GV;
631 if (I == GV->getParent()->global_begin())
636 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
637 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
640 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
641 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
644 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
645 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
648 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
649 unwrap<GlobalVariable>(GlobalVar)
650 ->setInitializer(unwrap<Constant>(ConstantVal));
653 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
654 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
657 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
658 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
661 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
662 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
665 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
666 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
669 /*--.. Operations on functions .............................................--*/
671 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
672 LLVMTypeRef FunctionTy) {
673 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
674 GlobalValue::ExternalLinkage, Name, unwrap(M)));
677 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
678 return wrap(unwrap(M)->getFunction(Name));
681 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
682 Module *Mod = unwrap(M);
683 Module::iterator I = Mod->begin();
689 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
690 Module *Mod = unwrap(M);
691 Module::iterator I = Mod->end();
692 if (I == Mod->begin())
697 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
698 Function *Func = unwrap<Function>(Fn);
699 Module::iterator I = Func;
700 if (++I == Func->getParent()->end())
705 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
706 Function *Func = unwrap<Function>(Fn);
707 Module::iterator I = Func;
708 if (I == Func->getParent()->begin())
713 void LLVMDeleteFunction(LLVMValueRef Fn) {
714 unwrap<Function>(Fn)->eraseFromParent();
717 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
718 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
719 return F->getIntrinsicID();
723 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
724 return unwrap<Function>(Fn)->getCallingConv();
727 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
728 return unwrap<Function>(Fn)->setCallingConv(CC);
731 const char *LLVMGetCollector(LLVMValueRef Fn) {
732 Function *F = unwrap<Function>(Fn);
733 return F->hasCollector()? F->getCollector() : 0;
736 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
737 Function *F = unwrap<Function>(Fn);
739 F->setCollector(Coll);
744 /*--.. Operations on parameters ............................................--*/
746 unsigned LLVMCountParams(LLVMValueRef FnRef) {
747 // This function is strictly redundant to
748 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
749 return unwrap<Function>(FnRef)->getArgumentList().size();
752 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
753 Function *Fn = unwrap<Function>(FnRef);
754 for (Function::arg_iterator I = Fn->arg_begin(),
755 E = Fn->arg_end(); I != E; I++)
756 *ParamRefs++ = wrap(I);
759 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
760 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
766 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
767 return wrap(unwrap<Argument>(V)->getParent());
770 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
771 Function *Func = unwrap<Function>(Fn);
772 Function::arg_iterator I = Func->arg_begin();
773 if (I == Func->arg_end())
778 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
779 Function *Func = unwrap<Function>(Fn);
780 Function::arg_iterator I = Func->arg_end();
781 if (I == Func->arg_begin())
786 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
787 Argument *A = unwrap<Argument>(Arg);
788 Function::arg_iterator I = A;
789 if (++I == A->getParent()->arg_end())
794 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
795 Argument *A = unwrap<Argument>(Arg);
796 Function::arg_iterator I = A;
797 if (I == A->getParent()->arg_begin())
802 /*--.. Operations on basic blocks ..........................................--*/
804 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
805 return wrap(static_cast<Value*>(unwrap(BB)));
808 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
809 return isa<BasicBlock>(unwrap(Val));
812 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
813 return wrap(unwrap<BasicBlock>(Val));
816 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
817 return wrap(unwrap(BB)->getParent());
820 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
821 return unwrap<Function>(FnRef)->getBasicBlockList().size();
824 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
825 Function *Fn = unwrap<Function>(FnRef);
826 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
827 *BasicBlocksRefs++ = wrap(I);
830 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
831 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
834 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
835 Function *Func = unwrap<Function>(Fn);
836 Function::iterator I = Func->begin();
837 if (I == Func->end())
842 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
843 Function *Func = unwrap<Function>(Fn);
844 Function::iterator I = Func->end();
845 if (I == Func->begin())
850 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
851 BasicBlock *Block = unwrap(BB);
852 Function::iterator I = Block;
853 if (++I == Block->getParent()->end())
858 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
859 BasicBlock *Block = unwrap(BB);
860 Function::iterator I = Block;
861 if (I == Block->getParent()->begin())
866 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
867 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
870 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
872 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
873 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
877 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
878 unwrap(BBRef)->eraseFromParent();
881 /*--.. Operations on instructions ..........................................--*/
883 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
884 return wrap(unwrap<Instruction>(Inst)->getParent());
887 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
888 BasicBlock *Block = unwrap(BB);
889 BasicBlock::iterator I = Block->begin();
890 if (I == Block->end())
895 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
896 BasicBlock *Block = unwrap(BB);
897 BasicBlock::iterator I = Block->end();
898 if (I == Block->begin())
903 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
904 Instruction *Instr = unwrap<Instruction>(Inst);
905 BasicBlock::iterator I = Instr;
906 if (++I == Instr->getParent()->end())
911 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
912 Instruction *Instr = unwrap<Instruction>(Inst);
913 BasicBlock::iterator I = Instr;
914 if (I == Instr->getParent()->begin())
919 /*--.. Call and invoke instructions ........................................--*/
921 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
922 Value *V = unwrap(Instr);
923 if (CallInst *CI = dyn_cast<CallInst>(V))
924 return CI->getCallingConv();
925 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
926 return II->getCallingConv();
927 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
931 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
932 Value *V = unwrap(Instr);
933 if (CallInst *CI = dyn_cast<CallInst>(V))
934 return CI->setCallingConv(CC);
935 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
936 return II->setCallingConv(CC);
937 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
940 /*--.. Operations on phi nodes .............................................--*/
942 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
943 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
944 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
945 for (unsigned I = 0; I != Count; ++I)
946 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
949 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
950 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
953 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
954 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
957 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
958 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
962 /*===-- Instruction builders ----------------------------------------------===*/
964 LLVMBuilderRef LLVMCreateBuilder() {
965 return wrap(new LLVMFoldingBuilder());
968 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
969 LLVMValueRef Instr) {
970 BasicBlock *BB = unwrap(Block);
971 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
972 unwrap(Builder)->SetInsertPoint(BB, I);
975 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
976 Instruction *I = unwrap<Instruction>(Instr);
977 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
980 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
981 BasicBlock *BB = unwrap(Block);
982 unwrap(Builder)->SetInsertPoint(BB);
985 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
986 return wrap(unwrap(Builder)->GetInsertBlock());
989 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
990 delete unwrap(Builder);
993 /*--.. Instruction builders ................................................--*/
995 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
996 return wrap(unwrap(B)->CreateRetVoid());
999 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1000 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1003 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1004 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1007 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1008 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1009 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1012 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1013 LLVMBasicBlockRef Else, unsigned NumCases) {
1014 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1017 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1018 LLVMValueRef *Args, unsigned NumArgs,
1019 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1021 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1022 unwrap(Args), unwrap(Args) + NumArgs,
1026 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1027 return wrap(unwrap(B)->CreateUnwind());
1030 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1031 return wrap(unwrap(B)->CreateUnreachable());
1034 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1035 LLVMBasicBlockRef Dest) {
1036 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1039 /*--.. Arithmetic ..........................................................--*/
1041 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1043 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1046 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1048 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1051 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1053 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1056 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1058 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1061 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1063 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1066 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1068 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1071 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1073 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1076 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1078 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1081 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1083 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1086 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1088 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1091 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1093 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1096 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1098 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1101 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1103 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1106 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1108 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1111 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1113 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1116 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1117 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1120 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1121 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1124 /*--.. Memory ..............................................................--*/
1126 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1128 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1131 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1132 LLVMValueRef Val, const char *Name) {
1133 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1136 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1138 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1141 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1142 LLVMValueRef Val, const char *Name) {
1143 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1146 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1147 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1151 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1153 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1156 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1157 LLVMValueRef PointerVal) {
1158 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1161 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1162 LLVMValueRef *Indices, unsigned NumIndices,
1164 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1165 unwrap(Indices) + NumIndices, Name));
1168 /*--.. Casts ...............................................................--*/
1170 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1171 LLVMTypeRef DestTy, const char *Name) {
1172 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1175 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1176 LLVMTypeRef DestTy, const char *Name) {
1177 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1180 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1181 LLVMTypeRef DestTy, const char *Name) {
1182 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1185 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1186 LLVMTypeRef DestTy, const char *Name) {
1187 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1190 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1191 LLVMTypeRef DestTy, const char *Name) {
1192 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1195 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1196 LLVMTypeRef DestTy, const char *Name) {
1197 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1200 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1201 LLVMTypeRef DestTy, const char *Name) {
1202 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1205 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1206 LLVMTypeRef DestTy, const char *Name) {
1207 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1210 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1211 LLVMTypeRef DestTy, const char *Name) {
1212 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1215 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1216 LLVMTypeRef DestTy, const char *Name) {
1217 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1220 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1221 LLVMTypeRef DestTy, const char *Name) {
1222 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1225 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1226 LLVMTypeRef DestTy, const char *Name) {
1227 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1230 /*--.. Comparisons .........................................................--*/
1232 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1233 LLVMValueRef LHS, LLVMValueRef RHS,
1235 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1236 unwrap(LHS), unwrap(RHS), Name));
1239 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1240 LLVMValueRef LHS, LLVMValueRef RHS,
1242 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1243 unwrap(LHS), unwrap(RHS), Name));
1246 /*--.. Miscellaneous instructions ..........................................--*/
1248 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1249 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1252 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1253 LLVMValueRef *Args, unsigned NumArgs,
1255 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1256 unwrap(Args) + NumArgs, Name));
1259 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1260 LLVMValueRef Then, LLVMValueRef Else,
1262 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1266 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1267 LLVMTypeRef Ty, const char *Name) {
1268 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1271 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1272 LLVMValueRef Index, const char *Name) {
1273 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1277 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1278 LLVMValueRef EltVal, LLVMValueRef Index,
1280 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1281 unwrap(Index), Name));
1284 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1285 LLVMValueRef V2, LLVMValueRef Mask,
1287 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1288 unwrap(Mask), Name));
1292 /*===-- Module providers --------------------------------------------------===*/
1294 LLVMModuleProviderRef
1295 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1296 return wrap(new ExistingModuleProvider(unwrap(M)));
1299 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1304 /*===-- Memory buffers ----------------------------------------------------===*/
1306 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1307 LLVMMemoryBufferRef *OutMemBuf,
1308 char **OutMessage) {
1310 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1311 *OutMemBuf = wrap(MB);
1315 *OutMessage = strdup(Error.c_str());
1319 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1320 char **OutMessage) {
1321 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1322 *OutMemBuf = wrap(MB);
1326 *OutMessage = strdup("stdin is empty.");
1330 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1331 delete unwrap(MemBuf);