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 common infrastructure (including the C bindings) 00011 // for libLLVMCore.a, which implements the LLVM intermediate representation. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm-c/Core.h" 00016 #include "llvm/Bitcode/ReaderWriter.h" 00017 #include "llvm/IR/Attributes.h" 00018 #include "llvm/IR/Constants.h" 00019 #include "llvm/IR/DerivedTypes.h" 00020 #include "llvm/IR/GlobalAlias.h" 00021 #include "llvm/IR/GlobalVariable.h" 00022 #include "llvm/IR/InlineAsm.h" 00023 #include "llvm/IR/IntrinsicInst.h" 00024 #include "llvm/IR/IRBuilder.h" 00025 #include "llvm/IR/LLVMContext.h" 00026 #include "llvm/IR/Module.h" 00027 #include "llvm/PassManager.h" 00028 #include "llvm/Support/CallSite.h" 00029 #include "llvm/Support/Debug.h" 00030 #include "llvm/Support/ErrorHandling.h" 00031 #include "llvm/Support/ManagedStatic.h" 00032 #include "llvm/Support/MemoryBuffer.h" 00033 #include "llvm/Support/raw_ostream.h" 00034 #include "llvm/Support/system_error.h" 00035 #include "llvm/Support/Threading.h" 00036 #include <cassert> 00037 #include <cstdlib> 00038 #include <cstring> 00039 00040 using namespace llvm; 00041 00042 void llvm::initializeCore(PassRegistry &Registry) { 00043 initializeDominatorTreePass(Registry); 00044 initializePrintModulePassPass(Registry); 00045 initializePrintFunctionPassPass(Registry); 00046 initializePrintBasicBlockPassPass(Registry); 00047 initializeVerifierPass(Registry); 00048 initializePreVerifierPass(Registry); 00049 } 00050 00051 void LLVMInitializeCore(LLVMPassRegistryRef R) { 00052 initializeCore(*unwrap(R)); 00053 } 00054 00055 void LLVMShutdown() { 00056 llvm_shutdown(); 00057 } 00058 00059 /*===-- Error handling ----------------------------------------------------===*/ 00060 00061 char *LLVMCreateMessage(const char *Message) { 00062 return strdup(Message); 00063 } 00064 00065 void LLVMDisposeMessage(char *Message) { 00066 free(Message); 00067 } 00068 00069 00070 /*===-- Operations on contexts --------------------------------------------===*/ 00071 00072 LLVMContextRef LLVMContextCreate() { 00073 return wrap(new LLVMContext()); 00074 } 00075 00076 LLVMContextRef LLVMGetGlobalContext() { 00077 return wrap(&getGlobalContext()); 00078 } 00079 00080 void LLVMContextDispose(LLVMContextRef C) { 00081 delete unwrap(C); 00082 } 00083 00084 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name, 00085 unsigned SLen) { 00086 return unwrap(C)->getMDKindID(StringRef(Name, SLen)); 00087 } 00088 00089 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) { 00090 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen); 00091 } 00092 00093 00094 /*===-- Operations on modules ---------------------------------------------===*/ 00095 00096 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) { 00097 return wrap(new Module(ModuleID, getGlobalContext())); 00098 } 00099 00100 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 00101 LLVMContextRef C) { 00102 return wrap(new Module(ModuleID, *unwrap(C))); 00103 } 00104 00105 void LLVMDisposeModule(LLVMModuleRef M) { 00106 delete unwrap(M); 00107 } 00108 00109 /*--.. Data layout .........................................................--*/ 00110 const char * LLVMGetDataLayout(LLVMModuleRef M) { 00111 return unwrap(M)->getDataLayout().c_str(); 00112 } 00113 00114 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) { 00115 unwrap(M)->setDataLayout(Triple); 00116 } 00117 00118 /*--.. Target triple .......................................................--*/ 00119 const char * LLVMGetTarget(LLVMModuleRef M) { 00120 return unwrap(M)->getTargetTriple().c_str(); 00121 } 00122 00123 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) { 00124 unwrap(M)->setTargetTriple(Triple); 00125 } 00126 00127 void LLVMDumpModule(LLVMModuleRef M) { 00128 unwrap(M)->dump(); 00129 } 00130 00131 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, 00132 char **ErrorMessage) { 00133 std::string error; 00134 raw_fd_ostream dest(Filename, error); 00135 if (!error.empty()) { 00136 *ErrorMessage = strdup(error.c_str()); 00137 return true; 00138 } 00139 00140 unwrap(M)->print(dest, NULL); 00141 00142 if (!error.empty()) { 00143 *ErrorMessage = strdup(error.c_str()); 00144 return true; 00145 } 00146 dest.flush(); 00147 return false; 00148 } 00149 00150 /*--.. Operations on inline assembler ......................................--*/ 00151 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) { 00152 unwrap(M)->setModuleInlineAsm(StringRef(Asm)); 00153 } 00154 00155 00156 /*--.. Operations on module contexts ......................................--*/ 00157 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) { 00158 return wrap(&unwrap(M)->getContext()); 00159 } 00160 00161 00162 /*===-- Operations on types -----------------------------------------------===*/ 00163 00164 /*--.. Operations on all types (mostly) ....................................--*/ 00165 00166 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) { 00167 switch (unwrap(Ty)->getTypeID()) { 00168 default: llvm_unreachable("Unhandled TypeID."); 00169 case Type::VoidTyID: 00170 return LLVMVoidTypeKind; 00171 case Type::HalfTyID: 00172 return LLVMHalfTypeKind; 00173 case Type::FloatTyID: 00174 return LLVMFloatTypeKind; 00175 case Type::DoubleTyID: 00176 return LLVMDoubleTypeKind; 00177 case Type::X86_FP80TyID: 00178 return LLVMX86_FP80TypeKind; 00179 case Type::FP128TyID: 00180 return LLVMFP128TypeKind; 00181 case Type::PPC_FP128TyID: 00182 return LLVMPPC_FP128TypeKind; 00183 case Type::LabelTyID: 00184 return LLVMLabelTypeKind; 00185 case Type::MetadataTyID: 00186 return LLVMMetadataTypeKind; 00187 case Type::IntegerTyID: 00188 return LLVMIntegerTypeKind; 00189 case Type::FunctionTyID: 00190 return LLVMFunctionTypeKind; 00191 case Type::StructTyID: 00192 return LLVMStructTypeKind; 00193 case Type::ArrayTyID: 00194 return LLVMArrayTypeKind; 00195 case Type::PointerTyID: 00196 return LLVMPointerTypeKind; 00197 case Type::VectorTyID: 00198 return LLVMVectorTypeKind; 00199 case Type::X86_MMXTyID: 00200 return LLVMX86_MMXTypeKind; 00201 } 00202 } 00203 00204 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty) 00205 { 00206 return unwrap(Ty)->isSized(); 00207 } 00208 00209 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) { 00210 return wrap(&unwrap(Ty)->getContext()); 00211 } 00212 00213 /*--.. Operations on integer types .........................................--*/ 00214 00215 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) { 00216 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C)); 00217 } 00218 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) { 00219 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C)); 00220 } 00221 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) { 00222 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C)); 00223 } 00224 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) { 00225 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C)); 00226 } 00227 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) { 00228 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C)); 00229 } 00230 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) { 00231 return wrap(IntegerType::get(*unwrap(C), NumBits)); 00232 } 00233 00234 LLVMTypeRef LLVMInt1Type(void) { 00235 return LLVMInt1TypeInContext(LLVMGetGlobalContext()); 00236 } 00237 LLVMTypeRef LLVMInt8Type(void) { 00238 return LLVMInt8TypeInContext(LLVMGetGlobalContext()); 00239 } 00240 LLVMTypeRef LLVMInt16Type(void) { 00241 return LLVMInt16TypeInContext(LLVMGetGlobalContext()); 00242 } 00243 LLVMTypeRef LLVMInt32Type(void) { 00244 return LLVMInt32TypeInContext(LLVMGetGlobalContext()); 00245 } 00246 LLVMTypeRef LLVMInt64Type(void) { 00247 return LLVMInt64TypeInContext(LLVMGetGlobalContext()); 00248 } 00249 LLVMTypeRef LLVMIntType(unsigned NumBits) { 00250 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits); 00251 } 00252 00253 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) { 00254 return unwrap<IntegerType>(IntegerTy)->getBitWidth(); 00255 } 00256 00257 /*--.. Operations on real types ............................................--*/ 00258 00259 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) { 00260 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C)); 00261 } 00262 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) { 00263 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C)); 00264 } 00265 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) { 00266 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C)); 00267 } 00268 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) { 00269 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C)); 00270 } 00271 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) { 00272 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C)); 00273 } 00274 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) { 00275 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C)); 00276 } 00277 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) { 00278 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C)); 00279 } 00280 00281 LLVMTypeRef LLVMHalfType(void) { 00282 return LLVMHalfTypeInContext(LLVMGetGlobalContext()); 00283 } 00284 LLVMTypeRef LLVMFloatType(void) { 00285 return LLVMFloatTypeInContext(LLVMGetGlobalContext()); 00286 } 00287 LLVMTypeRef LLVMDoubleType(void) { 00288 return LLVMDoubleTypeInContext(LLVMGetGlobalContext()); 00289 } 00290 LLVMTypeRef LLVMX86FP80Type(void) { 00291 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext()); 00292 } 00293 LLVMTypeRef LLVMFP128Type(void) { 00294 return LLVMFP128TypeInContext(LLVMGetGlobalContext()); 00295 } 00296 LLVMTypeRef LLVMPPCFP128Type(void) { 00297 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext()); 00298 } 00299 LLVMTypeRef LLVMX86MMXType(void) { 00300 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext()); 00301 } 00302 00303 /*--.. Operations on function types ........................................--*/ 00304 00305 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType, 00306 LLVMTypeRef *ParamTypes, unsigned ParamCount, 00307 LLVMBool IsVarArg) { 00308 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 00309 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0)); 00310 } 00311 00312 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) { 00313 return unwrap<FunctionType>(FunctionTy)->isVarArg(); 00314 } 00315 00316 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) { 00317 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType()); 00318 } 00319 00320 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) { 00321 return unwrap<FunctionType>(FunctionTy)->getNumParams(); 00322 } 00323 00324 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) { 00325 FunctionType *Ty = unwrap<FunctionType>(FunctionTy); 00326 for (FunctionType::param_iterator I = Ty->param_begin(), 00327 E = Ty->param_end(); I != E; ++I) 00328 *Dest++ = wrap(*I); 00329 } 00330 00331 /*--.. Operations on struct types ..........................................--*/ 00332 00333 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes, 00334 unsigned ElementCount, LLVMBool Packed) { 00335 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 00336 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0)); 00337 } 00338 00339 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, 00340 unsigned ElementCount, LLVMBool Packed) { 00341 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes, 00342 ElementCount, Packed); 00343 } 00344 00345 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name) 00346 { 00347 return wrap(StructType::create(*unwrap(C), Name)); 00348 } 00349 00350 const char *LLVMGetStructName(LLVMTypeRef Ty) 00351 { 00352 StructType *Type = unwrap<StructType>(Ty); 00353 if (!Type->hasName()) 00354 return 0; 00355 return Type->getName().data(); 00356 } 00357 00358 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes, 00359 unsigned ElementCount, LLVMBool Packed) { 00360 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 00361 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0); 00362 } 00363 00364 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) { 00365 return unwrap<StructType>(StructTy)->getNumElements(); 00366 } 00367 00368 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) { 00369 StructType *Ty = unwrap<StructType>(StructTy); 00370 for (StructType::element_iterator I = Ty->element_begin(), 00371 E = Ty->element_end(); I != E; ++I) 00372 *Dest++ = wrap(*I); 00373 } 00374 00375 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) { 00376 return unwrap<StructType>(StructTy)->isPacked(); 00377 } 00378 00379 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) { 00380 return unwrap<StructType>(StructTy)->isOpaque(); 00381 } 00382 00383 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { 00384 return wrap(unwrap(M)->getTypeByName(Name)); 00385 } 00386 00387 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ 00388 00389 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { 00390 return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); 00391 } 00392 00393 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) { 00394 return wrap(PointerType::get(unwrap(ElementType), AddressSpace)); 00395 } 00396 00397 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) { 00398 return wrap(VectorType::get(unwrap(ElementType), ElementCount)); 00399 } 00400 00401 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) { 00402 return wrap(unwrap<SequentialType>(Ty)->getElementType()); 00403 } 00404 00405 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { 00406 return unwrap<ArrayType>(ArrayTy)->getNumElements(); 00407 } 00408 00409 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) { 00410 return unwrap<PointerType>(PointerTy)->getAddressSpace(); 00411 } 00412 00413 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) { 00414 return unwrap<VectorType>(VectorTy)->getNumElements(); 00415 } 00416 00417 /*--.. Operations on other types ...........................................--*/ 00418 00419 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) { 00420 return wrap(Type::getVoidTy(*unwrap(C))); 00421 } 00422 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) { 00423 return wrap(Type::getLabelTy(*unwrap(C))); 00424 } 00425 00426 LLVMTypeRef LLVMVoidType(void) { 00427 return LLVMVoidTypeInContext(LLVMGetGlobalContext()); 00428 } 00429 LLVMTypeRef LLVMLabelType(void) { 00430 return LLVMLabelTypeInContext(LLVMGetGlobalContext()); 00431 } 00432 00433 /*===-- Operations on values ----------------------------------------------===*/ 00434 00435 /*--.. Operations on all values ............................................--*/ 00436 00437 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) { 00438 return wrap(unwrap(Val)->getType()); 00439 } 00440 00441 const char *LLVMGetValueName(LLVMValueRef Val) { 00442 return unwrap(Val)->getName().data(); 00443 } 00444 00445 void LLVMSetValueName(LLVMValueRef Val, const char *Name) { 00446 unwrap(Val)->setName(Name); 00447 } 00448 00449 void LLVMDumpValue(LLVMValueRef Val) { 00450 unwrap(Val)->dump(); 00451 } 00452 00453 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) { 00454 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal)); 00455 } 00456 00457 int LLVMHasMetadata(LLVMValueRef Inst) { 00458 return unwrap<Instruction>(Inst)->hasMetadata(); 00459 } 00460 00461 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { 00462 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID)); 00463 } 00464 00465 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) { 00466 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL); 00467 } 00468 00469 /*--.. Conversion functions ................................................--*/ 00470 00471 #define LLVM_DEFINE_VALUE_CAST(name) \ 00472 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \ 00473 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \ 00474 } 00475 00476 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) 00477 00478 /*--.. Operations on Uses ..................................................--*/ 00479 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { 00480 Value *V = unwrap(Val); 00481 Value::use_iterator I = V->use_begin(); 00482 if (I == V->use_end()) 00483 return 0; 00484 return wrap(&(I.getUse())); 00485 } 00486 00487 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) { 00488 Use *Next = unwrap(U)->getNext(); 00489 if (Next) 00490 return wrap(Next); 00491 return 0; 00492 } 00493 00494 LLVMValueRef LLVMGetUser(LLVMUseRef U) { 00495 return wrap(unwrap(U)->getUser()); 00496 } 00497 00498 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) { 00499 return wrap(unwrap(U)->get()); 00500 } 00501 00502 /*--.. Operations on Users .................................................--*/ 00503 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { 00504 Value *V = unwrap(Val); 00505 if (MDNode *MD = dyn_cast<MDNode>(V)) 00506 return wrap(MD->getOperand(Index)); 00507 return wrap(cast<User>(V)->getOperand(Index)); 00508 } 00509 00510 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) { 00511 unwrap<User>(Val)->setOperand(Index, unwrap(Op)); 00512 } 00513 00514 int LLVMGetNumOperands(LLVMValueRef Val) { 00515 Value *V = unwrap(Val); 00516 if (MDNode *MD = dyn_cast<MDNode>(V)) 00517 return MD->getNumOperands(); 00518 return cast<User>(V)->getNumOperands(); 00519 } 00520 00521 /*--.. Operations on constants of any type .................................--*/ 00522 00523 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) { 00524 return wrap(Constant::getNullValue(unwrap(Ty))); 00525 } 00526 00527 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) { 00528 return wrap(Constant::getAllOnesValue(unwrap(Ty))); 00529 } 00530 00531 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) { 00532 return wrap(UndefValue::get(unwrap(Ty))); 00533 } 00534 00535 LLVMBool LLVMIsConstant(LLVMValueRef Ty) { 00536 return isa<Constant>(unwrap(Ty)); 00537 } 00538 00539 LLVMBool LLVMIsNull(LLVMValueRef Val) { 00540 if (Constant *C = dyn_cast<Constant>(unwrap(Val))) 00541 return C->isNullValue(); 00542 return false; 00543 } 00544 00545 LLVMBool LLVMIsUndef(LLVMValueRef Val) { 00546 return isa<UndefValue>(unwrap(Val)); 00547 } 00548 00549 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { 00550 return 00551 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty))); 00552 } 00553 00554 /*--.. Operations on metadata nodes ........................................--*/ 00555 00556 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, 00557 unsigned SLen) { 00558 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); 00559 } 00560 00561 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { 00562 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen); 00563 } 00564 00565 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, 00566 unsigned Count) { 00567 return wrap(MDNode::get(*unwrap(C), 00568 makeArrayRef(unwrap<Value>(Vals, Count), Count))); 00569 } 00570 00571 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { 00572 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count); 00573 } 00574 00575 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) { 00576 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) { 00577 *Len = S->getString().size(); 00578 return S->getString().data(); 00579 } 00580 *Len = 0; 00581 return 0; 00582 } 00583 00584 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) 00585 { 00586 return cast<MDNode>(unwrap(V))->getNumOperands(); 00587 } 00588 00589 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) 00590 { 00591 const MDNode *N = cast<MDNode>(unwrap(V)); 00592 const unsigned numOperands = N->getNumOperands(); 00593 for (unsigned i = 0; i < numOperands; i++) 00594 Dest[i] = wrap(N->getOperand(i)); 00595 } 00596 00597 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name) 00598 { 00599 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) { 00600 return N->getNumOperands(); 00601 } 00602 return 0; 00603 } 00604 00605 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest) 00606 { 00607 NamedMDNode *N = unwrap(M)->getNamedMetadata(name); 00608 if (!N) 00609 return; 00610 for (unsigned i=0;i<N->getNumOperands();i++) 00611 Dest[i] = wrap(N->getOperand(i)); 00612 } 00613 00614 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, 00615 LLVMValueRef Val) 00616 { 00617 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); 00618 if (!N) 00619 return; 00620 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL; 00621 if (Op) 00622 N->addOperand(Op); 00623 } 00624 00625 /*--.. Operations on scalar constants ......................................--*/ 00626 00627 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, 00628 LLVMBool SignExtend) { 00629 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0)); 00630 } 00631 00632 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy, 00633 unsigned NumWords, 00634 const uint64_t Words[]) { 00635 IntegerType *Ty = unwrap<IntegerType>(IntTy); 00636 return wrap(ConstantInt::get(Ty->getContext(), 00637 APInt(Ty->getBitWidth(), 00638 makeArrayRef(Words, NumWords)))); 00639 } 00640 00641 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[], 00642 uint8_t Radix) { 00643 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str), 00644 Radix)); 00645 } 00646 00647 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[], 00648 unsigned SLen, uint8_t Radix) { 00649 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen), 00650 Radix)); 00651 } 00652 00653 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { 00654 return wrap(ConstantFP::get(unwrap(RealTy), N)); 00655 } 00656 00657 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { 00658 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text))); 00659 } 00660 00661 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[], 00662 unsigned SLen) { 00663 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen))); 00664 } 00665 00666 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { 00667 return unwrap<ConstantInt>(ConstantVal)->getZExtValue(); 00668 } 00669 00670 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { 00671 return unwrap<ConstantInt>(ConstantVal)->getSExtValue(); 00672 } 00673 00674 /*--.. Operations on composite constants ...................................--*/ 00675 00676 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, 00677 unsigned Length, 00678 LLVMBool DontNullTerminate) { 00679 /* Inverted the sense of AddNull because ', 0)' is a 00680 better mnemonic for null termination than ', 1)'. */ 00681 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), 00682 DontNullTerminate == 0)); 00683 } 00684 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 00685 LLVMValueRef *ConstantVals, 00686 unsigned Count, LLVMBool Packed) { 00687 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 00688 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count), 00689 Packed != 0)); 00690 } 00691 00692 LLVMValueRef LLVMConstString(const char *Str, unsigned Length, 00693 LLVMBool DontNullTerminate) { 00694 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length, 00695 DontNullTerminate); 00696 } 00697 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, 00698 LLVMValueRef *ConstantVals, unsigned Length) { 00699 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length); 00700 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V)); 00701 } 00702 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, 00703 LLVMBool Packed) { 00704 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count, 00705 Packed); 00706 } 00707 00708 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, 00709 LLVMValueRef *ConstantVals, 00710 unsigned Count) { 00711 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 00712 StructType *Ty = cast<StructType>(unwrap(StructTy)); 00713 00714 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count))); 00715 } 00716 00717 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) { 00718 return wrap(ConstantVector::get(makeArrayRef( 00719 unwrap<Constant>(ScalarConstantVals, Size), Size))); 00720 } 00721 00722 /*-- Opcode mapping */ 00723 00724 static LLVMOpcode map_to_llvmopcode(int opcode) 00725 { 00726 switch (opcode) { 00727 default: llvm_unreachable("Unhandled Opcode."); 00728 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc; 00729 #include "llvm/IR/Instruction.def" 00730 #undef HANDLE_INST 00731 } 00732 } 00733 00734 static int map_from_llvmopcode(LLVMOpcode code) 00735 { 00736 switch (code) { 00737 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num; 00738 #include "llvm/IR/Instruction.def" 00739 #undef HANDLE_INST 00740 } 00741 llvm_unreachable("Unhandled Opcode."); 00742 } 00743 00744 /*--.. Constant expressions ................................................--*/ 00745 00746 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) { 00747 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode()); 00748 } 00749 00750 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) { 00751 return wrap(ConstantExpr::getAlignOf(unwrap(Ty))); 00752 } 00753 00754 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) { 00755 return wrap(ConstantExpr::getSizeOf(unwrap(Ty))); 00756 } 00757 00758 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) { 00759 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal))); 00760 } 00761 00762 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) { 00763 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal))); 00764 } 00765 00766 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) { 00767 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal))); 00768 } 00769 00770 00771 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) { 00772 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal))); 00773 } 00774 00775 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) { 00776 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal))); 00777 } 00778 00779 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00780 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant), 00781 unwrap<Constant>(RHSConstant))); 00782 } 00783 00784 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, 00785 LLVMValueRef RHSConstant) { 00786 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant), 00787 unwrap<Constant>(RHSConstant))); 00788 } 00789 00790 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant, 00791 LLVMValueRef RHSConstant) { 00792 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant), 00793 unwrap<Constant>(RHSConstant))); 00794 } 00795 00796 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00797 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant), 00798 unwrap<Constant>(RHSConstant))); 00799 } 00800 00801 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00802 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant), 00803 unwrap<Constant>(RHSConstant))); 00804 } 00805 00806 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant, 00807 LLVMValueRef RHSConstant) { 00808 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant), 00809 unwrap<Constant>(RHSConstant))); 00810 } 00811 00812 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant, 00813 LLVMValueRef RHSConstant) { 00814 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant), 00815 unwrap<Constant>(RHSConstant))); 00816 } 00817 00818 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00819 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant), 00820 unwrap<Constant>(RHSConstant))); 00821 } 00822 00823 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00824 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant), 00825 unwrap<Constant>(RHSConstant))); 00826 } 00827 00828 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant, 00829 LLVMValueRef RHSConstant) { 00830 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant), 00831 unwrap<Constant>(RHSConstant))); 00832 } 00833 00834 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant, 00835 LLVMValueRef RHSConstant) { 00836 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant), 00837 unwrap<Constant>(RHSConstant))); 00838 } 00839 00840 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00841 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant), 00842 unwrap<Constant>(RHSConstant))); 00843 } 00844 00845 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00846 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant), 00847 unwrap<Constant>(RHSConstant))); 00848 } 00849 00850 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00851 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant), 00852 unwrap<Constant>(RHSConstant))); 00853 } 00854 00855 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, 00856 LLVMValueRef RHSConstant) { 00857 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant), 00858 unwrap<Constant>(RHSConstant))); 00859 } 00860 00861 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00862 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant), 00863 unwrap<Constant>(RHSConstant))); 00864 } 00865 00866 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00867 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant), 00868 unwrap<Constant>(RHSConstant))); 00869 } 00870 00871 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00872 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant), 00873 unwrap<Constant>(RHSConstant))); 00874 } 00875 00876 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00877 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant), 00878 unwrap<Constant>(RHSConstant))); 00879 } 00880 00881 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00882 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant), 00883 unwrap<Constant>(RHSConstant))); 00884 } 00885 00886 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00887 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant), 00888 unwrap<Constant>(RHSConstant))); 00889 } 00890 00891 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00892 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant), 00893 unwrap<Constant>(RHSConstant))); 00894 } 00895 00896 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, 00897 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00898 return wrap(ConstantExpr::getICmp(Predicate, 00899 unwrap<Constant>(LHSConstant), 00900 unwrap<Constant>(RHSConstant))); 00901 } 00902 00903 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, 00904 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00905 return wrap(ConstantExpr::getFCmp(Predicate, 00906 unwrap<Constant>(LHSConstant), 00907 unwrap<Constant>(RHSConstant))); 00908 } 00909 00910 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00911 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant), 00912 unwrap<Constant>(RHSConstant))); 00913 } 00914 00915 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00916 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant), 00917 unwrap<Constant>(RHSConstant))); 00918 } 00919 00920 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 00921 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant), 00922 unwrap<Constant>(RHSConstant))); 00923 } 00924 00925 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, 00926 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 00927 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 00928 NumIndices); 00929 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal), 00930 IdxList)); 00931 } 00932 00933 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 00934 LLVMValueRef *ConstantIndices, 00935 unsigned NumIndices) { 00936 Constant* Val = unwrap<Constant>(ConstantVal); 00937 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 00938 NumIndices); 00939 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList)); 00940 } 00941 00942 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00943 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal), 00944 unwrap(ToType))); 00945 } 00946 00947 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00948 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal), 00949 unwrap(ToType))); 00950 } 00951 00952 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00953 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal), 00954 unwrap(ToType))); 00955 } 00956 00957 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00958 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal), 00959 unwrap(ToType))); 00960 } 00961 00962 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00963 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal), 00964 unwrap(ToType))); 00965 } 00966 00967 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00968 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal), 00969 unwrap(ToType))); 00970 } 00971 00972 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00973 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 00974 unwrap(ToType))); 00975 } 00976 00977 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00978 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 00979 unwrap(ToType))); 00980 } 00981 00982 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00983 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal), 00984 unwrap(ToType))); 00985 } 00986 00987 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00988 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal), 00989 unwrap(ToType))); 00990 } 00991 00992 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00993 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal), 00994 unwrap(ToType))); 00995 } 00996 00997 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 00998 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal), 00999 unwrap(ToType))); 01000 } 01001 01002 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 01003 LLVMTypeRef ToType) { 01004 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal), 01005 unwrap(ToType))); 01006 } 01007 01008 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 01009 LLVMTypeRef ToType) { 01010 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal), 01011 unwrap(ToType))); 01012 } 01013 01014 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 01015 LLVMTypeRef ToType) { 01016 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal), 01017 unwrap(ToType))); 01018 } 01019 01020 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 01021 LLVMTypeRef ToType) { 01022 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal), 01023 unwrap(ToType))); 01024 } 01025 01026 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 01027 LLVMBool isSigned) { 01028 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal), 01029 unwrap(ToType), isSigned)); 01030 } 01031 01032 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 01033 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal), 01034 unwrap(ToType))); 01035 } 01036 01037 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 01038 LLVMValueRef ConstantIfTrue, 01039 LLVMValueRef ConstantIfFalse) { 01040 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition), 01041 unwrap<Constant>(ConstantIfTrue), 01042 unwrap<Constant>(ConstantIfFalse))); 01043 } 01044 01045 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 01046 LLVMValueRef IndexConstant) { 01047 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant), 01048 unwrap<Constant>(IndexConstant))); 01049 } 01050 01051 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 01052 LLVMValueRef ElementValueConstant, 01053 LLVMValueRef IndexConstant) { 01054 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant), 01055 unwrap<Constant>(ElementValueConstant), 01056 unwrap<Constant>(IndexConstant))); 01057 } 01058 01059 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 01060 LLVMValueRef VectorBConstant, 01061 LLVMValueRef MaskConstant) { 01062 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant), 01063 unwrap<Constant>(VectorBConstant), 01064 unwrap<Constant>(MaskConstant))); 01065 } 01066 01067 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 01068 unsigned NumIdx) { 01069 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant), 01070 makeArrayRef(IdxList, NumIdx))); 01071 } 01072 01073 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 01074 LLVMValueRef ElementValueConstant, 01075 unsigned *IdxList, unsigned NumIdx) { 01076 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant), 01077 unwrap<Constant>(ElementValueConstant), 01078 makeArrayRef(IdxList, NumIdx))); 01079 } 01080 01081 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 01082 const char *Constraints, 01083 LLVMBool HasSideEffects, 01084 LLVMBool IsAlignStack) { 01085 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 01086 Constraints, HasSideEffects, IsAlignStack)); 01087 } 01088 01089 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) { 01090 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB))); 01091 } 01092 01093 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 01094 01095 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 01096 return wrap(unwrap<GlobalValue>(Global)->getParent()); 01097 } 01098 01099 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { 01100 return unwrap<GlobalValue>(Global)->isDeclaration(); 01101 } 01102 01103 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 01104 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 01105 case GlobalValue::ExternalLinkage: 01106 return LLVMExternalLinkage; 01107 case GlobalValue::AvailableExternallyLinkage: 01108 return LLVMAvailableExternallyLinkage; 01109 case GlobalValue::LinkOnceAnyLinkage: 01110 return LLVMLinkOnceAnyLinkage; 01111 case GlobalValue::LinkOnceODRLinkage: 01112 return LLVMLinkOnceODRLinkage; 01113 case GlobalValue::LinkOnceODRAutoHideLinkage: 01114 return LLVMLinkOnceODRAutoHideLinkage; 01115 case GlobalValue::WeakAnyLinkage: 01116 return LLVMWeakAnyLinkage; 01117 case GlobalValue::WeakODRLinkage: 01118 return LLVMWeakODRLinkage; 01119 case GlobalValue::AppendingLinkage: 01120 return LLVMAppendingLinkage; 01121 case GlobalValue::InternalLinkage: 01122 return LLVMInternalLinkage; 01123 case GlobalValue::PrivateLinkage: 01124 return LLVMPrivateLinkage; 01125 case GlobalValue::LinkerPrivateLinkage: 01126 return LLVMLinkerPrivateLinkage; 01127 case GlobalValue::LinkerPrivateWeakLinkage: 01128 return LLVMLinkerPrivateWeakLinkage; 01129 case GlobalValue::DLLImportLinkage: 01130 return LLVMDLLImportLinkage; 01131 case GlobalValue::DLLExportLinkage: 01132 return LLVMDLLExportLinkage; 01133 case GlobalValue::ExternalWeakLinkage: 01134 return LLVMExternalWeakLinkage; 01135 case GlobalValue::CommonLinkage: 01136 return LLVMCommonLinkage; 01137 } 01138 01139 llvm_unreachable("Invalid GlobalValue linkage!"); 01140 } 01141 01142 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 01143 GlobalValue *GV = unwrap<GlobalValue>(Global); 01144 01145 switch (Linkage) { 01146 case LLVMExternalLinkage: 01147 GV->setLinkage(GlobalValue::ExternalLinkage); 01148 break; 01149 case LLVMAvailableExternallyLinkage: 01150 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 01151 break; 01152 case LLVMLinkOnceAnyLinkage: 01153 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 01154 break; 01155 case LLVMLinkOnceODRLinkage: 01156 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 01157 break; 01158 case LLVMLinkOnceODRAutoHideLinkage: 01159 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage); 01160 break; 01161 case LLVMWeakAnyLinkage: 01162 GV->setLinkage(GlobalValue::WeakAnyLinkage); 01163 break; 01164 case LLVMWeakODRLinkage: 01165 GV->setLinkage(GlobalValue::WeakODRLinkage); 01166 break; 01167 case LLVMAppendingLinkage: 01168 GV->setLinkage(GlobalValue::AppendingLinkage); 01169 break; 01170 case LLVMInternalLinkage: 01171 GV->setLinkage(GlobalValue::InternalLinkage); 01172 break; 01173 case LLVMPrivateLinkage: 01174 GV->setLinkage(GlobalValue::PrivateLinkage); 01175 break; 01176 case LLVMLinkerPrivateLinkage: 01177 GV->setLinkage(GlobalValue::LinkerPrivateLinkage); 01178 break; 01179 case LLVMLinkerPrivateWeakLinkage: 01180 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage); 01181 break; 01182 case LLVMDLLImportLinkage: 01183 GV->setLinkage(GlobalValue::DLLImportLinkage); 01184 break; 01185 case LLVMDLLExportLinkage: 01186 GV->setLinkage(GlobalValue::DLLExportLinkage); 01187 break; 01188 case LLVMExternalWeakLinkage: 01189 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 01190 break; 01191 case LLVMGhostLinkage: 01192 DEBUG(errs() 01193 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported."); 01194 break; 01195 case LLVMCommonLinkage: 01196 GV->setLinkage(GlobalValue::CommonLinkage); 01197 break; 01198 } 01199 } 01200 01201 const char *LLVMGetSection(LLVMValueRef Global) { 01202 return unwrap<GlobalValue>(Global)->getSection().c_str(); 01203 } 01204 01205 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 01206 unwrap<GlobalValue>(Global)->setSection(Section); 01207 } 01208 01209 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 01210 return static_cast<LLVMVisibility>( 01211 unwrap<GlobalValue>(Global)->getVisibility()); 01212 } 01213 01214 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 01215 unwrap<GlobalValue>(Global) 01216 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 01217 } 01218 01219 unsigned LLVMGetAlignment(LLVMValueRef Global) { 01220 return unwrap<GlobalValue>(Global)->getAlignment(); 01221 } 01222 01223 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) { 01224 unwrap<GlobalValue>(Global)->setAlignment(Bytes); 01225 } 01226 01227 /*--.. Operations on global variables ......................................--*/ 01228 01229 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 01230 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 01231 GlobalValue::ExternalLinkage, 0, Name)); 01232 } 01233 01234 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 01235 const char *Name, 01236 unsigned AddressSpace) { 01237 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 01238 GlobalValue::ExternalLinkage, 0, Name, 0, 01239 GlobalVariable::NotThreadLocal, AddressSpace)); 01240 } 01241 01242 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 01243 return wrap(unwrap(M)->getNamedGlobal(Name)); 01244 } 01245 01246 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 01247 Module *Mod = unwrap(M); 01248 Module::global_iterator I = Mod->global_begin(); 01249 if (I == Mod->global_end()) 01250 return 0; 01251 return wrap(I); 01252 } 01253 01254 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 01255 Module *Mod = unwrap(M); 01256 Module::global_iterator I = Mod->global_end(); 01257 if (I == Mod->global_begin()) 01258 return 0; 01259 return wrap(--I); 01260 } 01261 01262 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 01263 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 01264 Module::global_iterator I = GV; 01265 if (++I == GV->getParent()->global_end()) 01266 return 0; 01267 return wrap(I); 01268 } 01269 01270 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 01271 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 01272 Module::global_iterator I = GV; 01273 if (I == GV->getParent()->global_begin()) 01274 return 0; 01275 return wrap(--I); 01276 } 01277 01278 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 01279 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 01280 } 01281 01282 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 01283 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 01284 if ( !GV->hasInitializer() ) 01285 return 0; 01286 return wrap(GV->getInitializer()); 01287 } 01288 01289 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 01290 unwrap<GlobalVariable>(GlobalVar) 01291 ->setInitializer(unwrap<Constant>(ConstantVal)); 01292 } 01293 01294 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 01295 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 01296 } 01297 01298 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 01299 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 01300 } 01301 01302 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 01303 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 01304 } 01305 01306 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 01307 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 01308 } 01309 01310 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 01311 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 01312 case GlobalVariable::NotThreadLocal: 01313 return LLVMNotThreadLocal; 01314 case GlobalVariable::GeneralDynamicTLSModel: 01315 return LLVMGeneralDynamicTLSModel; 01316 case GlobalVariable::LocalDynamicTLSModel: 01317 return LLVMLocalDynamicTLSModel; 01318 case GlobalVariable::InitialExecTLSModel: 01319 return LLVMInitialExecTLSModel; 01320 case GlobalVariable::LocalExecTLSModel: 01321 return LLVMLocalExecTLSModel; 01322 } 01323 01324 llvm_unreachable("Invalid GlobalVariable thread local mode"); 01325 } 01326 01327 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 01328 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 01329 01330 switch (Mode) { 01331 case LLVMNotThreadLocal: 01332 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 01333 break; 01334 case LLVMGeneralDynamicTLSModel: 01335 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 01336 break; 01337 case LLVMLocalDynamicTLSModel: 01338 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 01339 break; 01340 case LLVMInitialExecTLSModel: 01341 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 01342 break; 01343 case LLVMLocalExecTLSModel: 01344 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 01345 break; 01346 } 01347 } 01348 01349 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 01350 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 01351 } 01352 01353 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 01354 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 01355 } 01356 01357 /*--.. Operations on aliases ......................................--*/ 01358 01359 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 01360 const char *Name) { 01361 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name, 01362 unwrap<Constant>(Aliasee), unwrap (M))); 01363 } 01364 01365 /*--.. Operations on functions .............................................--*/ 01366 01367 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 01368 LLVMTypeRef FunctionTy) { 01369 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 01370 GlobalValue::ExternalLinkage, Name, unwrap(M))); 01371 } 01372 01373 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 01374 return wrap(unwrap(M)->getFunction(Name)); 01375 } 01376 01377 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 01378 Module *Mod = unwrap(M); 01379 Module::iterator I = Mod->begin(); 01380 if (I == Mod->end()) 01381 return 0; 01382 return wrap(I); 01383 } 01384 01385 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 01386 Module *Mod = unwrap(M); 01387 Module::iterator I = Mod->end(); 01388 if (I == Mod->begin()) 01389 return 0; 01390 return wrap(--I); 01391 } 01392 01393 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 01394 Function *Func = unwrap<Function>(Fn); 01395 Module::iterator I = Func; 01396 if (++I == Func->getParent()->end()) 01397 return 0; 01398 return wrap(I); 01399 } 01400 01401 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 01402 Function *Func = unwrap<Function>(Fn); 01403 Module::iterator I = Func; 01404 if (I == Func->getParent()->begin()) 01405 return 0; 01406 return wrap(--I); 01407 } 01408 01409 void LLVMDeleteFunction(LLVMValueRef Fn) { 01410 unwrap<Function>(Fn)->eraseFromParent(); 01411 } 01412 01413 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 01414 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 01415 return F->getIntrinsicID(); 01416 return 0; 01417 } 01418 01419 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 01420 return unwrap<Function>(Fn)->getCallingConv(); 01421 } 01422 01423 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 01424 return unwrap<Function>(Fn)->setCallingConv( 01425 static_cast<CallingConv::ID>(CC)); 01426 } 01427 01428 const char *LLVMGetGC(LLVMValueRef Fn) { 01429 Function *F = unwrap<Function>(Fn); 01430 return F->hasGC()? F->getGC() : 0; 01431 } 01432 01433 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 01434 Function *F = unwrap<Function>(Fn); 01435 if (GC) 01436 F->setGC(GC); 01437 else 01438 F->clearGC(); 01439 } 01440 01441 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 01442 Function *Func = unwrap<Function>(Fn); 01443 const AttributeSet PAL = Func->getAttributes(); 01444 AttrBuilder B(PA); 01445 const AttributeSet PALnew = 01446 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex, 01447 AttributeSet::get(Func->getContext(), 01448 AttributeSet::FunctionIndex, B)); 01449 Func->setAttributes(PALnew); 01450 } 01451 01452 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 01453 const char *V) { 01454 Function *Func = unwrap<Function>(Fn); 01455 AttributeSet::AttrIndex Idx = 01456 AttributeSet::AttrIndex(AttributeSet::FunctionIndex); 01457 AttrBuilder B; 01458 01459 B.addAttribute(A, V); 01460 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B); 01461 Func->addAttributes(Idx, Set); 01462 } 01463 01464 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 01465 Function *Func = unwrap<Function>(Fn); 01466 const AttributeSet PAL = Func->getAttributes(); 01467 AttrBuilder B(PA); 01468 const AttributeSet PALnew = 01469 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex, 01470 AttributeSet::get(Func->getContext(), 01471 AttributeSet::FunctionIndex, B)); 01472 Func->setAttributes(PALnew); 01473 } 01474 01475 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) { 01476 Function *Func = unwrap<Function>(Fn); 01477 const AttributeSet PAL = Func->getAttributes(); 01478 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex); 01479 } 01480 01481 /*--.. Operations on parameters ............................................--*/ 01482 01483 unsigned LLVMCountParams(LLVMValueRef FnRef) { 01484 // This function is strictly redundant to 01485 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 01486 return unwrap<Function>(FnRef)->arg_size(); 01487 } 01488 01489 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 01490 Function *Fn = unwrap<Function>(FnRef); 01491 for (Function::arg_iterator I = Fn->arg_begin(), 01492 E = Fn->arg_end(); I != E; I++) 01493 *ParamRefs++ = wrap(I); 01494 } 01495 01496 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 01497 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin(); 01498 while (index --> 0) 01499 AI++; 01500 return wrap(AI); 01501 } 01502 01503 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 01504 return wrap(unwrap<Argument>(V)->getParent()); 01505 } 01506 01507 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 01508 Function *Func = unwrap<Function>(Fn); 01509 Function::arg_iterator I = Func->arg_begin(); 01510 if (I == Func->arg_end()) 01511 return 0; 01512 return wrap(I); 01513 } 01514 01515 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 01516 Function *Func = unwrap<Function>(Fn); 01517 Function::arg_iterator I = Func->arg_end(); 01518 if (I == Func->arg_begin()) 01519 return 0; 01520 return wrap(--I); 01521 } 01522 01523 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 01524 Argument *A = unwrap<Argument>(Arg); 01525 Function::arg_iterator I = A; 01526 if (++I == A->getParent()->arg_end()) 01527 return 0; 01528 return wrap(I); 01529 } 01530 01531 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 01532 Argument *A = unwrap<Argument>(Arg); 01533 Function::arg_iterator I = A; 01534 if (I == A->getParent()->arg_begin()) 01535 return 0; 01536 return wrap(--I); 01537 } 01538 01539 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 01540 Argument *A = unwrap<Argument>(Arg); 01541 AttrBuilder B(PA); 01542 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 01543 } 01544 01545 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 01546 Argument *A = unwrap<Argument>(Arg); 01547 AttrBuilder B(PA); 01548 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 01549 } 01550 01551 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { 01552 Argument *A = unwrap<Argument>(Arg); 01553 return (LLVMAttribute)A->getParent()->getAttributes(). 01554 Raw(A->getArgNo()+1); 01555 } 01556 01557 01558 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 01559 Argument *A = unwrap<Argument>(Arg); 01560 AttrBuilder B; 01561 B.addAlignmentAttr(align); 01562 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B)); 01563 } 01564 01565 /*--.. Operations on basic blocks ..........................................--*/ 01566 01567 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 01568 return wrap(static_cast<Value*>(unwrap(BB))); 01569 } 01570 01571 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 01572 return isa<BasicBlock>(unwrap(Val)); 01573 } 01574 01575 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 01576 return wrap(unwrap<BasicBlock>(Val)); 01577 } 01578 01579 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 01580 return wrap(unwrap(BB)->getParent()); 01581 } 01582 01583 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 01584 return wrap(unwrap(BB)->getTerminator()); 01585 } 01586 01587 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 01588 return unwrap<Function>(FnRef)->size(); 01589 } 01590 01591 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 01592 Function *Fn = unwrap<Function>(FnRef); 01593 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) 01594 *BasicBlocksRefs++ = wrap(I); 01595 } 01596 01597 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 01598 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 01599 } 01600 01601 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 01602 Function *Func = unwrap<Function>(Fn); 01603 Function::iterator I = Func->begin(); 01604 if (I == Func->end()) 01605 return 0; 01606 return wrap(I); 01607 } 01608 01609 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 01610 Function *Func = unwrap<Function>(Fn); 01611 Function::iterator I = Func->end(); 01612 if (I == Func->begin()) 01613 return 0; 01614 return wrap(--I); 01615 } 01616 01617 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 01618 BasicBlock *Block = unwrap(BB); 01619 Function::iterator I = Block; 01620 if (++I == Block->getParent()->end()) 01621 return 0; 01622 return wrap(I); 01623 } 01624 01625 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 01626 BasicBlock *Block = unwrap(BB); 01627 Function::iterator I = Block; 01628 if (I == Block->getParent()->begin()) 01629 return 0; 01630 return wrap(--I); 01631 } 01632 01633 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 01634 LLVMValueRef FnRef, 01635 const char *Name) { 01636 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 01637 } 01638 01639 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 01640 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 01641 } 01642 01643 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 01644 LLVMBasicBlockRef BBRef, 01645 const char *Name) { 01646 BasicBlock *BB = unwrap(BBRef); 01647 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 01648 } 01649 01650 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 01651 const char *Name) { 01652 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 01653 } 01654 01655 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 01656 unwrap(BBRef)->eraseFromParent(); 01657 } 01658 01659 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 01660 unwrap(BBRef)->removeFromParent(); 01661 } 01662 01663 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 01664 unwrap(BB)->moveBefore(unwrap(MovePos)); 01665 } 01666 01667 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 01668 unwrap(BB)->moveAfter(unwrap(MovePos)); 01669 } 01670 01671 /*--.. Operations on instructions ..........................................--*/ 01672 01673 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 01674 return wrap(unwrap<Instruction>(Inst)->getParent()); 01675 } 01676 01677 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 01678 BasicBlock *Block = unwrap(BB); 01679 BasicBlock::iterator I = Block->begin(); 01680 if (I == Block->end()) 01681 return 0; 01682 return wrap(I); 01683 } 01684 01685 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 01686 BasicBlock *Block = unwrap(BB); 01687 BasicBlock::iterator I = Block->end(); 01688 if (I == Block->begin()) 01689 return 0; 01690 return wrap(--I); 01691 } 01692 01693 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 01694 Instruction *Instr = unwrap<Instruction>(Inst); 01695 BasicBlock::iterator I = Instr; 01696 if (++I == Instr->getParent()->end()) 01697 return 0; 01698 return wrap(I); 01699 } 01700 01701 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 01702 Instruction *Instr = unwrap<Instruction>(Inst); 01703 BasicBlock::iterator I = Instr; 01704 if (I == Instr->getParent()->begin()) 01705 return 0; 01706 return wrap(--I); 01707 } 01708 01709 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 01710 unwrap<Instruction>(Inst)->eraseFromParent(); 01711 } 01712 01713 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 01714 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 01715 return (LLVMIntPredicate)I->getPredicate(); 01716 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 01717 if (CE->getOpcode() == Instruction::ICmp) 01718 return (LLVMIntPredicate)CE->getPredicate(); 01719 return (LLVMIntPredicate)0; 01720 } 01721 01722 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 01723 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 01724 return map_to_llvmopcode(C->getOpcode()); 01725 return (LLVMOpcode)0; 01726 } 01727 01728 /*--.. Call and invoke instructions ........................................--*/ 01729 01730 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 01731 Value *V = unwrap(Instr); 01732 if (CallInst *CI = dyn_cast<CallInst>(V)) 01733 return CI->getCallingConv(); 01734 if (InvokeInst *II = dyn_cast<InvokeInst>(V)) 01735 return II->getCallingConv(); 01736 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!"); 01737 } 01738 01739 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 01740 Value *V = unwrap(Instr); 01741 if (CallInst *CI = dyn_cast<CallInst>(V)) 01742 return CI->setCallingConv(static_cast<CallingConv::ID>(CC)); 01743 else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) 01744 return II->setCallingConv(static_cast<CallingConv::ID>(CC)); 01745 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!"); 01746 } 01747 01748 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 01749 LLVMAttribute PA) { 01750 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 01751 AttrBuilder B(PA); 01752 Call.setAttributes( 01753 Call.getAttributes().addAttributes(Call->getContext(), index, 01754 AttributeSet::get(Call->getContext(), 01755 index, B))); 01756 } 01757 01758 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 01759 LLVMAttribute PA) { 01760 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 01761 AttrBuilder B(PA); 01762 Call.setAttributes(Call.getAttributes() 01763 .removeAttributes(Call->getContext(), index, 01764 AttributeSet::get(Call->getContext(), 01765 index, B))); 01766 } 01767 01768 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 01769 unsigned align) { 01770 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 01771 AttrBuilder B; 01772 B.addAlignmentAttr(align); 01773 Call.setAttributes(Call.getAttributes() 01774 .addAttributes(Call->getContext(), index, 01775 AttributeSet::get(Call->getContext(), 01776 index, B))); 01777 } 01778 01779 /*--.. Operations on call instructions (only) ..............................--*/ 01780 01781 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 01782 return unwrap<CallInst>(Call)->isTailCall(); 01783 } 01784 01785 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 01786 unwrap<CallInst>(Call)->setTailCall(isTailCall); 01787 } 01788 01789 /*--.. Operations on switch instructions (only) ............................--*/ 01790 01791 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 01792 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 01793 } 01794 01795 /*--.. Operations on phi nodes .............................................--*/ 01796 01797 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 01798 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 01799 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 01800 for (unsigned I = 0; I != Count; ++I) 01801 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 01802 } 01803 01804 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 01805 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 01806 } 01807 01808 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 01809 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 01810 } 01811 01812 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 01813 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 01814 } 01815 01816 01817 /*===-- Instruction builders ----------------------------------------------===*/ 01818 01819 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 01820 return wrap(new IRBuilder<>(*unwrap(C))); 01821 } 01822 01823 LLVMBuilderRef LLVMCreateBuilder(void) { 01824 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 01825 } 01826 01827 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 01828 LLVMValueRef Instr) { 01829 BasicBlock *BB = unwrap(Block); 01830 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end(); 01831 unwrap(Builder)->SetInsertPoint(BB, I); 01832 } 01833 01834 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 01835 Instruction *I = unwrap<Instruction>(Instr); 01836 unwrap(Builder)->SetInsertPoint(I->getParent(), I); 01837 } 01838 01839 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 01840 BasicBlock *BB = unwrap(Block); 01841 unwrap(Builder)->SetInsertPoint(BB); 01842 } 01843 01844 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 01845 return wrap(unwrap(Builder)->GetInsertBlock()); 01846 } 01847 01848 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 01849 unwrap(Builder)->ClearInsertionPoint(); 01850 } 01851 01852 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 01853 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 01854 } 01855 01856 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 01857 const char *Name) { 01858 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 01859 } 01860 01861 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 01862 delete unwrap(Builder); 01863 } 01864 01865 /*--.. Metadata builders ...................................................--*/ 01866 01867 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 01868 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL; 01869 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc)); 01870 } 01871 01872 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 01873 return wrap(unwrap(Builder)->getCurrentDebugLocation() 01874 .getAsMDNode(unwrap(Builder)->getContext())); 01875 } 01876 01877 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 01878 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 01879 } 01880 01881 01882 /*--.. Instruction builders ................................................--*/ 01883 01884 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 01885 return wrap(unwrap(B)->CreateRetVoid()); 01886 } 01887 01888 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 01889 return wrap(unwrap(B)->CreateRet(unwrap(V))); 01890 } 01891 01892 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 01893 unsigned N) { 01894 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 01895 } 01896 01897 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 01898 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 01899 } 01900 01901 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 01902 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 01903 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 01904 } 01905 01906 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 01907 LLVMBasicBlockRef Else, unsigned NumCases) { 01908 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 01909 } 01910 01911 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 01912 unsigned NumDests) { 01913 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 01914 } 01915 01916 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 01917 LLVMValueRef *Args, unsigned NumArgs, 01918 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 01919 const char *Name) { 01920 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 01921 makeArrayRef(unwrap(Args), NumArgs), 01922 Name)); 01923 } 01924 01925 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 01926 LLVMValueRef PersFn, unsigned NumClauses, 01927 const char *Name) { 01928 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), 01929 cast<Function>(unwrap(PersFn)), 01930 NumClauses, Name)); 01931 } 01932 01933 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 01934 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 01935 } 01936 01937 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 01938 return wrap(unwrap(B)->CreateUnreachable()); 01939 } 01940 01941 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 01942 LLVMBasicBlockRef Dest) { 01943 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 01944 } 01945 01946 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 01947 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 01948 } 01949 01950 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 01951 unwrap<LandingPadInst>(LandingPad)-> 01952 addClause(cast<Constant>(unwrap(ClauseVal))); 01953 } 01954 01955 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 01956 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 01957 } 01958 01959 /*--.. Arithmetic ..........................................................--*/ 01960 01961 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01962 const char *Name) { 01963 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 01964 } 01965 01966 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01967 const char *Name) { 01968 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 01969 } 01970 01971 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01972 const char *Name) { 01973 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 01974 } 01975 01976 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01977 const char *Name) { 01978 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 01979 } 01980 01981 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01982 const char *Name) { 01983 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 01984 } 01985 01986 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01987 const char *Name) { 01988 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 01989 } 01990 01991 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01992 const char *Name) { 01993 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 01994 } 01995 01996 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 01997 const char *Name) { 01998 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 01999 } 02000 02001 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02002 const char *Name) { 02003 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 02004 } 02005 02006 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02007 const char *Name) { 02008 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 02009 } 02010 02011 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02012 const char *Name) { 02013 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 02014 } 02015 02016 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02017 const char *Name) { 02018 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 02019 } 02020 02021 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02022 const char *Name) { 02023 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 02024 } 02025 02026 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02027 const char *Name) { 02028 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 02029 } 02030 02031 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 02032 LLVMValueRef RHS, const char *Name) { 02033 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 02034 } 02035 02036 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02037 const char *Name) { 02038 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 02039 } 02040 02041 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02042 const char *Name) { 02043 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 02044 } 02045 02046 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02047 const char *Name) { 02048 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 02049 } 02050 02051 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02052 const char *Name) { 02053 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 02054 } 02055 02056 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02057 const char *Name) { 02058 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 02059 } 02060 02061 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02062 const char *Name) { 02063 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 02064 } 02065 02066 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02067 const char *Name) { 02068 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 02069 } 02070 02071 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02072 const char *Name) { 02073 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 02074 } 02075 02076 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02077 const char *Name) { 02078 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 02079 } 02080 02081 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 02082 const char *Name) { 02083 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 02084 } 02085 02086 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 02087 LLVMValueRef LHS, LLVMValueRef RHS, 02088 const char *Name) { 02089 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 02090 unwrap(RHS), Name)); 02091 } 02092 02093 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 02094 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 02095 } 02096 02097 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 02098 const char *Name) { 02099 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 02100 } 02101 02102 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 02103 const char *Name) { 02104 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 02105 } 02106 02107 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 02108 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 02109 } 02110 02111 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 02112 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 02113 } 02114 02115 /*--.. Memory ..............................................................--*/ 02116 02117 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 02118 const char *Name) { 02119 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 02120 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 02121 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 02122 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 02123 ITy, unwrap(Ty), AllocSize, 02124 0, 0, ""); 02125 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 02126 } 02127 02128 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 02129 LLVMValueRef Val, const char *Name) { 02130 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 02131 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 02132 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 02133 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 02134 ITy, unwrap(Ty), AllocSize, 02135 unwrap(Val), 0, ""); 02136 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 02137 } 02138 02139 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 02140 const char *Name) { 02141 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name)); 02142 } 02143 02144 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 02145 LLVMValueRef Val, const char *Name) { 02146 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 02147 } 02148 02149 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 02150 return wrap(unwrap(B)->Insert( 02151 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 02152 } 02153 02154 02155 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 02156 const char *Name) { 02157 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 02158 } 02159 02160 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 02161 LLVMValueRef PointerVal) { 02162 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 02163 } 02164 02165 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 02166 LLVMValueRef *Indices, unsigned NumIndices, 02167 const char *Name) { 02168 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 02169 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name)); 02170 } 02171 02172 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 02173 LLVMValueRef *Indices, unsigned NumIndices, 02174 const char *Name) { 02175 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 02176 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name)); 02177 } 02178 02179 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 02180 unsigned Idx, const char *Name) { 02181 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name)); 02182 } 02183 02184 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 02185 const char *Name) { 02186 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 02187 } 02188 02189 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 02190 const char *Name) { 02191 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 02192 } 02193 02194 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 02195 Value *P = unwrap<Value>(MemAccessInst); 02196 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 02197 return LI->isVolatile(); 02198 return cast<StoreInst>(P)->isVolatile(); 02199 } 02200 02201 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 02202 Value *P = unwrap<Value>(MemAccessInst); 02203 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 02204 return LI->setVolatile(isVolatile); 02205 return cast<StoreInst>(P)->setVolatile(isVolatile); 02206 } 02207 02208 /*--.. Casts ...............................................................--*/ 02209 02210 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 02211 LLVMTypeRef DestTy, const char *Name) { 02212 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 02213 } 02214 02215 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 02216 LLVMTypeRef DestTy, const char *Name) { 02217 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 02218 } 02219 02220 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 02221 LLVMTypeRef DestTy, const char *Name) { 02222 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 02223 } 02224 02225 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 02226 LLVMTypeRef DestTy, const char *Name) { 02227 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 02228 } 02229 02230 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 02231 LLVMTypeRef DestTy, const char *Name) { 02232 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 02233 } 02234 02235 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 02236 LLVMTypeRef DestTy, const char *Name) { 02237 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 02238 } 02239 02240 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 02241 LLVMTypeRef DestTy, const char *Name) { 02242 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 02243 } 02244 02245 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 02246 LLVMTypeRef DestTy, const char *Name) { 02247 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 02248 } 02249 02250 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 02251 LLVMTypeRef DestTy, const char *Name) { 02252 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 02253 } 02254 02255 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 02256 LLVMTypeRef DestTy, const char *Name) { 02257 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 02258 } 02259 02260 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 02261 LLVMTypeRef DestTy, const char *Name) { 02262 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 02263 } 02264 02265 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 02266 LLVMTypeRef DestTy, const char *Name) { 02267 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 02268 } 02269 02270 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 02271 LLVMTypeRef DestTy, const char *Name) { 02272 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 02273 Name)); 02274 } 02275 02276 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 02277 LLVMTypeRef DestTy, const char *Name) { 02278 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 02279 Name)); 02280 } 02281 02282 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 02283 LLVMTypeRef DestTy, const char *Name) { 02284 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 02285 Name)); 02286 } 02287 02288 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 02289 LLVMTypeRef DestTy, const char *Name) { 02290 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 02291 unwrap(DestTy), Name)); 02292 } 02293 02294 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 02295 LLVMTypeRef DestTy, const char *Name) { 02296 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 02297 } 02298 02299 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 02300 LLVMTypeRef DestTy, const char *Name) { 02301 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 02302 /*isSigned*/true, Name)); 02303 } 02304 02305 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 02306 LLVMTypeRef DestTy, const char *Name) { 02307 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 02308 } 02309 02310 /*--.. Comparisons .........................................................--*/ 02311 02312 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 02313 LLVMValueRef LHS, LLVMValueRef RHS, 02314 const char *Name) { 02315 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 02316 unwrap(LHS), unwrap(RHS), Name)); 02317 } 02318 02319 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 02320 LLVMValueRef LHS, LLVMValueRef RHS, 02321 const char *Name) { 02322 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 02323 unwrap(LHS), unwrap(RHS), Name)); 02324 } 02325 02326 /*--.. Miscellaneous instructions ..........................................--*/ 02327 02328 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 02329 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 02330 } 02331 02332 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 02333 LLVMValueRef *Args, unsigned NumArgs, 02334 const char *Name) { 02335 return wrap(unwrap(B)->CreateCall(unwrap(Fn), 02336 makeArrayRef(unwrap(Args), NumArgs), 02337 Name)); 02338 } 02339 02340 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 02341 LLVMValueRef Then, LLVMValueRef Else, 02342 const char *Name) { 02343 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 02344 Name)); 02345 } 02346 02347 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 02348 LLVMTypeRef Ty, const char *Name) { 02349 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 02350 } 02351 02352 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 02353 LLVMValueRef Index, const char *Name) { 02354 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 02355 Name)); 02356 } 02357 02358 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 02359 LLVMValueRef EltVal, LLVMValueRef Index, 02360 const char *Name) { 02361 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 02362 unwrap(Index), Name)); 02363 } 02364 02365 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 02366 LLVMValueRef V2, LLVMValueRef Mask, 02367 const char *Name) { 02368 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 02369 unwrap(Mask), Name)); 02370 } 02371 02372 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 02373 unsigned Index, const char *Name) { 02374 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 02375 } 02376 02377 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 02378 LLVMValueRef EltVal, unsigned Index, 02379 const char *Name) { 02380 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 02381 Index, Name)); 02382 } 02383 02384 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 02385 const char *Name) { 02386 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 02387 } 02388 02389 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 02390 const char *Name) { 02391 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 02392 } 02393 02394 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 02395 LLVMValueRef RHS, const char *Name) { 02396 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 02397 } 02398 02399 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 02400 LLVMValueRef PTR, LLVMValueRef Val, 02401 LLVMAtomicOrdering ordering, 02402 LLVMBool singleThread) { 02403 AtomicRMWInst::BinOp intop; 02404 switch (op) { 02405 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break; 02406 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break; 02407 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break; 02408 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break; 02409 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break; 02410 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break; 02411 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break; 02412 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break; 02413 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break; 02414 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break; 02415 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break; 02416 } 02417 AtomicOrdering intordering; 02418 switch (ordering) { 02419 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break; 02420 case LLVMAtomicOrderingUnordered: intordering = Unordered; break; 02421 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break; 02422 case LLVMAtomicOrderingAcquire: intordering = Acquire; break; 02423 case LLVMAtomicOrderingRelease: intordering = Release; break; 02424 case LLVMAtomicOrderingAcquireRelease: 02425 intordering = AcquireRelease; 02426 break; 02427 case LLVMAtomicOrderingSequentiallyConsistent: 02428 intordering = SequentiallyConsistent; 02429 break; 02430 } 02431 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), 02432 intordering, singleThread ? SingleThread : CrossThread)); 02433 } 02434 02435 02436 /*===-- Module providers --------------------------------------------------===*/ 02437 02438 LLVMModuleProviderRef 02439 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 02440 return reinterpret_cast<LLVMModuleProviderRef>(M); 02441 } 02442 02443 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 02444 delete unwrap(MP); 02445 } 02446 02447 02448 /*===-- Memory buffers ----------------------------------------------------===*/ 02449 02450 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 02451 const char *Path, 02452 LLVMMemoryBufferRef *OutMemBuf, 02453 char **OutMessage) { 02454 02455 OwningPtr<MemoryBuffer> MB; 02456 error_code ec; 02457 if (!(ec = MemoryBuffer::getFile(Path, MB))) { 02458 *OutMemBuf = wrap(MB.take()); 02459 return 0; 02460 } 02461 02462 *OutMessage = strdup(ec.message().c_str()); 02463 return 1; 02464 } 02465 02466 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 02467 char **OutMessage) { 02468 OwningPtr<MemoryBuffer> MB; 02469 error_code ec; 02470 if (!(ec = MemoryBuffer::getSTDIN(MB))) { 02471 *OutMemBuf = wrap(MB.take()); 02472 return 0; 02473 } 02474 02475 *OutMessage = strdup(ec.message().c_str()); 02476 return 1; 02477 } 02478 02479 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 02480 const char *InputData, 02481 size_t InputDataLength, 02482 const char *BufferName, 02483 LLVMBool RequiresNullTerminator) { 02484 02485 return wrap(MemoryBuffer::getMemBuffer( 02486 StringRef(InputData, InputDataLength), 02487 StringRef(BufferName), 02488 RequiresNullTerminator)); 02489 } 02490 02491 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 02492 const char *InputData, 02493 size_t InputDataLength, 02494 const char *BufferName) { 02495 02496 return wrap(MemoryBuffer::getMemBufferCopy( 02497 StringRef(InputData, InputDataLength), 02498 StringRef(BufferName))); 02499 } 02500 02501 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 02502 return unwrap(MemBuf)->getBufferStart(); 02503 } 02504 02505 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 02506 return unwrap(MemBuf)->getBufferSize(); 02507 } 02508 02509 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 02510 delete unwrap(MemBuf); 02511 } 02512 02513 /*===-- Pass Registry -----------------------------------------------------===*/ 02514 02515 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 02516 return wrap(PassRegistry::getPassRegistry()); 02517 } 02518 02519 /*===-- Pass Manager ------------------------------------------------------===*/ 02520 02521 LLVMPassManagerRef LLVMCreatePassManager() { 02522 return wrap(new PassManager()); 02523 } 02524 02525 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 02526 return wrap(new FunctionPassManager(unwrap(M))); 02527 } 02528 02529 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 02530 return LLVMCreateFunctionPassManagerForModule( 02531 reinterpret_cast<LLVMModuleRef>(P)); 02532 } 02533 02534 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 02535 return unwrap<PassManager>(PM)->run(*unwrap(M)); 02536 } 02537 02538 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 02539 return unwrap<FunctionPassManager>(FPM)->doInitialization(); 02540 } 02541 02542 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 02543 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 02544 } 02545 02546 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 02547 return unwrap<FunctionPassManager>(FPM)->doFinalization(); 02548 } 02549 02550 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 02551 delete unwrap(PM); 02552 } 02553 02554 /*===-- Threading ------------------------------------------------------===*/ 02555 02556 LLVMBool LLVMStartMultithreaded() { 02557 return llvm_start_multithreaded(); 02558 } 02559 02560 void LLVMStopMultithreaded() { 02561 llvm_stop_multithreaded(); 02562 } 02563 02564 LLVMBool LLVMIsMultithreaded() { 02565 return llvm_is_multithreaded(); 02566 }