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"
23 #include "llvm/Support/CallSite.h"
31 /*===-- Error handling ----------------------------------------------------===*/
33 void LLVMDisposeMessage(char *Message) {
38 /*===-- Operations on modules ---------------------------------------------===*/
40 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
41 return wrap(new Module(ModuleID));
44 void LLVMDisposeModule(LLVMModuleRef M) {
48 /*--.. Data layout .........................................................--*/
49 const char * LLVMGetDataLayout(LLVMModuleRef M) {
50 return unwrap(M)->getDataLayout().c_str();
53 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
54 unwrap(M)->setDataLayout(Triple);
57 /*--.. Target triple .......................................................--*/
58 const char * LLVMGetTarget(LLVMModuleRef M) {
59 return unwrap(M)->getTargetTriple().c_str();
62 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
63 unwrap(M)->setTargetTriple(Triple);
66 /*--.. Type names ..........................................................--*/
67 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
68 return unwrap(M)->addTypeName(Name, unwrap(Ty));
71 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
74 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
75 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
80 void LLVMDumpModule(LLVMModuleRef M) {
85 /*===-- Operations on types -----------------------------------------------===*/
87 /*--.. Operations on all types (mostly) ....................................--*/
89 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
90 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
93 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
94 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
95 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
98 /*--.. Operations on integer types .........................................--*/
100 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
101 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
102 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
103 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
104 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
106 LLVMTypeRef LLVMIntType(unsigned NumBits) {
107 return wrap(IntegerType::get(NumBits));
110 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
111 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
114 /*--.. Operations on real types ............................................--*/
116 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
117 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
118 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
119 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
120 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
122 /*--.. Operations on function types ........................................--*/
124 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
125 LLVMTypeRef *ParamTypes, unsigned ParamCount,
127 std::vector<const Type*> Tys;
128 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
129 Tys.push_back(unwrap(*I));
131 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
134 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
135 return unwrap<FunctionType>(FunctionTy)->isVarArg();
138 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
139 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
142 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
143 return unwrap<FunctionType>(FunctionTy)->getNumParams();
146 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
147 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
148 for (FunctionType::param_iterator I = Ty->param_begin(),
149 E = Ty->param_end(); I != E; ++I)
153 /*--.. Operations on struct types ..........................................--*/
155 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
156 unsigned ElementCount, int Packed) {
157 std::vector<const Type*> Tys;
158 for (LLVMTypeRef *I = ElementTypes,
159 *E = ElementTypes + ElementCount; I != E; ++I)
160 Tys.push_back(unwrap(*I));
162 return wrap(StructType::get(Tys, Packed != 0));
165 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
166 return unwrap<StructType>(StructTy)->getNumElements();
169 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
170 StructType *Ty = unwrap<StructType>(StructTy);
171 for (FunctionType::param_iterator I = Ty->element_begin(),
172 E = Ty->element_end(); I != E; ++I)
176 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
177 return unwrap<StructType>(StructTy)->isPacked();
180 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
182 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
183 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
186 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
187 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
190 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
191 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
194 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
195 return wrap(unwrap<SequentialType>(Ty)->getElementType());
198 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
199 return unwrap<ArrayType>(ArrayTy)->getNumElements();
202 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
203 return unwrap<PointerType>(PointerTy)->getAddressSpace();
206 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
207 return unwrap<VectorType>(VectorTy)->getNumElements();
210 /*--.. Operations on other types ...........................................--*/
212 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
213 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
215 LLVMTypeRef LLVMOpaqueType(void) {
216 return wrap(llvm::OpaqueType::get());
219 /*--.. Operations on type handles ..........................................--*/
221 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
222 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
225 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
226 delete unwrap(TypeHandle);
229 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
230 return wrap(unwrap(TypeHandle)->get());
233 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
234 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
238 /*===-- Operations on values ----------------------------------------------===*/
240 /*--.. Operations on all values ............................................--*/
242 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
243 return wrap(unwrap(Val)->getType());
246 const char *LLVMGetValueName(LLVMValueRef Val) {
247 return unwrap(Val)->getNameStart();
250 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
251 unwrap(Val)->setName(Name);
254 void LLVMDumpValue(LLVMValueRef Val) {
258 /*--.. Operations on constants of any type .................................--*/
260 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
261 return wrap(Constant::getNullValue(unwrap(Ty)));
264 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
265 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
268 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
269 return wrap(UndefValue::get(unwrap(Ty)));
272 int LLVMIsConstant(LLVMValueRef Ty) {
273 return isa<Constant>(unwrap(Ty));
276 int LLVMIsNull(LLVMValueRef Val) {
277 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
278 return C->isNullValue();
282 int LLVMIsUndef(LLVMValueRef Val) {
283 return isa<UndefValue>(unwrap(Val));
286 /*--.. Operations on scalar constants ......................................--*/
288 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
290 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
293 static const fltSemantics &SemanticsForType(Type *Ty) {
294 assert(Ty->isFloatingPoint() && "Type is not floating point!");
295 if (Ty == Type::FloatTy)
296 return APFloat::IEEEsingle;
297 if (Ty == Type::DoubleTy)
298 return APFloat::IEEEdouble;
299 if (Ty == Type::X86_FP80Ty)
300 return APFloat::x87DoubleExtended;
301 if (Ty == Type::FP128Ty)
302 return APFloat::IEEEquad;
303 if (Ty == Type::PPC_FP128Ty)
304 return APFloat::PPCDoubleDouble;
305 return APFloat::Bogus;
308 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
310 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
311 return wrap(ConstantFP::get(APN));
314 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
315 return wrap(ConstantFP::get(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(Function::Create(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 void LLVMAddParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
803 unwrap<Argument>(Arg)->addAttr(PA);
806 void LLVMRemoveParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
807 unwrap<Argument>(Arg)->removeAttr(PA);
810 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
811 unwrap<Argument>(Arg)->addAttr(
812 ParamAttr::constructAlignmentFromInt(align));
815 /*--.. Operations on basic blocks ..........................................--*/
817 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
818 return wrap(static_cast<Value*>(unwrap(BB)));
821 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
822 return isa<BasicBlock>(unwrap(Val));
825 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
826 return wrap(unwrap<BasicBlock>(Val));
829 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
830 return wrap(unwrap(BB)->getParent());
833 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
834 return unwrap<Function>(FnRef)->getBasicBlockList().size();
837 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
838 Function *Fn = unwrap<Function>(FnRef);
839 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
840 *BasicBlocksRefs++ = wrap(I);
843 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
844 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
847 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
848 Function *Func = unwrap<Function>(Fn);
849 Function::iterator I = Func->begin();
850 if (I == Func->end())
855 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
856 Function *Func = unwrap<Function>(Fn);
857 Function::iterator I = Func->end();
858 if (I == Func->begin())
863 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
864 BasicBlock *Block = unwrap(BB);
865 Function::iterator I = Block;
866 if (++I == Block->getParent()->end())
871 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
872 BasicBlock *Block = unwrap(BB);
873 Function::iterator I = Block;
874 if (I == Block->getParent()->begin())
879 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
880 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
883 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
885 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
886 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
890 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
891 unwrap(BBRef)->eraseFromParent();
894 /*--.. Operations on instructions ..........................................--*/
896 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
897 return wrap(unwrap<Instruction>(Inst)->getParent());
900 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
901 BasicBlock *Block = unwrap(BB);
902 BasicBlock::iterator I = Block->begin();
903 if (I == Block->end())
908 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
909 BasicBlock *Block = unwrap(BB);
910 BasicBlock::iterator I = Block->end();
911 if (I == Block->begin())
916 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
917 Instruction *Instr = unwrap<Instruction>(Inst);
918 BasicBlock::iterator I = Instr;
919 if (++I == Instr->getParent()->end())
924 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
925 Instruction *Instr = unwrap<Instruction>(Inst);
926 BasicBlock::iterator I = Instr;
927 if (I == Instr->getParent()->begin())
932 /*--.. Call and invoke instructions ........................................--*/
934 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
935 Value *V = unwrap(Instr);
936 if (CallInst *CI = dyn_cast<CallInst>(V))
937 return CI->getCallingConv();
938 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
939 return II->getCallingConv();
940 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
944 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
945 Value *V = unwrap(Instr);
946 if (CallInst *CI = dyn_cast<CallInst>(V))
947 return CI->setCallingConv(CC);
948 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
949 return II->setCallingConv(CC);
950 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
953 void LLVMAddInstrParamAttr(LLVMValueRef Instr, unsigned index,
955 CallSite Call = CallSite(unwrap<Instruction>(Instr));
957 Call.getParamAttrs().addAttr(index, PA));
960 void LLVMRemoveInstrParamAttr(LLVMValueRef Instr, unsigned index,
962 CallSite Call = CallSite(unwrap<Instruction>(Instr));
964 Call.getParamAttrs().removeAttr(index, PA));
967 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
969 CallSite Call = CallSite(unwrap<Instruction>(Instr));
971 Call.getParamAttrs().addAttr(index,
972 ParamAttr::constructAlignmentFromInt(align)));
975 /*--.. Operations on phi nodes .............................................--*/
977 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
978 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
979 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
980 for (unsigned I = 0; I != Count; ++I)
981 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
984 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
985 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
988 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
989 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
992 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
993 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
997 /*===-- Instruction builders ----------------------------------------------===*/
999 LLVMBuilderRef LLVMCreateBuilder(void) {
1000 return wrap(new IRBuilder());
1003 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1004 LLVMValueRef Instr) {
1005 BasicBlock *BB = unwrap(Block);
1006 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1007 unwrap(Builder)->SetInsertPoint(BB, I);
1010 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1011 Instruction *I = unwrap<Instruction>(Instr);
1012 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1015 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1016 BasicBlock *BB = unwrap(Block);
1017 unwrap(Builder)->SetInsertPoint(BB);
1020 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1021 return wrap(unwrap(Builder)->GetInsertBlock());
1024 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1025 delete unwrap(Builder);
1028 /*--.. Instruction builders ................................................--*/
1030 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1031 return wrap(unwrap(B)->CreateRetVoid());
1034 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1035 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1038 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1039 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1042 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1043 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1044 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1047 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1048 LLVMBasicBlockRef Else, unsigned NumCases) {
1049 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1052 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1053 LLVMValueRef *Args, unsigned NumArgs,
1054 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1056 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1057 unwrap(Args), unwrap(Args) + NumArgs,
1061 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1062 return wrap(unwrap(B)->CreateUnwind());
1065 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1066 return wrap(unwrap(B)->CreateUnreachable());
1069 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1070 LLVMBasicBlockRef Dest) {
1071 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1074 /*--.. Arithmetic ..........................................................--*/
1076 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1078 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1081 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1083 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1086 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1088 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1091 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1093 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1096 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1098 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1101 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1103 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1106 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1108 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1111 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1113 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1116 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1118 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1121 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1123 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1126 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1128 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1131 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1133 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1136 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1138 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1141 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1143 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1146 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1148 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1151 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1152 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1155 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1156 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1159 /*--.. Memory ..............................................................--*/
1161 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1163 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1166 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1167 LLVMValueRef Val, const char *Name) {
1168 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1171 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1173 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1176 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1177 LLVMValueRef Val, const char *Name) {
1178 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1181 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1182 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1186 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1188 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1191 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1192 LLVMValueRef PointerVal) {
1193 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1196 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1197 LLVMValueRef *Indices, unsigned NumIndices,
1199 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1200 unwrap(Indices) + NumIndices, Name));
1203 /*--.. Casts ...............................................................--*/
1205 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1206 LLVMTypeRef DestTy, const char *Name) {
1207 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1210 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1211 LLVMTypeRef DestTy, const char *Name) {
1212 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1215 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1216 LLVMTypeRef DestTy, const char *Name) {
1217 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1220 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1221 LLVMTypeRef DestTy, const char *Name) {
1222 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1225 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1226 LLVMTypeRef DestTy, const char *Name) {
1227 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1230 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1231 LLVMTypeRef DestTy, const char *Name) {
1232 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1235 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1236 LLVMTypeRef DestTy, const char *Name) {
1237 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1240 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1241 LLVMTypeRef DestTy, const char *Name) {
1242 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1245 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1246 LLVMTypeRef DestTy, const char *Name) {
1247 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1250 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1251 LLVMTypeRef DestTy, const char *Name) {
1252 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1255 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1256 LLVMTypeRef DestTy, const char *Name) {
1257 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1260 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1261 LLVMTypeRef DestTy, const char *Name) {
1262 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1265 /*--.. Comparisons .........................................................--*/
1267 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1268 LLVMValueRef LHS, LLVMValueRef RHS,
1270 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1271 unwrap(LHS), unwrap(RHS), Name));
1274 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1275 LLVMValueRef LHS, LLVMValueRef RHS,
1277 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1278 unwrap(LHS), unwrap(RHS), Name));
1281 /*--.. Miscellaneous instructions ..........................................--*/
1283 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1284 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1287 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1288 LLVMValueRef *Args, unsigned NumArgs,
1290 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1291 unwrap(Args) + NumArgs, Name));
1294 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1295 LLVMValueRef Then, LLVMValueRef Else,
1297 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1301 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1302 LLVMTypeRef Ty, const char *Name) {
1303 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1306 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1307 LLVMValueRef Index, const char *Name) {
1308 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1312 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1313 LLVMValueRef EltVal, LLVMValueRef Index,
1315 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1316 unwrap(Index), Name));
1319 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1320 LLVMValueRef V2, LLVMValueRef Mask,
1322 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1323 unwrap(Mask), Name));
1327 /*===-- Module providers --------------------------------------------------===*/
1329 LLVMModuleProviderRef
1330 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1331 return wrap(new ExistingModuleProvider(unwrap(M)));
1334 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1339 /*===-- Memory buffers ----------------------------------------------------===*/
1341 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1342 LLVMMemoryBufferRef *OutMemBuf,
1343 char **OutMessage) {
1345 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1346 *OutMemBuf = wrap(MB);
1350 *OutMessage = strdup(Error.c_str());
1354 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1355 char **OutMessage) {
1356 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1357 *OutMemBuf = wrap(MB);
1361 *OutMessage = strdup("stdin is empty.");
1365 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1366 delete unwrap(MemBuf);