LLVM API Documentation

Core.cpp

Go to the documentation of this file.
00001 //===-- Core.cpp ----------------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the C bindings for libLLVMCore.a, which implements
00011 // the LLVM intermediate representation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm-c/Core.h"
00016 #include "llvm/Bitcode/ReaderWriter.h"
00017 #include "llvm/Constants.h"
00018 #include "llvm/DerivedTypes.h"
00019 #include "llvm/GlobalVariable.h"
00020 #include "llvm/GlobalAlias.h"
00021 #include "llvm/TypeSymbolTable.h"
00022 #include "llvm/ModuleProvider.h"
00023 #include "llvm/InlineAsm.h"
00024 #include "llvm/IntrinsicInst.h"
00025 #include "llvm/Support/MemoryBuffer.h"
00026 #include "llvm/Support/CallSite.h"
00027 #include <cassert>
00028 #include <cstdlib>
00029 #include <cstring>
00030 
00031 using namespace llvm;
00032 
00033 
00034 /*===-- Error handling ----------------------------------------------------===*/
00035 
00036 void LLVMDisposeMessage(char *Message) {
00037   free(Message);
00038 }
00039 
00040 
00041 /*===-- Operations on modules ---------------------------------------------===*/
00042 
00043 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
00044   return wrap(new Module(ModuleID));
00045 }
00046 
00047 void LLVMDisposeModule(LLVMModuleRef M) {
00048   delete unwrap(M);
00049 }
00050 
00051 /*--.. Data layout .........................................................--*/
00052 const char * LLVMGetDataLayout(LLVMModuleRef M) {
00053   return unwrap(M)->getDataLayout().c_str();
00054 }
00055 
00056 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
00057   unwrap(M)->setDataLayout(Triple);
00058 }
00059 
00060 /*--.. Target triple .......................................................--*/
00061 const char * LLVMGetTarget(LLVMModuleRef M) {
00062   return unwrap(M)->getTargetTriple().c_str();
00063 }
00064 
00065 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
00066   unwrap(M)->setTargetTriple(Triple);
00067 }
00068 
00069 /*--.. Type names ..........................................................--*/
00070 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
00071   return unwrap(M)->addTypeName(Name, unwrap(Ty));
00072 }
00073 
00074 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
00075   std::string N(Name);
00076   
00077   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
00078   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
00079     if (I->first == N)
00080       TST.remove(I);
00081 }
00082 
00083 void LLVMDumpModule(LLVMModuleRef M) {
00084   unwrap(M)->dump();
00085 }
00086 
00087 
00088 /*===-- Operations on types -----------------------------------------------===*/
00089 
00090 /*--.. Operations on all types (mostly) ....................................--*/
00091 
00092 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
00093   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
00094 }
00095 
00096 /*--.. Operations on integer types .........................................--*/
00097 
00098 LLVMTypeRef LLVMInt1Type(void)  { return (LLVMTypeRef) Type::Int1Ty;  }
00099 LLVMTypeRef LLVMInt8Type(void)  { return (LLVMTypeRef) Type::Int8Ty;  }
00100 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
00101 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
00102 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
00103 
00104 LLVMTypeRef LLVMIntType(unsigned NumBits) {
00105   return wrap(IntegerType::get(NumBits));
00106 }
00107 
00108 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
00109   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
00110 }
00111 
00112 /*--.. Operations on real types ............................................--*/
00113 
00114 LLVMTypeRef LLVMFloatType(void)    { return (LLVMTypeRef) Type::FloatTy;     }
00115 LLVMTypeRef LLVMDoubleType(void)   { return (LLVMTypeRef) Type::DoubleTy;    }
00116 LLVMTypeRef LLVMX86FP80Type(void)  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
00117 LLVMTypeRef LLVMFP128Type(void)    { return (LLVMTypeRef) Type::FP128Ty;     }
00118 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
00119 
00120 /*--.. Operations on function types ........................................--*/
00121 
00122 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
00123                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
00124                              int IsVarArg) {
00125   std::vector<const Type*> Tys;
00126   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
00127     Tys.push_back(unwrap(*I));
00128   
00129   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
00130 }
00131 
00132 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
00133   return unwrap<FunctionType>(FunctionTy)->isVarArg();
00134 }
00135 
00136 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
00137   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
00138 }
00139 
00140 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
00141   return unwrap<FunctionType>(FunctionTy)->getNumParams();
00142 }
00143 
00144 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
00145   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
00146   for (FunctionType::param_iterator I = Ty->param_begin(),
00147                                     E = Ty->param_end(); I != E; ++I)
00148     *Dest++ = wrap(*I);
00149 }
00150 
00151 /*--.. Operations on struct types ..........................................--*/
00152 
00153 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
00154                            unsigned ElementCount, int Packed) {
00155   std::vector<const Type*> Tys;
00156   for (LLVMTypeRef *I = ElementTypes,
00157                    *E = ElementTypes + ElementCount; I != E; ++I)
00158     Tys.push_back(unwrap(*I));
00159   
00160   return wrap(StructType::get(Tys, Packed != 0));
00161 }
00162 
00163 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
00164   return unwrap<StructType>(StructTy)->getNumElements();
00165 }
00166 
00167 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
00168   StructType *Ty = unwrap<StructType>(StructTy);
00169   for (FunctionType::param_iterator I = Ty->element_begin(),
00170                                     E = Ty->element_end(); I != E; ++I)
00171     *Dest++ = wrap(*I);
00172 }
00173 
00174 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
00175   return unwrap<StructType>(StructTy)->isPacked();
00176 }
00177 
00178 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
00179 
00180 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
00181   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
00182 }
00183 
00184 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
00185   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
00186 }
00187 
00188 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
00189   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
00190 }
00191 
00192 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
00193   return wrap(unwrap<SequentialType>(Ty)->getElementType());
00194 }
00195 
00196 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
00197   return unwrap<ArrayType>(ArrayTy)->getNumElements();
00198 }
00199 
00200 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
00201   return unwrap<PointerType>(PointerTy)->getAddressSpace();
00202 }
00203 
00204 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
00205   return unwrap<VectorType>(VectorTy)->getNumElements();
00206 }
00207 
00208 /*--.. Operations on other types ...........................................--*/
00209 
00210 LLVMTypeRef LLVMVoidType(void)  { return (LLVMTypeRef) Type::VoidTy;  }
00211 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
00212 
00213 LLVMTypeRef LLVMOpaqueType(void) {
00214   return wrap(llvm::OpaqueType::get());
00215 }
00216 
00217 /*--.. Operations on type handles ..........................................--*/
00218 
00219 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
00220   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
00221 }
00222 
00223 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
00224   delete unwrap(TypeHandle);
00225 }
00226 
00227 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
00228   return wrap(unwrap(TypeHandle)->get());
00229 }
00230 
00231 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
00232   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
00233 }
00234 
00235 
00236 /*===-- Operations on values ----------------------------------------------===*/
00237 
00238 /*--.. Operations on all values ............................................--*/
00239 
00240 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
00241   return wrap(unwrap(Val)->getType());
00242 }
00243 
00244 const char *LLVMGetValueName(LLVMValueRef Val) {
00245   return unwrap(Val)->getNameStart();
00246 }
00247 
00248 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
00249   unwrap(Val)->setName(Name);
00250 }
00251 
00252 void LLVMDumpValue(LLVMValueRef Val) {
00253   unwrap(Val)->dump();
00254 }
00255 
00256 
00257 /*--.. Conversion functions ................................................--*/
00258 
00259 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
00260   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
00261     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
00262   }
00263 
00264 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
00265 
00266 
00267 /*--.. Operations on constants of any type .................................--*/
00268 
00269 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
00270   return wrap(Constant::getNullValue(unwrap(Ty)));
00271 }
00272 
00273 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
00274   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
00275 }
00276 
00277 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
00278   return wrap(UndefValue::get(unwrap(Ty)));
00279 }
00280 
00281 int LLVMIsConstant(LLVMValueRef Ty) {
00282   return isa<Constant>(unwrap(Ty));
00283 }
00284 
00285 int LLVMIsNull(LLVMValueRef Val) {
00286   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
00287     return C->isNullValue();
00288   return false;
00289 }
00290 
00291 int LLVMIsUndef(LLVMValueRef Val) {
00292   return isa<UndefValue>(unwrap(Val));
00293 }
00294 
00295 /*--.. Operations on scalar constants ......................................--*/
00296 
00297 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
00298                           int SignExtend) {
00299   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
00300 }
00301 
00302 static const fltSemantics &SemanticsForType(Type *Ty) {
00303   assert(Ty->isFloatingPoint() && "Type is not floating point!");
00304   if (Ty == Type::FloatTy)
00305     return APFloat::IEEEsingle;
00306   if (Ty == Type::DoubleTy)
00307     return APFloat::IEEEdouble;
00308   if (Ty == Type::X86_FP80Ty)
00309     return APFloat::x87DoubleExtended;
00310   if (Ty == Type::FP128Ty)
00311     return APFloat::IEEEquad;
00312   if (Ty == Type::PPC_FP128Ty)
00313     return APFloat::PPCDoubleDouble;
00314   return APFloat::Bogus;
00315 }
00316 
00317 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
00318   APFloat APN(N);
00319   bool ignored;
00320   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
00321               &ignored);
00322   return wrap(ConstantFP::get(APN));
00323 }
00324 
00325 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
00326   return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
00327 }
00328 
00329 /*--.. Operations on composite constants ...................................--*/
00330 
00331 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
00332                              int DontNullTerminate) {
00333   /* Inverted the sense of AddNull because ', 0)' is a
00334      better mnemonic for null termination than ', 1)'. */
00335   return wrap(ConstantArray::get(std::string(Str, Length),
00336                                  DontNullTerminate == 0));
00337 }
00338 
00339 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
00340                             LLVMValueRef *ConstantVals, unsigned Length) {
00341   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
00342                                  unwrap<Constant>(ConstantVals, Length),
00343                                  Length));
00344 }
00345 
00346 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
00347                              int Packed) {
00348   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
00349                                   Count, Packed != 0));
00350 }
00351 
00352 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
00353   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
00354                                   Size));
00355 }
00356 
00357 /*--.. Constant expressions ................................................--*/
00358 
00359 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
00360   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
00361 }
00362 
00363 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
00364   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
00365 }
00366 
00367 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
00368   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
00369 }
00370 
00371 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00372   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
00373                                    unwrap<Constant>(RHSConstant)));
00374 }
00375 
00376 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00377   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
00378                                    unwrap<Constant>(RHSConstant)));
00379 }
00380 
00381 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00382   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
00383                                    unwrap<Constant>(RHSConstant)));
00384 }
00385 
00386 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00387   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
00388                                     unwrap<Constant>(RHSConstant)));
00389 }
00390 
00391 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00392   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
00393                                     unwrap<Constant>(RHSConstant)));
00394 }
00395 
00396 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00397   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
00398                                     unwrap<Constant>(RHSConstant)));
00399 }
00400 
00401 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00402   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
00403                                     unwrap<Constant>(RHSConstant)));
00404 }
00405 
00406 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00407   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
00408                                     unwrap<Constant>(RHSConstant)));
00409 }
00410 
00411 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00412   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
00413                                     unwrap<Constant>(RHSConstant)));
00414 }
00415 
00416 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00417   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
00418                                    unwrap<Constant>(RHSConstant)));
00419 }
00420 
00421 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00422   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
00423                                   unwrap<Constant>(RHSConstant)));
00424 }
00425 
00426 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00427   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
00428                                    unwrap<Constant>(RHSConstant)));
00429 }
00430 
00431 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
00432                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00433   return wrap(ConstantExpr::getICmp(Predicate,
00434                                     unwrap<Constant>(LHSConstant),
00435                                     unwrap<Constant>(RHSConstant)));
00436 }
00437 
00438 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
00439                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00440   return wrap(ConstantExpr::getFCmp(Predicate,
00441                                     unwrap<Constant>(LHSConstant),
00442                                     unwrap<Constant>(RHSConstant)));
00443 }
00444 
00445 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00446   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
00447                                   unwrap<Constant>(RHSConstant)));
00448 }
00449 
00450 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00451   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
00452                                     unwrap<Constant>(RHSConstant)));
00453 }
00454 
00455 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00456   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
00457                                     unwrap<Constant>(RHSConstant)));
00458 }
00459 
00460 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
00461                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
00462   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
00463                                              unwrap<Constant>(ConstantIndices, 
00464                                                               NumIndices),
00465                                              NumIndices));
00466 }
00467 
00468 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00469   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
00470                                      unwrap(ToType)));
00471 }
00472 
00473 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00474   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
00475                                     unwrap(ToType)));
00476 }
00477 
00478 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00479   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
00480                                     unwrap(ToType)));
00481 }
00482 
00483 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00484   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
00485                                        unwrap(ToType)));
00486 }
00487 
00488 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00489   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
00490                                         unwrap(ToType)));
00491 }
00492 
00493 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00494   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
00495                                       unwrap(ToType)));
00496 }
00497 
00498 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00499   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
00500                                       unwrap(ToType)));
00501 }
00502 
00503 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00504   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
00505                                       unwrap(ToType)));
00506 }
00507 
00508 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00509   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
00510                                       unwrap(ToType)));
00511 }
00512 
00513 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00514   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
00515                                         unwrap(ToType)));
00516 }
00517 
00518 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00519   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
00520                                         unwrap(ToType)));
00521 }
00522 
00523 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
00524   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
00525                                        unwrap(ToType)));
00526 }
00527 
00528 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
00529                              LLVMValueRef ConstantIfTrue,
00530                              LLVMValueRef ConstantIfFalse) {
00531   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
00532                                       unwrap<Constant>(ConstantIfTrue),
00533                                       unwrap<Constant>(ConstantIfFalse)));
00534 }
00535 
00536 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
00537                                      LLVMValueRef IndexConstant) {
00538   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
00539                                               unwrap<Constant>(IndexConstant)));
00540 }
00541 
00542 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
00543                                     LLVMValueRef ElementValueConstant,
00544                                     LLVMValueRef IndexConstant) {
00545   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
00546                                          unwrap<Constant>(ElementValueConstant),
00547                                              unwrap<Constant>(IndexConstant)));
00548 }
00549 
00550 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
00551                                     LLVMValueRef VectorBConstant,
00552                                     LLVMValueRef MaskConstant) {
00553   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
00554                                              unwrap<Constant>(VectorBConstant),
00555                                              unwrap<Constant>(MaskConstant)));
00556 }
00557 
00558 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
00559                                    unsigned NumIdx) {
00560   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
00561                                             IdxList, NumIdx));
00562 }
00563 
00564 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
00565                                   LLVMValueRef ElementValueConstant,
00566                                   unsigned *IdxList, unsigned NumIdx) {
00567   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
00568                                          unwrap<Constant>(ElementValueConstant),
00569                                            IdxList, NumIdx));
00570 }
00571 
00572 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
00573                                 const char *Constraints, int HasSideEffects) {
00574   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
00575                              Constraints, HasSideEffects));
00576 }
00577 
00578 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
00579 
00580 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
00581   return wrap(unwrap<GlobalValue>(Global)->getParent());
00582 }
00583 
00584 int LLVMIsDeclaration(LLVMValueRef Global) {
00585   return unwrap<GlobalValue>(Global)->isDeclaration();
00586 }
00587 
00588 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
00589   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
00590 }
00591 
00592 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
00593   unwrap<GlobalValue>(Global)
00594     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
00595 }
00596 
00597 const char *LLVMGetSection(LLVMValueRef Global) {
00598   return unwrap<GlobalValue>(Global)->getSection().c_str();
00599 }
00600 
00601 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
00602   unwrap<GlobalValue>(Global)->setSection(Section);
00603 }
00604 
00605 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
00606   return static_cast<LLVMVisibility>(
00607     unwrap<GlobalValue>(Global)->getVisibility());
00608 }
00609 
00610 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
00611   unwrap<GlobalValue>(Global)
00612     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
00613 }
00614 
00615 unsigned LLVMGetAlignment(LLVMValueRef Global) {
00616   return unwrap<GlobalValue>(Global)->getAlignment();
00617 }
00618 
00619 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
00620   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
00621 }
00622 
00623 /*--.. Operations on global variables ......................................--*/
00624 
00625 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
00626   return wrap(new GlobalVariable(unwrap(Ty), false,
00627                                  GlobalValue::ExternalLinkage, 0, Name,
00628                                  unwrap(M)));
00629 }
00630 
00631 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
00632   return wrap(unwrap(M)->getNamedGlobal(Name));
00633 }
00634 
00635 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
00636   Module *Mod = unwrap(M);
00637   Module::global_iterator I = Mod->global_begin();
00638   if (I == Mod->global_end())
00639     return 0;
00640   return wrap(I);
00641 }
00642 
00643 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
00644   Module *Mod = unwrap(M);
00645   Module::global_iterator I = Mod->global_end();
00646   if (I == Mod->global_begin())
00647     return 0;
00648   return wrap(--I);
00649 }
00650 
00651 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
00652   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
00653   Module::global_iterator I = GV;
00654   if (++I == GV->getParent()->global_end())
00655     return 0;
00656   return wrap(I);
00657 }
00658 
00659 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
00660   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
00661   Module::global_iterator I = GV;
00662   if (I == GV->getParent()->global_begin())
00663     return 0;
00664   return wrap(--I);
00665 }
00666 
00667 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
00668   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
00669 }
00670 
00671 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
00672   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
00673 }
00674 
00675 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
00676   unwrap<GlobalVariable>(GlobalVar)
00677     ->setInitializer(unwrap<Constant>(ConstantVal));
00678 }
00679 
00680 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
00681   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
00682 }
00683 
00684 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
00685   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
00686 }
00687 
00688 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
00689   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
00690 }
00691 
00692 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
00693   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
00694 }
00695 
00696 /*--.. Operations on aliases ......................................--*/
00697 
00698 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
00699                           const char *Name) {
00700   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
00701                               unwrap<Constant>(Aliasee), unwrap (M)));
00702 }
00703 
00704 /*--.. Operations on functions .............................................--*/
00705 
00706 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
00707                              LLVMTypeRef FunctionTy) {
00708   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
00709                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
00710 }
00711 
00712 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
00713   return wrap(unwrap(M)->getFunction(Name));
00714 }
00715 
00716 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
00717   Module *Mod = unwrap(M);
00718   Module::iterator I = Mod->begin();
00719   if (I == Mod->end())
00720     return 0;
00721   return wrap(I);
00722 }
00723 
00724 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
00725   Module *Mod = unwrap(M);
00726   Module::iterator I = Mod->end();
00727   if (I == Mod->begin())
00728     return 0;
00729   return wrap(--I);
00730 }
00731 
00732 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
00733   Function *Func = unwrap<Function>(Fn);
00734   Module::iterator I = Func;
00735   if (++I == Func->getParent()->end())
00736     return 0;
00737   return wrap(I);
00738 }
00739 
00740 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
00741   Function *Func = unwrap<Function>(Fn);
00742   Module::iterator I = Func;
00743   if (I == Func->getParent()->begin())
00744     return 0;
00745   return wrap(--I);
00746 }
00747 
00748 void LLVMDeleteFunction(LLVMValueRef Fn) {
00749   unwrap<Function>(Fn)->eraseFromParent();
00750 }
00751 
00752 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
00753   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
00754     return F->getIntrinsicID();
00755   return 0;
00756 }
00757 
00758 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
00759   return unwrap<Function>(Fn)->getCallingConv();
00760 }
00761 
00762 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
00763   return unwrap<Function>(Fn)->setCallingConv(CC);
00764 }
00765 
00766 const char *LLVMGetGC(LLVMValueRef Fn) {
00767   Function *F = unwrap<Function>(Fn);
00768   return F->hasGC()? F->getGC() : 0;
00769 }
00770 
00771 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
00772   Function *F = unwrap<Function>(Fn);
00773   if (GC)
00774     F->setGC(GC);
00775   else
00776     F->clearGC();
00777 }
00778 
00779 /*--.. Operations on parameters ............................................--*/
00780 
00781 unsigned LLVMCountParams(LLVMValueRef FnRef) {
00782   // This function is strictly redundant to
00783   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
00784   return unwrap<Function>(FnRef)->arg_size();
00785 }
00786 
00787 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
00788   Function *Fn = unwrap<Function>(FnRef);
00789   for (Function::arg_iterator I = Fn->arg_begin(),
00790                               E = Fn->arg_end(); I != E; I++)
00791     *ParamRefs++ = wrap(I);
00792 }
00793 
00794 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
00795   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
00796   while (index --> 0)
00797     AI++;
00798   return wrap(AI);
00799 }
00800 
00801 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
00802   return wrap(unwrap<Argument>(V)->getParent());
00803 }
00804 
00805 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
00806   Function *Func = unwrap<Function>(Fn);
00807   Function::arg_iterator I = Func->arg_begin();
00808   if (I == Func->arg_end())
00809     return 0;
00810   return wrap(I);
00811 }
00812 
00813 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
00814   Function *Func = unwrap<Function>(Fn);
00815   Function::arg_iterator I = Func->arg_end();
00816   if (I == Func->arg_begin())
00817     return 0;
00818   return wrap(--I);
00819 }
00820 
00821 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
00822   Argument *A = unwrap<Argument>(Arg);
00823   Function::arg_iterator I = A;
00824   if (++I == A->getParent()->arg_end())
00825     return 0;
00826   return wrap(I);
00827 }
00828 
00829 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
00830   Argument *A = unwrap<Argument>(Arg);
00831   Function::arg_iterator I = A;
00832   if (I == A->getParent()->arg_begin())
00833     return 0;
00834   return wrap(--I);
00835 }
00836 
00837 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
00838   unwrap<Argument>(Arg)->addAttr(PA);
00839 }
00840 
00841 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
00842   unwrap<Argument>(Arg)->removeAttr(PA);
00843 }
00844 
00845 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
00846   unwrap<Argument>(Arg)->addAttr(
00847           Attribute::constructAlignmentFromInt(align));
00848 }
00849 
00850 /*--.. Operations on basic blocks ..........................................--*/
00851 
00852 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
00853   return wrap(static_cast<Value*>(unwrap(BB)));
00854 }
00855 
00856 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
00857   return isa<BasicBlock>(unwrap(Val));
00858 }
00859 
00860 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
00861   return wrap(unwrap<BasicBlock>(Val));
00862 }
00863 
00864 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
00865   return wrap(unwrap(BB)->getParent());
00866 }
00867 
00868 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
00869   return unwrap<Function>(FnRef)->size();
00870 }
00871 
00872 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
00873   Function *Fn = unwrap<Function>(FnRef);
00874   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
00875     *BasicBlocksRefs++ = wrap(I);
00876 }
00877 
00878 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
00879   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
00880 }
00881 
00882 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
00883   Function *Func = unwrap<Function>(Fn);
00884   Function::iterator I = Func->begin();
00885   if (I == Func->end())
00886     return 0;
00887   return wrap(I);
00888 }
00889 
00890 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
00891   Function *Func = unwrap<Function>(Fn);
00892   Function::iterator I = Func->end();
00893   if (I == Func->begin())
00894     return 0;
00895   return wrap(--I);
00896 }
00897 
00898 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
00899   BasicBlock *Block = unwrap(BB);
00900   Function::iterator I = Block;
00901   if (++I == Block->getParent()->end())
00902     return 0;
00903   return wrap(I);
00904 }
00905 
00906 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
00907   BasicBlock *Block = unwrap(BB);
00908   Function::iterator I = Block;
00909   if (I == Block->getParent()->begin())
00910     return 0;
00911   return wrap(--I);
00912 }
00913 
00914 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
00915   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
00916 }
00917 
00918 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
00919                                        const char *Name) {
00920   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
00921   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
00922                                  InsertBeforeBB));
00923 }
00924 
00925 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
00926   unwrap(BBRef)->eraseFromParent();
00927 }
00928 
00929 /*--.. Operations on instructions ..........................................--*/
00930 
00931 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
00932   return wrap(unwrap<Instruction>(Inst)->getParent());
00933 }
00934 
00935 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
00936   BasicBlock *Block = unwrap(BB);
00937   BasicBlock::iterator I = Block->begin();
00938   if (I == Block->end())
00939     return 0;
00940   return wrap(I);
00941 }
00942 
00943 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
00944   BasicBlock *Block = unwrap(BB);
00945   BasicBlock::iterator I = Block->end();
00946   if (I == Block->begin())
00947     return 0;
00948   return wrap(--I);
00949 }
00950 
00951 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
00952   Instruction *Instr = unwrap<Instruction>(Inst);
00953   BasicBlock::iterator I = Instr;
00954   if (++I == Instr->getParent()->end())
00955     return 0;
00956   return wrap(I);
00957 }
00958 
00959 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
00960   Instruction *Instr = unwrap<Instruction>(Inst);
00961   BasicBlock::iterator I = Instr;
00962   if (I == Instr->getParent()->begin())
00963     return 0;
00964   return wrap(--I);
00965 }
00966 
00967 /*--.. Call and invoke instructions ........................................--*/
00968 
00969 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
00970   Value *V = unwrap(Instr);
00971   if (CallInst *CI = dyn_cast<CallInst>(V))
00972     return CI->getCallingConv();
00973   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
00974     return II->getCallingConv();
00975   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
00976   return 0;
00977 }
00978 
00979 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
00980   Value *V = unwrap(Instr);
00981   if (CallInst *CI = dyn_cast<CallInst>(V))
00982     return CI->setCallingConv(CC);
00983   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
00984     return II->setCallingConv(CC);
00985   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
00986 }
00987 
00988 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
00989                            LLVMAttribute PA) {
00990   CallSite Call = CallSite(unwrap<Instruction>(Instr));
00991   Call.setAttributes(
00992     Call.getAttributes().addAttr(index, PA));
00993 }
00994 
00995 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
00996                               LLVMAttribute PA) {
00997   CallSite Call = CallSite(unwrap<Instruction>(Instr));
00998   Call.setAttributes(
00999     Call.getAttributes().removeAttr(index, PA));
01000 }
01001 
01002 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
01003                                 unsigned align) {
01004   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01005   Call.setAttributes(
01006     Call.getAttributes().addAttr(index, 
01007         Attribute::constructAlignmentFromInt(align)));
01008 }
01009 
01010 /*--.. Operations on call instructions (only) ..............................--*/
01011 
01012 int LLVMIsTailCall(LLVMValueRef Call) {
01013   return unwrap<CallInst>(Call)->isTailCall();
01014 }
01015 
01016 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
01017   unwrap<CallInst>(Call)->setTailCall(isTailCall);
01018 }
01019 
01020 /*--.. Operations on phi nodes .............................................--*/
01021 
01022 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
01023                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
01024   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
01025   for (unsigned I = 0; I != Count; ++I)
01026     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
01027 }
01028 
01029 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
01030   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
01031 }
01032 
01033 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
01034   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
01035 }
01036 
01037 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
01038   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
01039 }
01040 
01041 
01042 /*===-- Instruction builders ----------------------------------------------===*/
01043 
01044 LLVMBuilderRef LLVMCreateBuilder(void) {
01045   return wrap(new IRBuilder<>());
01046 }
01047 
01048 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
01049                          LLVMValueRef Instr) {
01050   BasicBlock *BB = unwrap(Block);
01051   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
01052   unwrap(Builder)->SetInsertPoint(BB, I);
01053 }
01054 
01055 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
01056   Instruction *I = unwrap<Instruction>(Instr);
01057   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
01058 }
01059 
01060 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
01061   BasicBlock *BB = unwrap(Block);
01062   unwrap(Builder)->SetInsertPoint(BB);
01063 }
01064 
01065 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
01066    return wrap(unwrap(Builder)->GetInsertBlock());
01067 }
01068 
01069 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
01070   unwrap(Builder)->ClearInsertionPoint ();
01071 }
01072 
01073 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
01074   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
01075 }
01076 
01077 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
01078   delete unwrap(Builder);
01079 }
01080 
01081 /*--.. Instruction builders ................................................--*/
01082 
01083 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
01084   return wrap(unwrap(B)->CreateRetVoid());
01085 }
01086 
01087 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
01088   return wrap(unwrap(B)->CreateRet(unwrap(V)));
01089 }
01090 
01091 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
01092   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
01093 }
01094 
01095 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
01096                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
01097   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
01098 }
01099 
01100 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
01101                              LLVMBasicBlockRef Else, unsigned NumCases) {
01102   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
01103 }
01104 
01105 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
01106                              LLVMValueRef *Args, unsigned NumArgs,
01107                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
01108                              const char *Name) {
01109   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
01110                                       unwrap(Args), unwrap(Args) + NumArgs,
01111                                       Name));
01112 }
01113 
01114 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
01115   return wrap(unwrap(B)->CreateUnwind());
01116 }
01117 
01118 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
01119   return wrap(unwrap(B)->CreateUnreachable());
01120 }
01121 
01122 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
01123                  LLVMBasicBlockRef Dest) {
01124   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
01125 }
01126 
01127 /*--.. Arithmetic ..........................................................--*/
01128 
01129 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01130                           const char *Name) {
01131   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
01132 }
01133 
01134 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01135                           const char *Name) {
01136   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
01137 }
01138 
01139 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01140                           const char *Name) {
01141   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
01142 }
01143 
01144 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01145                            const char *Name) {
01146   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
01147 }
01148 
01149 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01150                            const char *Name) {
01151   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
01152 }
01153 
01154 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01155                            const char *Name) {
01156   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
01157 }
01158 
01159 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01160                            const char *Name) {
01161   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
01162 }
01163 
01164 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01165                            const char *Name) {
01166   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
01167 }
01168 
01169 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01170                            const char *Name) {
01171   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
01172 }
01173 
01174 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01175                           const char *Name) {
01176   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
01177 }
01178 
01179 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01180                            const char *Name) {
01181   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
01182 }
01183 
01184 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01185                            const char *Name) {
01186   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
01187 }
01188 
01189 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01190                           const char *Name) {
01191   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
01192 }
01193 
01194 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01195                          const char *Name) {
01196   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
01197 }
01198 
01199 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
01200                           const char *Name) {
01201   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
01202 }
01203 
01204 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
01205   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
01206 }
01207 
01208 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
01209   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
01210 }
01211 
01212 /*--.. Memory ..............................................................--*/
01213 
01214 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
01215                              const char *Name) {
01216   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
01217 }
01218 
01219 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
01220                                   LLVMValueRef Val, const char *Name) {
01221   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
01222 }
01223 
01224 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
01225                              const char *Name) {
01226   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
01227 }
01228 
01229 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
01230                                   LLVMValueRef Val, const char *Name) {
01231   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
01232 }
01233 
01234 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
01235   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
01236 }
01237 
01238 
01239 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
01240                            const char *Name) {
01241   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
01242 }
01243 
01244 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
01245                             LLVMValueRef PointerVal) {
01246   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
01247 }
01248 
01249 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
01250                           LLVMValueRef *Indices, unsigned NumIndices,
01251                           const char *Name) {
01252   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
01253                                    unwrap(Indices) + NumIndices, Name));
01254 }
01255 
01256 /*--.. Casts ...............................................................--*/
01257 
01258 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
01259                             LLVMTypeRef DestTy, const char *Name) {
01260   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
01261 }
01262 
01263 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
01264                            LLVMTypeRef DestTy, const char *Name) {
01265   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
01266 }
01267 
01268 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
01269                            LLVMTypeRef DestTy, const char *Name) {
01270   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
01271 }
01272 
01273 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
01274                              LLVMTypeRef DestTy, const char *Name) {
01275   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
01276 }
01277 
01278 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
01279                              LLVMTypeRef DestTy, const char *Name) {
01280   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
01281 }
01282 
01283 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
01284                              LLVMTypeRef DestTy, const char *Name) {
01285   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
01286 }
01287 
01288 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
01289                              LLVMTypeRef DestTy, const char *Name) {
01290   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
01291 }
01292 
01293 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
01294                               LLVMTypeRef DestTy, const char *Name) {
01295   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
01296 }
01297 
01298 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
01299                             LLVMTypeRef DestTy, const char *Name) {
01300   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
01301 }
01302 
01303 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
01304                                LLVMTypeRef DestTy, const char *Name) {
01305   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
01306 }
01307 
01308 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
01309                                LLVMTypeRef DestTy, const char *Name) {
01310   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
01311 }
01312 
01313 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
01314                               LLVMTypeRef DestTy, const char *Name) {
01315   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
01316 }
01317 
01318 /*--.. Comparisons .........................................................--*/
01319 
01320 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
01321                            LLVMValueRef LHS, LLVMValueRef RHS,
01322                            const char *Name) {
01323   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
01324                                     unwrap(LHS), unwrap(RHS), Name));
01325 }
01326 
01327 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
01328                            LLVMValueRef LHS, LLVMValueRef RHS,
01329                            const char *Name) {
01330   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
01331                                     unwrap(LHS), unwrap(RHS), Name));
01332 }
01333 
01334 /*--.. Miscellaneous instructions ..........................................--*/
01335 
01336 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
01337   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
01338 }
01339 
01340 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
01341                            LLVMValueRef *Args, unsigned NumArgs,
01342                            const char *Name) {
01343   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
01344                                     unwrap(Args) + NumArgs, Name));
01345 }
01346 
01347 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
01348                              LLVMValueRef Then, LLVMValueRef Else,
01349                              const char *Name) {
01350   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
01351                                       Name));
01352 }
01353 
01354 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
01355                             LLVMTypeRef Ty, const char *Name) {
01356   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
01357 }
01358 
01359 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
01360                                       LLVMValueRef Index, const char *Name) {
01361   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
01362                                               Name));
01363 }
01364 
01365 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
01366                                     LLVMValueRef EltVal, LLVMValueRef Index,
01367                                     const char *Name) {
01368   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
01369                                              unwrap(Index), Name));
01370 }
01371 
01372 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
01373                                     LLVMValueRef V2, LLVMValueRef Mask,
01374                                     const char *Name) {
01375   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
01376                                              unwrap(Mask), Name));
01377 }
01378 
01379 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
01380                                    unsigned Index, const char *Name) {
01381   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
01382 }
01383 
01384 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
01385                                   LLVMValueRef EltVal, unsigned Index,
01386                                   const char *Name) {
01387   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
01388                                            Index, Name));
01389 }
01390 
01391 
01392 /*===-- Module providers --------------------------------------------------===*/
01393 
01394 LLVMModuleProviderRef
01395 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
01396   return wrap(new ExistingModuleProvider(unwrap(M)));
01397 }
01398 
01399 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
01400   delete unwrap(MP);
01401 }
01402 
01403 
01404 /*===-- Memory buffers ----------------------------------------------------===*/
01405 
01406 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
01407                                              LLVMMemoryBufferRef *OutMemBuf,
01408                                              char **OutMessage) {
01409   std::string Error;
01410   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
01411     *OutMemBuf = wrap(MB);
01412     return 0;
01413   }
01414   
01415   *OutMessage = strdup(Error.c_str());
01416   return 1;
01417 }
01418 
01419 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
01420                                     char **OutMessage) {
01421   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
01422     *OutMemBuf = wrap(MB);
01423     return 0;
01424   }
01425   
01426   *OutMessage = strdup("stdin is empty.");
01427   return 1;
01428 }
01429 
01430 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
01431   delete unwrap(MemBuf);
01432 }