LLVM API Documentation
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