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/LLVMContext.h" 00022 #include "llvm/TypeSymbolTable.h" 00023 #include "llvm/ModuleProvider.h" 00024 #include "llvm/InlineAsm.h" 00025 #include "llvm/IntrinsicInst.h" 00026 #include "llvm/Support/MemoryBuffer.h" 00027 #include "llvm/Support/CallSite.h" 00028 #include "llvm/Support/ErrorHandling.h" 00029 #include <cassert> 00030 #include <cstdlib> 00031 #include <cstring> 00032 00033 using namespace llvm; 00034 00035 00036 /*===-- Error handling ----------------------------------------------------===*/ 00037 00038 void LLVMDisposeMessage(char *Message) { 00039 free(Message); 00040 } 00041 00042 00043 /*===-- Operations on contexts --------------------------------------------===*/ 00044 00045 LLVMContextRef LLVMContextCreate() { 00046 return wrap(new LLVMContext()); 00047 } 00048 00049 LLVMContextRef LLVMGetGlobalContext() { 00050 return wrap(&getGlobalContext()); 00051 } 00052 00053 void LLVMContextDispose(LLVMContextRef C) { 00054 delete unwrap(C); 00055 } 00056 00057 00058 /*===-- Operations on modules ---------------------------------------------===*/ 00059 00060 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) { 00061 return wrap(new Module(ModuleID, getGlobalContext())); 00062 } 00063 00064 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 00065 LLVMContextRef C) { 00066 return wrap(new Module(ModuleID, *unwrap(C))); 00067 } 00068 00069 void LLVMDisposeModule(LLVMModuleRef M) { 00070 delete unwrap(M); 00071 } 00072 00073 /*--.. Data layout .........................................................--*/ 00074 const char * LLVMGetDataLayout(LLVMModuleRef M) { 00075 return unwrap(M)->getDataLayout().c_str(); 00076 } 00077 00078 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) { 00079 unwrap(M)->setDataLayout(Triple); 00080 } 00081 00082 /*--.. Target triple .......................................................--*/ 00083 const char * LLVMGetTarget(LLVMModuleRef M) { 00084 return unwrap(M)->getTargetTriple().c_str(); 00085 } 00086 00087 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) { 00088 unwrap(M)->setTargetTriple(Triple); 00089 } 00090 00091 /*--.. Type names ..........................................................--*/ 00092 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) { 00093 return unwrap(M)->addTypeName(Name, unwrap(Ty)); 00094 } 00095 00096 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) { 00097 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable(); 00098 00099 TypeSymbolTable::iterator I = TST.find(Name); 00100 if (I != TST.end()) 00101 TST.remove(I); 00102 } 00103 00104 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { 00105 return wrap(unwrap(M)->getTypeByName(Name)); 00106 } 00107 00108 void LLVMDumpModule(LLVMModuleRef M) { 00109 unwrap(M)->dump(); 00110 } 00111 00112 00113 /*===-- Operations on types -----------------------------------------------===*/ 00114 00115 /*--.. Operations on all types (mostly) ....................................--*/ 00116 00117 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) { 00118 switch (unwrap(Ty)->getTypeID()) { 00119 default: 00120 assert(false && "Unhandled TypeID."); 00121 case Type::VoidTyID: 00122 return LLVMVoidTypeKind; 00123 case Type::FloatTyID: 00124 return LLVMFloatTypeKind; 00125 case Type::DoubleTyID: 00126 return LLVMDoubleTypeKind; 00127 case Type::X86_FP80TyID: 00128 return LLVMX86_FP80TypeKind; 00129 case Type::FP128TyID: 00130 return LLVMFP128TypeKind; 00131 case Type::PPC_FP128TyID: 00132 return LLVMPPC_FP128TypeKind; 00133 case Type::LabelTyID: 00134 return LLVMLabelTypeKind; 00135 case Type::MetadataTyID: 00136 return LLVMMetadataTypeKind; 00137 case Type::IntegerTyID: 00138 return LLVMIntegerTypeKind; 00139 case Type::FunctionTyID: 00140 return LLVMFunctionTypeKind; 00141 case Type::StructTyID: 00142 return LLVMStructTypeKind; 00143 case Type::ArrayTyID: 00144 return LLVMArrayTypeKind; 00145 case Type::PointerTyID: 00146 return LLVMPointerTypeKind; 00147 case Type::OpaqueTyID: 00148 return LLVMOpaqueTypeKind; 00149 case Type::VectorTyID: 00150 return LLVMVectorTypeKind; 00151 } 00152 } 00153 00154 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) { 00155 return wrap(&unwrap(Ty)->getContext()); 00156 } 00157 00158 /*--.. Operations on integer types .........................................--*/ 00159 00160 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) { 00161 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C)); 00162 } 00163 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) { 00164 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C)); 00165 } 00166 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) { 00167 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C)); 00168 } 00169 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) { 00170 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C)); 00171 } 00172 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) { 00173 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C)); 00174 } 00175 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) { 00176 return wrap(IntegerType::get(*unwrap(C), NumBits)); 00177 } 00178 00179 LLVMTypeRef LLVMInt1Type(void) { 00180 return LLVMInt1TypeInContext(LLVMGetGlobalContext()); 00181 } 00182 LLVMTypeRef LLVMInt8Type(void) { 00183 return LLVMInt8TypeInContext(LLVMGetGlobalContext()); 00184 } 00185 LLVMTypeRef LLVMInt16Type(void) { 00186 return LLVMInt16TypeInContext(LLVMGetGlobalContext()); 00187 } 00188 LLVMTypeRef LLVMInt32Type(void) { 00189 return LLVMInt32TypeInContext(LLVMGetGlobalContext()); 00190 } 00191 LLVMTypeRef LLVMInt64Type(void) { 00192 return LLVMInt64TypeInContext(LLVMGetGlobalContext()); 00193 } 00194 LLVMTypeRef LLVMIntType(unsigned NumBits) { 00195 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits); 00196 } 00197 00198 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) { 00199 return unwrap<IntegerType>(IntegerTy)->getBitWidth(); 00200 } 00201 00202 /*--.. Operations on real types ............................................--*/ 00203 00204 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) { 00205 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C)); 00206 } 00207 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) { 00208 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C)); 00209 } 00210 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) { 00211 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C)); 00212 } 00213 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) { 00214 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C)); 00215 } 00216 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) { 00217 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C)); 00218 } 00219 00220 LLVMTypeRef LLVMFloatType(void) { 00221 return LLVMFloatTypeInContext(LLVMGetGlobalContext()); 00222 } 00223 LLVMTypeRef LLVMDoubleType(void) { 00224 return LLVMDoubleTypeInContext(LLVMGetGlobalContext()); 00225 } 00226 LLVMTypeRef LLVMX86FP80Type(void) { 00227 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext()); 00228 } 00229 LLVMTypeRef LLVMFP128Type(void) { 00230 return LLVMFP128TypeInContext(LLVMGetGlobalContext()); 00231 } 00232 LLVMTypeRef LLVMPPCFP128Type(void) { 00233 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext()); 00234 } 00235 00236 /*--.. Operations on function types ........................................--*/ 00237 00238 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType, 00239 LLVMTypeRef *ParamTypes, unsigned ParamCount, 00240 int IsVarArg) { 00241 std::vector<const Type*> Tys; 00242 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I) 00243 Tys.push_back(unwrap(*I)); 00244 00245 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0)); 00246 } 00247 00248 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) { 00249 return unwrap<FunctionType>(FunctionTy)->isVarArg(); 00250 } 00251 00252 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) { 00253 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType()); 00254 } 00255 00256 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) { 00257 return unwrap<FunctionType>(FunctionTy)->getNumParams(); 00258 } 00259 00260 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) { 00261 FunctionType *Ty = unwrap<FunctionType>(FunctionTy); 00262 for (FunctionType::param_iterator I = Ty->param_begin(), 00263 E = Ty->param_end(); I != E; ++I) 00264 *Dest++ = wrap(*I); 00265 } 00266 00267 /*--.. Operations on struct types ..........................................--*/ 00268 00269 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes, 00270 unsigned ElementCount, int Packed) { 00271 std::vector<const Type*> Tys; 00272 for (LLVMTypeRef *I = ElementTypes, 00273 *E = ElementTypes + ElementCount; I != E; ++I) 00274 Tys.push_back(unwrap(*I)); 00275 00276 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0)); 00277 } 00278 00279 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, 00280 unsigned ElementCount, int Packed) { 00281 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes, 00282 ElementCount, Packed); 00283 } 00284 00285 00286 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) { 00287 return unwrap<StructType>(StructTy)->getNumElements(); 00288 } 00289 00290 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) { 00291 StructType *Ty = unwrap<StructType>(StructTy); 00292 for (FunctionType::param_iterator I = Ty->element_begin(), 00293 E = Ty->element_end(); I != E; ++I) 00294 *Dest++ = wrap(*I); 00295 } 00296 00297 int LLVMIsPackedStruct(LLVMTypeRef StructTy) { 00298 return unwrap<StructType>(StructTy)->isPacked(); 00299 } 00300 00301 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ 00302 00303 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { 00304 return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); 00305 } 00306 00307 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) { 00308 return wrap(PointerType::get(unwrap(ElementType), AddressSpace)); 00309 } 00310 00311 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) { 00312 return wrap(VectorType::get(unwrap(ElementType), ElementCount)); 00313 } 00314 00315 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) { 00316 return wrap(unwrap<SequentialType>(Ty)->getElementType()); 00317 } 00318 00319 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { 00320 return unwrap<ArrayType>(ArrayTy)->getNumElements(); 00321 } 00322 00323 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) { 00324 return unwrap<PointerType>(PointerTy)->getAddressSpace(); 00325 } 00326 00327 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) { 00328 return unwrap<VectorType>(VectorTy)->getNumElements(); 00329 } 00330 00331 /*--.. Operations on other types ...........................................--*/ 00332 00333 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) { 00334 return wrap(Type::getVoidTy(*unwrap(C))); 00335 } 00336 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) { 00337 return wrap(Type::getLabelTy(*unwrap(C))); 00338 } 00339 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) { 00340 return wrap(OpaqueType::get(*unwrap(C))); 00341 } 00342 00343 LLVMTypeRef LLVMVoidType(void) { 00344 return LLVMVoidTypeInContext(LLVMGetGlobalContext()); 00345 } 00346 LLVMTypeRef LLVMLabelType(void) { 00347 return LLVMLabelTypeInContext(LLVMGetGlobalContext()); 00348 } 00349 LLVMTypeRef LLVMOpaqueType(void) { 00350 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext()); 00351 } 00352 00353 /*--.. Operations on type handles ..........................................--*/ 00354 00355 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) { 00356 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy))); 00357 } 00358 00359 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) { 00360 delete unwrap(TypeHandle); 00361 } 00362 00363 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) { 00364 return wrap(unwrap(TypeHandle)->get()); 00365 } 00366 00367 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) { 00368 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy)); 00369 } 00370 00371 00372 /*===-- Operations on values ----------------------------------------------===*/ 00373 00374 /*--.. Operations on all values ............................................--*/ 00375 00376 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) { 00377 return wrap(unwrap(Val)->getType()); 00378 } 00379 00380 const char *LLVMGetValueName(LLVMValueRef Val) { 00381 return unwrap(Val)->getName().data(); 00382 } 00383 00384 void LLVMSetValueName(LLVMValueRef Val, const char *Name) { 00385 unwrap(Val)->setName(Name); 00386 } 00387 00388 void LLVMDumpValue(LLVMValueRef Val) { 00389 unwrap(Val)->dump(); 00390 } 00391 00392 00393 /*--.. Conversion functions ................................................--*/ 00394 00395 #define LLVM_DEFINE_VALUE_CAST(name) \ 00396 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \ 00397 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \ 00398 } 00399 00400 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) 00401 00402 00403 /*--.. Operations on constants of any type .................................--*/ 00404 00405 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) { 00406 return wrap(Constant::getNullValue(unwrap(Ty))); 00407 } 00408 00409 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) { 00410 return wrap(Constant::getAllOnesValue(unwrap(Ty))); 00411 } 00412 00413 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) { 00414 return wrap(UndefValue::get(unwrap(Ty))); 00415 } 00416 00417 int LLVMIsConstant(LLVMValueRef Ty) { 00418 return isa<Constant>(unwrap(Ty)); 00419 } 00420 00421 int LLVMIsNull(LLVMValueRef Val) { 00422 if (Constant *C = dyn_cast<Constant>(unwrap(Val))) 00423 return C->isNullValue(); 00424 return false; 00425 } 00426 00427 int LLVMIsUndef(LLVMValueRef Val) { 00428 return isa<UndefValue>(unwrap(Val)); 00429 } 00430 00431 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { 00432 return 00433 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty))); 00434 } 00435 00436 /*--.. Operations on scalar constants ......................................--*/ 00437 00438 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, 00439 int SignExtend) { 00440 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0)); 00441 } 00442 00443 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[], 00444 uint8_t Radix) { 00445 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str), 00446 Radix)); 00447 } 00448 00449 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[], 00450 unsigned SLen, uint8_t Radix) { 00451 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen), 00452 Radix)); 00453 } 00454 00455 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { 00456 return wrap(ConstantFP::get(unwrap(RealTy), N)); 00457 } 00458 00459 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { 00460 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text))); 00461 } 00462 00463 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[], 00464 unsigned SLen) { 00465 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen))); 00466 } 00467 00468 /*--.. Operations on composite constants ...................................--*/ 00469 00470 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, 00471 unsigned Length, int DontNullTerminate) { 00472 /* Inverted the sense of AddNull because ', 0)' is a 00473 better mnemonic for null termination than ', 1)'. */ 00474 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length), 00475 DontNullTerminate == 0)); 00476 } 00477 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 00478 LLVMValueRef *ConstantVals, 00479 unsigned Count, int Packed) { 00480 return wrap(ConstantStruct::get(*unwrap(C), 00481 unwrap<Constant>(ConstantVals, Count), 00482 Count, Packed != 0)); 00483 } 00484 00485 LLVMValueRef LLVMConstString(const char *Str, unsigned Length, 00486 int DontNullTerminate) { 00487 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length, 00488 DontNullTerminate); 00489 } 00490 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, 00491 LLVMValueRef *ConstantVals, unsigned Length) { 00492 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), 00493 unwrap<Constant>(ConstantVals, Length), 00494 Length)); 00495 } 00496 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, 00497 int Packed) { 00498 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count, 00499 Packed); 00500 } 00501 00502 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) { 00503 return wrap(ConstantVector::get( 00504 unwrap<Constant>(ScalarConstantVals, Size), Size)); 00505 } 00506 00507 /*--.. Constant expressions ................................................--*/ 00508 00509 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) { 00510 return wrap(ConstantExpr::getAlignOf(unwrap(Ty))); 00511 } 00512 00513 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) { 00514 return wrap(ConstantExpr::getSizeOf(unwrap(Ty))); 00515 } 00516 00517 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) { 00518 return wrap(ConstantExpr::getNeg( 00519 unwrap<Constant>(ConstantVal))); 00520 } 00521 00522 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) { 00523 return wrap(ConstantExpr::getFNeg( 00524 unwrap<Constant>(ConstantVal))); 00525 } 00526 00527 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) { 00528 return wrap(ConstantExpr::getNot( 00529 unwrap<Constant>(ConstantVal))); 00530 } 00531 00532 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00533 return wrap(ConstantExpr::getAdd( 00534 unwrap<Constant>(LHSConstant), 00535 unwrap<Constant>(RHSConstant))); 00536 } 00537 00538 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, 00539 LLVMValueRef RHSConstant) { 00540 return wrap(ConstantExpr::getNSWAdd( 00541 unwrap<Constant>(LHSConstant), 00542 unwrap<Constant>(RHSConstant))); 00543 } 00544 00545 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00546 return wrap(ConstantExpr::getFAdd( 00547 unwrap<Constant>(LHSConstant), 00548 unwrap<Constant>(RHSConstant))); 00549 } 00550 00551 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00552 return wrap(ConstantExpr::getSub( 00553 unwrap<Constant>(LHSConstant), 00554 unwrap<Constant>(RHSConstant))); 00555 } 00556 00557 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00558 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant), 00559 unwrap<Constant>(RHSConstant))); 00560 } 00561 00562 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00563 return wrap(ConstantExpr::getMul( 00564 unwrap<Constant>(LHSConstant), 00565 unwrap<Constant>(RHSConstant))); 00566 } 00567 00568 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00569 return wrap(ConstantExpr::getFMul( 00570 unwrap<Constant>(LHSConstant), 00571 unwrap<Constant>(RHSConstant))); 00572 } 00573 00574 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00575 return wrap(ConstantExpr::getUDiv( 00576 unwrap<Constant>(LHSConstant), 00577 unwrap<Constant>(RHSConstant))); 00578 } 00579 00580 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00581 return wrap(ConstantExpr::getSDiv( 00582 unwrap<Constant>(LHSConstant), 00583 unwrap<Constant>(RHSConstant))); 00584 } 00585 00586 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, 00587 LLVMValueRef RHSConstant) { 00588 return wrap(ConstantExpr::getExactSDiv( 00589 unwrap<Constant>(LHSConstant), 00590 unwrap<Constant>(RHSConstant))); 00591 } 00592 00593 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00594 return wrap(ConstantExpr::getFDiv( 00595 unwrap<Constant>(LHSConstant), 00596 unwrap<Constant>(RHSConstant))); 00597 } 00598 00599 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00600 return wrap(ConstantExpr::getURem( 00601 unwrap<Constant>(LHSConstant), 00602 unwrap<Constant>(RHSConstant))); 00603 } 00604 00605 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00606 return wrap(ConstantExpr::getSRem( 00607 unwrap<Constant>(LHSConstant), 00608 unwrap<Constant>(RHSConstant))); 00609 } 00610 00611 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00612 return wrap(ConstantExpr::getFRem( 00613 unwrap<Constant>(LHSConstant), 00614 unwrap<Constant>(RHSConstant))); 00615 } 00616 00617 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00618 return wrap(ConstantExpr::getAnd( 00619 unwrap<Constant>(LHSConstant), 00620 unwrap<Constant>(RHSConstant))); 00621 } 00622 00623 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00624 return wrap(ConstantExpr::getOr( 00625 unwrap<Constant>(LHSConstant), 00626 unwrap<Constant>(RHSConstant))); 00627 } 00628 00629 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00630 return wrap(ConstantExpr::getXor( 00631 unwrap<Constant>(LHSConstant), 00632 unwrap<Constant>(RHSConstant))); 00633 } 00634 00635 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, 00636 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00637 return wrap(ConstantExpr::getICmp(Predicate, 00638 unwrap<Constant>(LHSConstant), 00639 unwrap<Constant>(RHSConstant))); 00640 } 00641 00642 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, 00643 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00644 return wrap(ConstantExpr::getFCmp(Predicate, 00645 unwrap<Constant>(LHSConstant), 00646 unwrap<Constant>(RHSConstant))); 00647 } 00648 00649 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00650 return wrap(ConstantExpr::getShl( 00651 unwrap<Constant>(LHSConstant), 00652 unwrap<Constant>(RHSConstant))); 00653 } 00654 00655 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00656 return wrap(ConstantExpr::getLShr( 00657 unwrap<Constant>(LHSConstant), 00658 unwrap<Constant>(RHSConstant))); 00659 } 00660 00661 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00662 return wrap(ConstantExpr::getAShr( 00663 unwrap<Constant>(LHSConstant), 00664 unwrap<Constant>(RHSConstant))); 00665 } 00666 00667 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, 00668 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 00669 return wrap(ConstantExpr::getGetElementPtr( 00670 unwrap<Constant>(ConstantVal), 00671 unwrap<Constant>(ConstantIndices, 00672 NumIndices), 00673 NumIndices)); 00674 } 00675 00676 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 00677 LLVMValueRef *ConstantIndices, 00678 unsigned NumIndices) { 00679 Constant* Val = unwrap<Constant>(ConstantVal); 00680 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices); 00681 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices)); 00682 } 00683 00684 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00685 return wrap(ConstantExpr::getTrunc( 00686 unwrap<Constant>(ConstantVal), 00687 unwrap(ToType))); 00688 } 00689 00690 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00691 return wrap(ConstantExpr::getSExt( 00692 unwrap<Constant>(ConstantVal), 00693 unwrap(ToType))); 00694 } 00695 00696 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00697 return wrap(ConstantExpr::getZExt( 00698 unwrap<Constant>(ConstantVal), 00699 unwrap(ToType))); 00700 } 00701 00702 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00703 return wrap(ConstantExpr::getFPTrunc( 00704 unwrap<Constant>(ConstantVal), 00705 unwrap(ToType))); 00706 } 00707 00708 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00709 return wrap(ConstantExpr::getFPExtend( 00710 unwrap<Constant>(ConstantVal), 00711 unwrap(ToType))); 00712 } 00713 00714 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00715 return wrap(ConstantExpr::getUIToFP( 00716 unwrap<Constant>(ConstantVal), 00717 unwrap(ToType))); 00718 } 00719 00720 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00721 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 00722 unwrap(ToType))); 00723 } 00724 00725 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00726 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 00727 unwrap(ToType))); 00728 } 00729 00730 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00731 return wrap(ConstantExpr::getFPToSI( 00732 unwrap<Constant>(ConstantVal), 00733 unwrap(ToType))); 00734 } 00735 00736 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00737 return wrap(ConstantExpr::getPtrToInt( 00738 unwrap<Constant>(ConstantVal), 00739 unwrap(ToType))); 00740 } 00741 00742 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00743 return wrap(ConstantExpr::getIntToPtr( 00744 unwrap<Constant>(ConstantVal), 00745 unwrap(ToType))); 00746 } 00747 00748 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00749 return wrap(ConstantExpr::getBitCast( 00750 unwrap<Constant>(ConstantVal), 00751 unwrap(ToType))); 00752 } 00753 00754 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 00755 LLVMTypeRef ToType) { 00756 return wrap(ConstantExpr::getZExtOrBitCast( 00757 unwrap<Constant>(ConstantVal), 00758 unwrap(ToType))); 00759 } 00760 00761 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 00762 LLVMTypeRef ToType) { 00763 return wrap(ConstantExpr::getSExtOrBitCast( 00764 unwrap<Constant>(ConstantVal), 00765 unwrap(ToType))); 00766 } 00767 00768 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 00769 LLVMTypeRef ToType) { 00770 return wrap(ConstantExpr::getTruncOrBitCast( 00771 unwrap<Constant>(ConstantVal), 00772 unwrap(ToType))); 00773 } 00774 00775 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 00776 LLVMTypeRef ToType) { 00777 return wrap(ConstantExpr::getPointerCast( 00778 unwrap<Constant>(ConstantVal), 00779 unwrap(ToType))); 00780 } 00781 00782 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 00783 unsigned isSigned) { 00784 return wrap(ConstantExpr::getIntegerCast( 00785 unwrap<Constant>(ConstantVal), 00786 unwrap(ToType), 00787 isSigned)); 00788 } 00789 00790 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00791 return wrap(ConstantExpr::getFPCast( 00792 unwrap<Constant>(ConstantVal), 00793 unwrap(ToType))); 00794 } 00795 00796 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 00797 LLVMValueRef ConstantIfTrue, 00798 LLVMValueRef ConstantIfFalse) { 00799 return wrap(ConstantExpr::getSelect( 00800 unwrap<Constant>(ConstantCondition), 00801 unwrap<Constant>(ConstantIfTrue), 00802 unwrap<Constant>(ConstantIfFalse))); 00803 } 00804 00805 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 00806 LLVMValueRef IndexConstant) { 00807 return wrap(ConstantExpr::getExtractElement( 00808 unwrap<Constant>(VectorConstant), 00809 unwrap<Constant>(IndexConstant))); 00810 } 00811 00812 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 00813 LLVMValueRef ElementValueConstant, 00814 LLVMValueRef IndexConstant) { 00815 return wrap(ConstantExpr::getInsertElement( 00816 unwrap<Constant>(VectorConstant), 00817 unwrap<Constant>(ElementValueConstant), 00818 unwrap<Constant>(IndexConstant))); 00819 } 00820 00821 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 00822 LLVMValueRef VectorBConstant, 00823 LLVMValueRef MaskConstant) { 00824 return wrap(ConstantExpr::getShuffleVector( 00825 unwrap<Constant>(VectorAConstant), 00826 unwrap<Constant>(VectorBConstant), 00827 unwrap<Constant>(MaskConstant))); 00828 } 00829 00830 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 00831 unsigned NumIdx) { 00832 return wrap(ConstantExpr::getExtractValue( 00833 unwrap<Constant>(AggConstant), 00834 IdxList, NumIdx)); 00835 } 00836 00837 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 00838 LLVMValueRef ElementValueConstant, 00839 unsigned *IdxList, unsigned NumIdx) { 00840 return wrap(ConstantExpr::getInsertValue( 00841 unwrap<Constant>(AggConstant), 00842 unwrap<Constant>(ElementValueConstant), 00843 IdxList, NumIdx)); 00844 } 00845 00846 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 00847 const char *Constraints, int HasSideEffects) { 00848 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 00849 Constraints, HasSideEffects)); 00850 } 00851 00852 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 00853 00854 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 00855 return wrap(unwrap<GlobalValue>(Global)->getParent()); 00856 } 00857 00858 int LLVMIsDeclaration(LLVMValueRef Global) { 00859 return unwrap<GlobalValue>(Global)->isDeclaration(); 00860 } 00861 00862 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 00863 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 00864 default: 00865 assert(false && "Unhandled Linkage Type."); 00866 case GlobalValue::ExternalLinkage: 00867 return LLVMExternalLinkage; 00868 case GlobalValue::AvailableExternallyLinkage: 00869 return LLVMAvailableExternallyLinkage; 00870 case GlobalValue::LinkOnceAnyLinkage: 00871 return LLVMLinkOnceAnyLinkage; 00872 case GlobalValue::LinkOnceODRLinkage: 00873 return LLVMLinkOnceODRLinkage; 00874 case GlobalValue::WeakAnyLinkage: 00875 return LLVMWeakAnyLinkage; 00876 case GlobalValue::WeakODRLinkage: 00877 return LLVMWeakODRLinkage; 00878 case GlobalValue::AppendingLinkage: 00879 return LLVMAppendingLinkage; 00880 case GlobalValue::InternalLinkage: 00881 return LLVMInternalLinkage; 00882 case GlobalValue::PrivateLinkage: 00883 return LLVMPrivateLinkage; 00884 case GlobalValue::LinkerPrivateLinkage: 00885 return LLVMLinkerPrivateLinkage; 00886 case GlobalValue::DLLImportLinkage: 00887 return LLVMDLLImportLinkage; 00888 case GlobalValue::DLLExportLinkage: 00889 return LLVMDLLExportLinkage; 00890 case GlobalValue::ExternalWeakLinkage: 00891 return LLVMExternalWeakLinkage; 00892 case GlobalValue::GhostLinkage: 00893 return LLVMGhostLinkage; 00894 case GlobalValue::CommonLinkage: 00895 return LLVMCommonLinkage; 00896 } 00897 00898 // Should never get here. 00899 return static_cast<LLVMLinkage>(0); 00900 } 00901 00902 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 00903 GlobalValue *GV = unwrap<GlobalValue>(Global); 00904 00905 switch (Linkage) { 00906 default: 00907 assert(false && "Unhandled Linkage Type."); 00908 case LLVMExternalLinkage: 00909 GV->setLinkage(GlobalValue::ExternalLinkage); 00910 break; 00911 case LLVMAvailableExternallyLinkage: 00912 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 00913 break; 00914 case LLVMLinkOnceAnyLinkage: 00915 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 00916 break; 00917 case LLVMLinkOnceODRLinkage: 00918 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 00919 break; 00920 case LLVMWeakAnyLinkage: 00921 GV->setLinkage(GlobalValue::WeakAnyLinkage); 00922 break; 00923 case LLVMWeakODRLinkage: 00924 GV->setLinkage(GlobalValue::WeakODRLinkage); 00925 break; 00926 case LLVMAppendingLinkage: 00927 GV->setLinkage(GlobalValue::AppendingLinkage); 00928 break; 00929 case LLVMInternalLinkage: 00930 GV->setLinkage(GlobalValue::InternalLinkage); 00931 break; 00932 case LLVMPrivateLinkage: 00933 GV->setLinkage(GlobalValue::PrivateLinkage); 00934 break; 00935 case LLVMLinkerPrivateLinkage: 00936 GV->setLinkage(GlobalValue::LinkerPrivateLinkage); 00937 break; 00938 case LLVMDLLImportLinkage: 00939 GV->setLinkage(GlobalValue::DLLImportLinkage); 00940 break; 00941 case LLVMDLLExportLinkage: 00942 GV->setLinkage(GlobalValue::DLLExportLinkage); 00943 break; 00944 case LLVMExternalWeakLinkage: 00945 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 00946 break; 00947 case LLVMGhostLinkage: 00948 GV->setLinkage(GlobalValue::GhostLinkage); 00949 break; 00950 case LLVMCommonLinkage: 00951 GV->setLinkage(GlobalValue::CommonLinkage); 00952 break; 00953 } 00954 } 00955 00956 const char *LLVMGetSection(LLVMValueRef Global) { 00957 return unwrap<GlobalValue>(Global)->getSection().c_str(); 00958 } 00959 00960 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 00961 unwrap<GlobalValue>(Global)->setSection(Section); 00962 } 00963 00964 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 00965 return static_cast<LLVMVisibility>( 00966 unwrap<GlobalValue>(Global)->getVisibility()); 00967 } 00968 00969 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 00970 unwrap<GlobalValue>(Global) 00971 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 00972 } 00973 00974 unsigned LLVMGetAlignment(LLVMValueRef Global) { 00975 return unwrap<GlobalValue>(Global)->getAlignment(); 00976 } 00977 00978 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) { 00979 unwrap<GlobalValue>(Global)->setAlignment(Bytes); 00980 } 00981 00982 /*--.. Operations on global variables ......................................--*/ 00983 00984 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 00985 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 00986 GlobalValue::ExternalLinkage, 0, Name)); 00987 } 00988 00989 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 00990 return wrap(unwrap(M)->getNamedGlobal(Name)); 00991 } 00992 00993 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 00994 Module *Mod = unwrap(M); 00995 Module::global_iterator I = Mod->global_begin(); 00996 if (I == Mod->global_end()) 00997 return 0; 00998 return wrap(I); 00999 } 01000 01001 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 01002 Module *Mod = unwrap(M); 01003 Module::global_iterator I = Mod->global_end(); 01004 if (I == Mod->global_begin()) 01005 return 0; 01006 return wrap(--I); 01007 } 01008 01009 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 01010 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 01011 Module::global_iterator I = GV; 01012 if (++I == GV->getParent()->global_end()) 01013 return 0; 01014 return wrap(I); 01015 } 01016 01017 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 01018 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 01019 Module::global_iterator I = GV; 01020 if (I == GV->getParent()->global_begin()) 01021 return 0; 01022 return wrap(--I); 01023 } 01024 01025 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 01026 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 01027 } 01028 01029 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 01030 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer()); 01031 } 01032 01033 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 01034 unwrap<GlobalVariable>(GlobalVar) 01035 ->setInitializer(unwrap<Constant>(ConstantVal)); 01036 } 01037 01038 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 01039 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 01040 } 01041 01042 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) { 01043 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 01044 } 01045 01046 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 01047 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 01048 } 01049 01050 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) { 01051 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 01052 } 01053 01054 /*--.. Operations on aliases ......................................--*/ 01055 01056 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 01057 const char *Name) { 01058 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name, 01059 unwrap<Constant>(Aliasee), unwrap (M))); 01060 } 01061 01062 /*--.. Operations on functions .............................................--*/ 01063 01064 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 01065 LLVMTypeRef FunctionTy) { 01066 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 01067 GlobalValue::ExternalLinkage, Name, unwrap(M))); 01068 } 01069 01070 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 01071 return wrap(unwrap(M)->getFunction(Name)); 01072 } 01073 01074 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 01075 Module *Mod = unwrap(M); 01076 Module::iterator I = Mod->begin(); 01077 if (I == Mod->end()) 01078 return 0; 01079 return wrap(I); 01080 } 01081 01082 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 01083 Module *Mod = unwrap(M); 01084 Module::iterator I = Mod->end(); 01085 if (I == Mod->begin()) 01086 return 0; 01087 return wrap(--I); 01088 } 01089 01090 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 01091 Function *Func = unwrap<Function>(Fn); 01092 Module::iterator I = Func; 01093 if (++I == Func->getParent()->end()) 01094 return 0; 01095 return wrap(I); 01096 } 01097 01098 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 01099 Function *Func = unwrap<Function>(Fn); 01100 Module::iterator I = Func; 01101 if (I == Func->getParent()->begin()) 01102 return 0; 01103 return wrap(--I); 01104 } 01105 01106 void LLVMDeleteFunction(LLVMValueRef Fn) { 01107 unwrap<Function>(Fn)->eraseFromParent(); 01108 } 01109 01110 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 01111 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 01112 return F->getIntrinsicID(); 01113 return 0; 01114 } 01115 01116 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 01117 return unwrap<Function>(Fn)->getCallingConv(); 01118 } 01119 01120 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 01121 return unwrap<Function>(Fn)->setCallingConv(CC); 01122 } 01123 01124 const char *LLVMGetGC(LLVMValueRef Fn) { 01125 Function *F = unwrap<Function>(Fn); 01126 return F->hasGC()? F->getGC() : 0; 01127 } 01128 01129 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 01130 Function *F = unwrap<Function>(Fn); 01131 if (GC) 01132 F->setGC(GC); 01133 else 01134 F->clearGC(); 01135 } 01136 01137 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 01138 Function *Func = unwrap<Function>(Fn); 01139 const AttrListPtr PAL = Func->getAttributes(); 01140 const AttrListPtr PALnew = PAL.addAttr(0, PA); 01141 Func->setAttributes(PALnew); 01142 } 01143 01144 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 01145 Function *Func = unwrap<Function>(Fn); 01146 const AttrListPtr PAL = Func->getAttributes(); 01147 const AttrListPtr PALnew = PAL.removeAttr(0, PA); 01148 Func->setAttributes(PALnew); 01149 } 01150 01151 /*--.. Operations on parameters ............................................--*/ 01152 01153 unsigned LLVMCountParams(LLVMValueRef FnRef) { 01154 // This function is strictly redundant to 01155 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 01156 return unwrap<Function>(FnRef)->arg_size(); 01157 } 01158 01159 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 01160 Function *Fn = unwrap<Function>(FnRef); 01161 for (Function::arg_iterator I = Fn->arg_begin(), 01162 E = Fn->arg_end(); I != E; I++) 01163 *ParamRefs++ = wrap(I); 01164 } 01165 01166 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 01167 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin(); 01168 while (index --> 0) 01169 AI++; 01170 return wrap(AI); 01171 } 01172 01173 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 01174 return wrap(unwrap<Argument>(V)->getParent()); 01175 } 01176 01177 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 01178 Function *Func = unwrap<Function>(Fn); 01179 Function::arg_iterator I = Func->arg_begin(); 01180 if (I == Func->arg_end()) 01181 return 0; 01182 return wrap(I); 01183 } 01184 01185 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 01186 Function *Func = unwrap<Function>(Fn); 01187 Function::arg_iterator I = Func->arg_end(); 01188 if (I == Func->arg_begin()) 01189 return 0; 01190 return wrap(--I); 01191 } 01192 01193 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 01194 Argument *A = unwrap<Argument>(Arg); 01195 Function::arg_iterator I = A; 01196 if (++I == A->getParent()->arg_end()) 01197 return 0; 01198 return wrap(I); 01199 } 01200 01201 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 01202 Argument *A = unwrap<Argument>(Arg); 01203 Function::arg_iterator I = A; 01204 if (I == A->getParent()->arg_begin()) 01205 return 0; 01206 return wrap(--I); 01207 } 01208 01209 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 01210 unwrap<Argument>(Arg)->addAttr(PA); 01211 } 01212 01213 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 01214 unwrap<Argument>(Arg)->removeAttr(PA); 01215 } 01216 01217 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 01218 unwrap<Argument>(Arg)->addAttr( 01219 Attribute::constructAlignmentFromInt(align)); 01220 } 01221 01222 /*--.. Operations on basic blocks ..........................................--*/ 01223 01224 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 01225 return wrap(static_cast<Value*>(unwrap(BB))); 01226 } 01227 01228 int LLVMValueIsBasicBlock(LLVMValueRef Val) { 01229 return isa<BasicBlock>(unwrap(Val)); 01230 } 01231 01232 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 01233 return wrap(unwrap<BasicBlock>(Val)); 01234 } 01235 01236 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 01237 return wrap(unwrap(BB)->getParent()); 01238 } 01239 01240 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 01241 return unwrap<Function>(FnRef)->size(); 01242 } 01243 01244 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 01245 Function *Fn = unwrap<Function>(FnRef); 01246 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) 01247 *BasicBlocksRefs++ = wrap(I); 01248 } 01249 01250 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 01251 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 01252 } 01253 01254 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 01255 Function *Func = unwrap<Function>(Fn); 01256 Function::iterator I = Func->begin(); 01257 if (I == Func->end()) 01258 return 0; 01259 return wrap(I); 01260 } 01261 01262 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 01263 Function *Func = unwrap<Function>(Fn); 01264 Function::iterator I = Func->end(); 01265 if (I == Func->begin()) 01266 return 0; 01267 return wrap(--I); 01268 } 01269 01270 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 01271 BasicBlock *Block = unwrap(BB); 01272 Function::iterator I = Block; 01273 if (++I == Block->getParent()->end()) 01274 return 0; 01275 return wrap(I); 01276 } 01277 01278 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 01279 BasicBlock *Block = unwrap(BB); 01280 Function::iterator I = Block; 01281 if (I == Block->getParent()->begin()) 01282 return 0; 01283 return wrap(--I); 01284 } 01285 01286 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 01287 LLVMValueRef FnRef, 01288 const char *Name) { 01289 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 01290 } 01291 01292 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 01293 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 01294 } 01295 01296 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 01297 LLVMBasicBlockRef BBRef, 01298 const char *Name) { 01299 BasicBlock *BB = unwrap(BBRef); 01300 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 01301 } 01302 01303 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 01304 const char *Name) { 01305 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 01306 } 01307 01308 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 01309 unwrap(BBRef)->eraseFromParent(); 01310 } 01311 01312 /*--.. Operations on instructions ..........................................--*/ 01313 01314 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 01315 return wrap(unwrap<Instruction>(Inst)->getParent()); 01316 } 01317 01318 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 01319 BasicBlock *Block = unwrap(BB); 01320 BasicBlock::iterator I = Block->begin(); 01321 if (I == Block->end()) 01322 return 0; 01323 return wrap(I); 01324 } 01325 01326 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 01327 BasicBlock *Block = unwrap(BB); 01328 BasicBlock::iterator I = Block->end(); 01329 if (I == Block->begin()) 01330 return 0; 01331 return wrap(--I); 01332 } 01333 01334 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 01335 Instruction *Instr = unwrap<Instruction>(Inst); 01336 BasicBlock::iterator I = Instr; 01337 if (++I == Instr->getParent()->end()) 01338 return 0; 01339 return wrap(I); 01340 } 01341 01342 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 01343 Instruction *Instr = unwrap<Instruction>(Inst); 01344 BasicBlock::iterator I = Instr; 01345 if (I == Instr->getParent()->begin()) 01346 return 0; 01347 return wrap(--I); 01348 } 01349 01350 /*--.. Call and invoke instructions ........................................--*/ 01351 01352 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 01353 Value *V = unwrap(Instr); 01354 if (CallInst *CI = dyn_cast<CallInst>(V)) 01355 return CI->getCallingConv(); 01356 else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) 01357 return II->getCallingConv(); 01358 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!"); 01359 return 0; 01360 } 01361 01362 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 01363 Value *V = unwrap(Instr); 01364 if (CallInst *CI = dyn_cast<CallInst>(V)) 01365 return CI->setCallingConv(CC); 01366 else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) 01367 return II->setCallingConv(CC); 01368 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!"); 01369 } 01370 01371 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 01372 LLVMAttribute PA) { 01373 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 01374 Call.setAttributes( 01375 Call.getAttributes().addAttr(index, PA)); 01376 } 01377 01378 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 01379 LLVMAttribute PA) { 01380 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 01381 Call.setAttributes( 01382 Call.getAttributes().removeAttr(index, PA)); 01383 } 01384 01385 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 01386 unsigned align) { 01387 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 01388 Call.setAttributes( 01389 Call.getAttributes().addAttr(index, 01390 Attribute::constructAlignmentFromInt(align))); 01391 } 01392 01393 /*--.. Operations on call instructions (only) ..............................--*/ 01394 01395 int LLVMIsTailCall(LLVMValueRef Call) { 01396 return unwrap<CallInst>(Call)->isTailCall(); 01397 } 01398 01399 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) { 01400 unwrap<CallInst>(Call)->setTailCall(isTailCall); 01401 } 01402 01403 /*--.. Operations on phi nodes .............................................--*/ 01404 01405 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 01406 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 01407 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 01408 for (unsigned I = 0; I != Count; ++I) 01409 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 01410 } 01411 01412 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 01413 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 01414 } 01415 01416 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 01417 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 01418 } 01419 01420 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 01421 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 01422 } 01423 01424 01425 /*===-- Instruction builders ----------------------------------------------===*/ 01426 01427 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 01428 return wrap(new IRBuilder<>(*unwrap(C))); 01429 } 01430 01431 LLVMBuilderRef LLVMCreateBuilder(void) { 01432 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 01433 } 01434 01435 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 01436 LLVMValueRef Instr) { 01437 BasicBlock *BB = unwrap(Block); 01438 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end(); 01439 unwrap(Builder)->SetInsertPoint(BB, I); 01440 } 01441 01442 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 01443 Instruction *I = unwrap<Instruction>(Instr); 01444 unwrap(Builder)->SetInsertPoint(I->getParent(), I); 01445 } 01446 01447 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 01448 BasicBlock *BB = unwrap(Block); 01449 unwrap(Builder)->SetInsertPoint(BB); 01450 } 01451 01452 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 01453 return wrap(unwrap(Builder)->GetInsertBlock()); 01454 } 01455 01456 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 01457 unwrap(Builder)->ClearInsertionPoint (); 01458 } 01459 01460 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 01461 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 01462 } 01463 01464 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 01465 const char *Name) { 01466 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 01467 } 01468 01469 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 01470 delete unwrap(Builder); 01471 } 01472 01473 /*--.. Instruction builders ................................................--*/ 01474 01475 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 01476 return wrap(unwrap(B)->CreateRetVoid()); 01477 } 01478 01479 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 01480 return wrap(unwrap(B)->CreateRet(unwrap(V))); 01481 } 01482 01483 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 01484 unsigned N) { 01485 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 01486 } 01487 01488 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 01489 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 01490 } 01491 01492 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 01493 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 01494 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 01495 } 01496 01497 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 01498 LLVMBasicBlockRef Else, unsigned NumCases) { 01499 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 01500 } 01501 01502 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 01503 LLVMValueRef *Args, unsigned NumArgs, 01504 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 01505 const char *Name) { 01506 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 01507 unwrap(Args), unwrap(Args) + NumArgs, 01508 Name)); 01509 } 01510 01511 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) { 01512 return wrap(unwrap(B)->CreateUnwind()); 01513 } 01514 01515 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 01516 return wrap(unwrap(B)->CreateUnreachable()); 01517 } 01518 01519 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 01520 LLVMBasicBlockRef Dest) { 01521 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 01522 } 01523 01524 /*--.. Arithmetic ..........................................................--*/ 01525 01526 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01527 const char *Name) { 01528 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 01529 } 01530 01531 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01532 const char *Name) { 01533 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 01534 } 01535 01536 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01537 const char *Name) { 01538 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 01539 } 01540 01541 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01542 const char *Name) { 01543 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 01544 } 01545 01546 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01547 const char *Name) { 01548 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 01549 } 01550 01551 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01552 const char *Name) { 01553 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 01554 } 01555 01556 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01557 const char *Name) { 01558 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 01559 } 01560 01561 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01562 const char *Name) { 01563 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 01564 } 01565 01566 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01567 const char *Name) { 01568 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 01569 } 01570 01571 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 01572 LLVMValueRef RHS, const char *Name) { 01573 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 01574 } 01575 01576 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01577 const char *Name) { 01578 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 01579 } 01580 01581 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01582 const char *Name) { 01583 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 01584 } 01585 01586 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01587 const char *Name) { 01588 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 01589 } 01590 01591 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01592 const char *Name) { 01593 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 01594 } 01595 01596 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01597 const char *Name) { 01598 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 01599 } 01600 01601 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01602 const char *Name) { 01603 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 01604 } 01605 01606 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01607 const char *Name) { 01608 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 01609 } 01610 01611 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01612 const char *Name) { 01613 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 01614 } 01615 01616 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01617 const char *Name) { 01618 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 01619 } 01620 01621 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01622 const char *Name) { 01623 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 01624 } 01625 01626 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 01627 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 01628 } 01629 01630 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 01631 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 01632 } 01633 01634 /*--.. Memory ..............................................................--*/ 01635 01636 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 01637 const char *Name) { 01638 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name)); 01639 } 01640 01641 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 01642 LLVMValueRef Val, const char *Name) { 01643 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name)); 01644 } 01645 01646 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 01647 const char *Name) { 01648 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name)); 01649 } 01650 01651 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 01652 LLVMValueRef Val, const char *Name) { 01653 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 01654 } 01655 01656 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 01657 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal))); 01658 } 01659 01660 01661 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 01662 const char *Name) { 01663 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 01664 } 01665 01666 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 01667 LLVMValueRef PointerVal) { 01668 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 01669 } 01670 01671 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 01672 LLVMValueRef *Indices, unsigned NumIndices, 01673 const char *Name) { 01674 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices), 01675 unwrap(Indices) + NumIndices, Name)); 01676 } 01677 01678 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 01679 LLVMValueRef *Indices, unsigned NumIndices, 01680 const char *Name) { 01681 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices), 01682 unwrap(Indices) + NumIndices, Name)); 01683 } 01684 01685 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 01686 unsigned Idx, const char *Name) { 01687 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name)); 01688 } 01689 01690 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 01691 const char *Name) { 01692 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 01693 } 01694 01695 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 01696 const char *Name) { 01697 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 01698 } 01699 01700 /*--.. Casts ...............................................................--*/ 01701 01702 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 01703 LLVMTypeRef DestTy, const char *Name) { 01704 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 01705 } 01706 01707 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 01708 LLVMTypeRef DestTy, const char *Name) { 01709 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 01710 } 01711 01712 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 01713 LLVMTypeRef DestTy, const char *Name) { 01714 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 01715 } 01716 01717 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 01718 LLVMTypeRef DestTy, const char *Name) { 01719 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 01720 } 01721 01722 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 01723 LLVMTypeRef DestTy, const char *Name) { 01724 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 01725 } 01726 01727 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 01728 LLVMTypeRef DestTy, const char *Name) { 01729 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 01730 } 01731 01732 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 01733 LLVMTypeRef DestTy, const char *Name) { 01734 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 01735 } 01736 01737 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 01738 LLVMTypeRef DestTy, const char *Name) { 01739 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 01740 } 01741 01742 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 01743 LLVMTypeRef DestTy, const char *Name) { 01744 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 01745 } 01746 01747 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 01748 LLVMTypeRef DestTy, const char *Name) { 01749 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 01750 } 01751 01752 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 01753 LLVMTypeRef DestTy, const char *Name) { 01754 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 01755 } 01756 01757 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 01758 LLVMTypeRef DestTy, const char *Name) { 01759 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 01760 } 01761 01762 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 01763 LLVMTypeRef DestTy, const char *Name) { 01764 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 01765 Name)); 01766 } 01767 01768 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 01769 LLVMTypeRef DestTy, const char *Name) { 01770 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 01771 Name)); 01772 } 01773 01774 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 01775 LLVMTypeRef DestTy, const char *Name) { 01776 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 01777 Name)); 01778 } 01779 01780 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 01781 LLVMTypeRef DestTy, const char *Name) { 01782 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 01783 } 01784 01785 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 01786 LLVMTypeRef DestTy, const char *Name) { 01787 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), Name)); 01788 } 01789 01790 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 01791 LLVMTypeRef DestTy, const char *Name) { 01792 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 01793 } 01794 01795 /*--.. Comparisons .........................................................--*/ 01796 01797 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 01798 LLVMValueRef LHS, LLVMValueRef RHS, 01799 const char *Name) { 01800 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 01801 unwrap(LHS), unwrap(RHS), Name)); 01802 } 01803 01804 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 01805 LLVMValueRef LHS, LLVMValueRef RHS, 01806 const char *Name) { 01807 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 01808 unwrap(LHS), unwrap(RHS), Name)); 01809 } 01810 01811 /*--.. Miscellaneous instructions ..........................................--*/ 01812 01813 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 01814 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name)); 01815 } 01816 01817 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 01818 LLVMValueRef *Args, unsigned NumArgs, 01819 const char *Name) { 01820 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args), 01821 unwrap(Args) + NumArgs, Name)); 01822 } 01823 01824 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 01825 LLVMValueRef Then, LLVMValueRef Else, 01826 const char *Name) { 01827 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 01828 Name)); 01829 } 01830 01831 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 01832 LLVMTypeRef Ty, const char *Name) { 01833 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 01834 } 01835 01836 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 01837 LLVMValueRef Index, const char *Name) { 01838 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 01839 Name)); 01840 } 01841 01842 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 01843 LLVMValueRef EltVal, LLVMValueRef Index, 01844 const char *Name) { 01845 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 01846 unwrap(Index), Name)); 01847 } 01848 01849 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 01850 LLVMValueRef V2, LLVMValueRef Mask, 01851 const char *Name) { 01852 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 01853 unwrap(Mask), Name)); 01854 } 01855 01856 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 01857 unsigned Index, const char *Name) { 01858 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 01859 } 01860 01861 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 01862 LLVMValueRef EltVal, unsigned Index, 01863 const char *Name) { 01864 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 01865 Index, Name)); 01866 } 01867 01868 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 01869 const char *Name) { 01870 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 01871 } 01872 01873 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 01874 const char *Name) { 01875 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 01876 } 01877 01878 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 01879 LLVMValueRef RHS, const char *Name) { 01880 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 01881 } 01882 01883 01884 /*===-- Module providers --------------------------------------------------===*/ 01885 01886 LLVMModuleProviderRef 01887 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 01888 return wrap(new ExistingModuleProvider(unwrap(M))); 01889 } 01890 01891 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 01892 delete unwrap(MP); 01893 } 01894 01895 01896 /*===-- Memory buffers ----------------------------------------------------===*/ 01897 01898 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path, 01899 LLVMMemoryBufferRef *OutMemBuf, 01900 char **OutMessage) { 01901 std::string Error; 01902 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) { 01903 *OutMemBuf = wrap(MB); 01904 return 0; 01905 } 01906 01907 *OutMessage = strdup(Error.c_str()); 01908 return 1; 01909 } 01910 01911 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 01912 char **OutMessage) { 01913 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) { 01914 *OutMemBuf = wrap(MB); 01915 return 0; 01916 } 01917 01918 *OutMessage = strdup("stdin is empty."); 01919 return 1; 01920 } 01921 01922 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 01923 delete unwrap(MemBuf); 01924 }