1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/GlobalAlias.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/InlineAsm.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/PassManager.h"
28 #include "llvm/Support/CallSite.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ManagedStatic.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/system_error.h"
35 #include "llvm/Support/Threading.h"
42 void llvm::initializeCore(PassRegistry &Registry) {
43 initializeDominatorTreePass(Registry);
44 initializePrintModulePassPass(Registry);
45 initializePrintFunctionPassPass(Registry);
46 initializePrintBasicBlockPassPass(Registry);
47 initializeVerifierPass(Registry);
48 initializePreVerifierPass(Registry);
51 void LLVMInitializeCore(LLVMPassRegistryRef R) {
52 initializeCore(*unwrap(R));
59 /*===-- Error handling ----------------------------------------------------===*/
61 void LLVMDisposeMessage(char *Message) {
66 /*===-- Operations on contexts --------------------------------------------===*/
68 LLVMContextRef LLVMContextCreate() {
69 return wrap(new LLVMContext());
72 LLVMContextRef LLVMGetGlobalContext() {
73 return wrap(&getGlobalContext());
76 void LLVMContextDispose(LLVMContextRef C) {
80 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
82 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
85 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
86 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
90 /*===-- Operations on modules ---------------------------------------------===*/
92 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
93 return wrap(new Module(ModuleID, getGlobalContext()));
96 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
98 return wrap(new Module(ModuleID, *unwrap(C)));
101 void LLVMDisposeModule(LLVMModuleRef M) {
105 /*--.. Data layout .........................................................--*/
106 const char * LLVMGetDataLayout(LLVMModuleRef M) {
107 return unwrap(M)->getDataLayout().c_str();
110 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
111 unwrap(M)->setDataLayout(Triple);
114 /*--.. Target triple .......................................................--*/
115 const char * LLVMGetTarget(LLVMModuleRef M) {
116 return unwrap(M)->getTargetTriple().c_str();
119 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
120 unwrap(M)->setTargetTriple(Triple);
123 void LLVMDumpModule(LLVMModuleRef M) {
127 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
128 char **ErrorMessage) {
130 raw_fd_ostream dest(Filename, error);
131 if (!error.empty()) {
132 *ErrorMessage = strdup(error.c_str());
136 unwrap(M)->print(dest, NULL);
138 if (!error.empty()) {
139 *ErrorMessage = strdup(error.c_str());
146 /*--.. Operations on inline assembler ......................................--*/
147 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
148 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
152 /*--.. Operations on module contexts ......................................--*/
153 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
154 return wrap(&unwrap(M)->getContext());
158 /*===-- Operations on types -----------------------------------------------===*/
160 /*--.. Operations on all types (mostly) ....................................--*/
162 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
163 switch (unwrap(Ty)->getTypeID()) {
164 default: llvm_unreachable("Unhandled TypeID.");
166 return LLVMVoidTypeKind;
168 return LLVMHalfTypeKind;
169 case Type::FloatTyID:
170 return LLVMFloatTypeKind;
171 case Type::DoubleTyID:
172 return LLVMDoubleTypeKind;
173 case Type::X86_FP80TyID:
174 return LLVMX86_FP80TypeKind;
175 case Type::FP128TyID:
176 return LLVMFP128TypeKind;
177 case Type::PPC_FP128TyID:
178 return LLVMPPC_FP128TypeKind;
179 case Type::LabelTyID:
180 return LLVMLabelTypeKind;
181 case Type::MetadataTyID:
182 return LLVMMetadataTypeKind;
183 case Type::IntegerTyID:
184 return LLVMIntegerTypeKind;
185 case Type::FunctionTyID:
186 return LLVMFunctionTypeKind;
187 case Type::StructTyID:
188 return LLVMStructTypeKind;
189 case Type::ArrayTyID:
190 return LLVMArrayTypeKind;
191 case Type::PointerTyID:
192 return LLVMPointerTypeKind;
193 case Type::VectorTyID:
194 return LLVMVectorTypeKind;
195 case Type::X86_MMXTyID:
196 return LLVMX86_MMXTypeKind;
200 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
202 return unwrap(Ty)->isSized();
205 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
206 return wrap(&unwrap(Ty)->getContext());
209 /*--.. Operations on integer types .........................................--*/
211 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
212 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
214 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
215 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
217 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
218 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
220 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
221 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
223 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
224 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
226 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
227 return wrap(IntegerType::get(*unwrap(C), NumBits));
230 LLVMTypeRef LLVMInt1Type(void) {
231 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
233 LLVMTypeRef LLVMInt8Type(void) {
234 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
236 LLVMTypeRef LLVMInt16Type(void) {
237 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
239 LLVMTypeRef LLVMInt32Type(void) {
240 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
242 LLVMTypeRef LLVMInt64Type(void) {
243 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
245 LLVMTypeRef LLVMIntType(unsigned NumBits) {
246 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
249 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
250 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
253 /*--.. Operations on real types ............................................--*/
255 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
256 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
258 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
259 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
261 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
262 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
264 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
265 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
267 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
268 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
270 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
271 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
273 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
274 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
277 LLVMTypeRef LLVMHalfType(void) {
278 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
280 LLVMTypeRef LLVMFloatType(void) {
281 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
283 LLVMTypeRef LLVMDoubleType(void) {
284 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
286 LLVMTypeRef LLVMX86FP80Type(void) {
287 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
289 LLVMTypeRef LLVMFP128Type(void) {
290 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
292 LLVMTypeRef LLVMPPCFP128Type(void) {
293 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
295 LLVMTypeRef LLVMX86MMXType(void) {
296 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
299 /*--.. Operations on function types ........................................--*/
301 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
302 LLVMTypeRef *ParamTypes, unsigned ParamCount,
304 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
305 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
308 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
309 return unwrap<FunctionType>(FunctionTy)->isVarArg();
312 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
313 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
316 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
317 return unwrap<FunctionType>(FunctionTy)->getNumParams();
320 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
321 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
322 for (FunctionType::param_iterator I = Ty->param_begin(),
323 E = Ty->param_end(); I != E; ++I)
327 /*--.. Operations on struct types ..........................................--*/
329 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
330 unsigned ElementCount, LLVMBool Packed) {
331 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
332 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
335 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
336 unsigned ElementCount, LLVMBool Packed) {
337 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
338 ElementCount, Packed);
341 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
343 return wrap(StructType::create(*unwrap(C), Name));
346 const char *LLVMGetStructName(LLVMTypeRef Ty)
348 StructType *Type = unwrap<StructType>(Ty);
349 if (!Type->hasName())
351 return Type->getName().data();
354 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
355 unsigned ElementCount, LLVMBool Packed) {
356 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
357 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
360 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
361 return unwrap<StructType>(StructTy)->getNumElements();
364 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
365 StructType *Ty = unwrap<StructType>(StructTy);
366 for (StructType::element_iterator I = Ty->element_begin(),
367 E = Ty->element_end(); I != E; ++I)
371 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
372 return unwrap<StructType>(StructTy)->isPacked();
375 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
376 return unwrap<StructType>(StructTy)->isOpaque();
379 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
380 return wrap(unwrap(M)->getTypeByName(Name));
383 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
385 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
386 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
389 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
390 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
393 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
394 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
397 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
398 return wrap(unwrap<SequentialType>(Ty)->getElementType());
401 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
402 return unwrap<ArrayType>(ArrayTy)->getNumElements();
405 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
406 return unwrap<PointerType>(PointerTy)->getAddressSpace();
409 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
410 return unwrap<VectorType>(VectorTy)->getNumElements();
413 /*--.. Operations on other types ...........................................--*/
415 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
416 return wrap(Type::getVoidTy(*unwrap(C)));
418 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
419 return wrap(Type::getLabelTy(*unwrap(C)));
422 LLVMTypeRef LLVMVoidType(void) {
423 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
425 LLVMTypeRef LLVMLabelType(void) {
426 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
429 /*===-- Operations on values ----------------------------------------------===*/
431 /*--.. Operations on all values ............................................--*/
433 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
434 return wrap(unwrap(Val)->getType());
437 const char *LLVMGetValueName(LLVMValueRef Val) {
438 return unwrap(Val)->getName().data();
441 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
442 unwrap(Val)->setName(Name);
445 void LLVMDumpValue(LLVMValueRef Val) {
449 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
450 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
453 int LLVMHasMetadata(LLVMValueRef Inst) {
454 return unwrap<Instruction>(Inst)->hasMetadata();
457 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
458 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
461 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
462 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
465 /*--.. Conversion functions ................................................--*/
467 #define LLVM_DEFINE_VALUE_CAST(name) \
468 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
469 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
472 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
474 /*--.. Operations on Uses ..................................................--*/
475 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
476 Value *V = unwrap(Val);
477 Value::use_iterator I = V->use_begin();
478 if (I == V->use_end())
480 return wrap(&(I.getUse()));
483 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
484 Use *Next = unwrap(U)->getNext();
490 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
491 return wrap(unwrap(U)->getUser());
494 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
495 return wrap(unwrap(U)->get());
498 /*--.. Operations on Users .................................................--*/
499 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
500 Value *V = unwrap(Val);
501 if (MDNode *MD = dyn_cast<MDNode>(V))
502 return wrap(MD->getOperand(Index));
503 return wrap(cast<User>(V)->getOperand(Index));
506 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
507 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
510 int LLVMGetNumOperands(LLVMValueRef Val) {
511 Value *V = unwrap(Val);
512 if (MDNode *MD = dyn_cast<MDNode>(V))
513 return MD->getNumOperands();
514 return cast<User>(V)->getNumOperands();
517 /*--.. Operations on constants of any type .................................--*/
519 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
520 return wrap(Constant::getNullValue(unwrap(Ty)));
523 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
524 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
527 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
528 return wrap(UndefValue::get(unwrap(Ty)));
531 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
532 return isa<Constant>(unwrap(Ty));
535 LLVMBool LLVMIsNull(LLVMValueRef Val) {
536 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
537 return C->isNullValue();
541 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
542 return isa<UndefValue>(unwrap(Val));
545 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
547 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
550 /*--.. Operations on metadata nodes ........................................--*/
552 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
554 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
557 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
558 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
561 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
563 return wrap(MDNode::get(*unwrap(C),
564 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
567 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
568 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
571 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
572 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
573 *Len = S->getString().size();
574 return S->getString().data();
580 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
582 return cast<MDNode>(unwrap(V))->getNumOperands();
585 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
587 const MDNode *N = cast<MDNode>(unwrap(V));
588 const unsigned numOperands = N->getNumOperands();
589 for (unsigned i = 0; i < numOperands; i++)
590 Dest[i] = wrap(N->getOperand(i));
593 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
595 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
596 return N->getNumOperands();
601 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
603 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
606 for (unsigned i=0;i<N->getNumOperands();i++)
607 Dest[i] = wrap(N->getOperand(i));
610 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
613 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
616 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
621 /*--.. Operations on scalar constants ......................................--*/
623 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
624 LLVMBool SignExtend) {
625 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
628 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
630 const uint64_t Words[]) {
631 IntegerType *Ty = unwrap<IntegerType>(IntTy);
632 return wrap(ConstantInt::get(Ty->getContext(),
633 APInt(Ty->getBitWidth(),
634 makeArrayRef(Words, NumWords))));
637 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
639 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
643 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
644 unsigned SLen, uint8_t Radix) {
645 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
649 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
650 return wrap(ConstantFP::get(unwrap(RealTy), N));
653 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
654 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
657 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
659 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
662 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
663 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
666 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
667 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
670 /*--.. Operations on composite constants ...................................--*/
672 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
674 LLVMBool DontNullTerminate) {
675 /* Inverted the sense of AddNull because ', 0)' is a
676 better mnemonic for null termination than ', 1)'. */
677 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
678 DontNullTerminate == 0));
680 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
681 LLVMValueRef *ConstantVals,
682 unsigned Count, LLVMBool Packed) {
683 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
684 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
688 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
689 LLVMBool DontNullTerminate) {
690 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
693 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
694 LLVMValueRef *ConstantVals, unsigned Length) {
695 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
696 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
698 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
700 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
704 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
705 LLVMValueRef *ConstantVals,
707 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
708 StructType *Ty = cast<StructType>(unwrap(StructTy));
710 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
713 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
714 return wrap(ConstantVector::get(makeArrayRef(
715 unwrap<Constant>(ScalarConstantVals, Size), Size)));
718 /*-- Opcode mapping */
720 static LLVMOpcode map_to_llvmopcode(int opcode)
723 default: llvm_unreachable("Unhandled Opcode.");
724 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
725 #include "llvm/IR/Instruction.def"
730 static int map_from_llvmopcode(LLVMOpcode code)
733 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
734 #include "llvm/IR/Instruction.def"
737 llvm_unreachable("Unhandled Opcode.");
740 /*--.. Constant expressions ................................................--*/
742 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
743 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
746 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
747 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
750 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
751 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
754 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
755 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
758 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
759 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
762 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
763 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
767 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
768 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
771 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
772 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
775 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
776 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
777 unwrap<Constant>(RHSConstant)));
780 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
781 LLVMValueRef RHSConstant) {
782 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
783 unwrap<Constant>(RHSConstant)));
786 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
787 LLVMValueRef RHSConstant) {
788 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
789 unwrap<Constant>(RHSConstant)));
792 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
793 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
794 unwrap<Constant>(RHSConstant)));
797 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
798 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
799 unwrap<Constant>(RHSConstant)));
802 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
803 LLVMValueRef RHSConstant) {
804 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
805 unwrap<Constant>(RHSConstant)));
808 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
809 LLVMValueRef RHSConstant) {
810 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
811 unwrap<Constant>(RHSConstant)));
814 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
815 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
816 unwrap<Constant>(RHSConstant)));
819 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
820 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
821 unwrap<Constant>(RHSConstant)));
824 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
825 LLVMValueRef RHSConstant) {
826 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
827 unwrap<Constant>(RHSConstant)));
830 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
831 LLVMValueRef RHSConstant) {
832 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
833 unwrap<Constant>(RHSConstant)));
836 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
837 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
838 unwrap<Constant>(RHSConstant)));
841 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
842 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
843 unwrap<Constant>(RHSConstant)));
846 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
847 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
848 unwrap<Constant>(RHSConstant)));
851 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
852 LLVMValueRef RHSConstant) {
853 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
854 unwrap<Constant>(RHSConstant)));
857 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
858 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
859 unwrap<Constant>(RHSConstant)));
862 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
863 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
864 unwrap<Constant>(RHSConstant)));
867 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
868 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
869 unwrap<Constant>(RHSConstant)));
872 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
873 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
874 unwrap<Constant>(RHSConstant)));
877 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
878 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
879 unwrap<Constant>(RHSConstant)));
882 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
883 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
884 unwrap<Constant>(RHSConstant)));
887 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
888 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
889 unwrap<Constant>(RHSConstant)));
892 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
893 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
894 return wrap(ConstantExpr::getICmp(Predicate,
895 unwrap<Constant>(LHSConstant),
896 unwrap<Constant>(RHSConstant)));
899 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
900 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
901 return wrap(ConstantExpr::getFCmp(Predicate,
902 unwrap<Constant>(LHSConstant),
903 unwrap<Constant>(RHSConstant)));
906 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
907 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
908 unwrap<Constant>(RHSConstant)));
911 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
912 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
913 unwrap<Constant>(RHSConstant)));
916 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
917 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
918 unwrap<Constant>(RHSConstant)));
921 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
922 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
923 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
925 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
929 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
930 LLVMValueRef *ConstantIndices,
931 unsigned NumIndices) {
932 Constant* Val = unwrap<Constant>(ConstantVal);
933 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
935 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
938 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
939 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
943 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
944 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
948 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
949 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
953 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
954 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
958 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
959 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
963 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
964 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
968 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
969 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
973 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
974 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
978 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
979 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
983 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
984 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
988 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
989 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
993 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
994 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
998 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
999 LLVMTypeRef ToType) {
1000 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1004 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1005 LLVMTypeRef ToType) {
1006 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1010 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1011 LLVMTypeRef ToType) {
1012 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1016 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1017 LLVMTypeRef ToType) {
1018 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1022 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1023 LLVMBool isSigned) {
1024 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1025 unwrap(ToType), isSigned));
1028 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1029 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1033 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1034 LLVMValueRef ConstantIfTrue,
1035 LLVMValueRef ConstantIfFalse) {
1036 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1037 unwrap<Constant>(ConstantIfTrue),
1038 unwrap<Constant>(ConstantIfFalse)));
1041 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1042 LLVMValueRef IndexConstant) {
1043 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1044 unwrap<Constant>(IndexConstant)));
1047 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1048 LLVMValueRef ElementValueConstant,
1049 LLVMValueRef IndexConstant) {
1050 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1051 unwrap<Constant>(ElementValueConstant),
1052 unwrap<Constant>(IndexConstant)));
1055 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1056 LLVMValueRef VectorBConstant,
1057 LLVMValueRef MaskConstant) {
1058 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1059 unwrap<Constant>(VectorBConstant),
1060 unwrap<Constant>(MaskConstant)));
1063 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1065 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1066 makeArrayRef(IdxList, NumIdx)));
1069 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1070 LLVMValueRef ElementValueConstant,
1071 unsigned *IdxList, unsigned NumIdx) {
1072 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1073 unwrap<Constant>(ElementValueConstant),
1074 makeArrayRef(IdxList, NumIdx)));
1077 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1078 const char *Constraints,
1079 LLVMBool HasSideEffects,
1080 LLVMBool IsAlignStack) {
1081 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1082 Constraints, HasSideEffects, IsAlignStack));
1085 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1086 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1089 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1091 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1092 return wrap(unwrap<GlobalValue>(Global)->getParent());
1095 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1096 return unwrap<GlobalValue>(Global)->isDeclaration();
1099 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1100 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1101 case GlobalValue::ExternalLinkage:
1102 return LLVMExternalLinkage;
1103 case GlobalValue::AvailableExternallyLinkage:
1104 return LLVMAvailableExternallyLinkage;
1105 case GlobalValue::LinkOnceAnyLinkage:
1106 return LLVMLinkOnceAnyLinkage;
1107 case GlobalValue::LinkOnceODRLinkage:
1108 return LLVMLinkOnceODRLinkage;
1109 case GlobalValue::LinkOnceODRAutoHideLinkage:
1110 return LLVMLinkOnceODRAutoHideLinkage;
1111 case GlobalValue::WeakAnyLinkage:
1112 return LLVMWeakAnyLinkage;
1113 case GlobalValue::WeakODRLinkage:
1114 return LLVMWeakODRLinkage;
1115 case GlobalValue::AppendingLinkage:
1116 return LLVMAppendingLinkage;
1117 case GlobalValue::InternalLinkage:
1118 return LLVMInternalLinkage;
1119 case GlobalValue::PrivateLinkage:
1120 return LLVMPrivateLinkage;
1121 case GlobalValue::LinkerPrivateLinkage:
1122 return LLVMLinkerPrivateLinkage;
1123 case GlobalValue::LinkerPrivateWeakLinkage:
1124 return LLVMLinkerPrivateWeakLinkage;
1125 case GlobalValue::DLLImportLinkage:
1126 return LLVMDLLImportLinkage;
1127 case GlobalValue::DLLExportLinkage:
1128 return LLVMDLLExportLinkage;
1129 case GlobalValue::ExternalWeakLinkage:
1130 return LLVMExternalWeakLinkage;
1131 case GlobalValue::CommonLinkage:
1132 return LLVMCommonLinkage;
1135 llvm_unreachable("Invalid GlobalValue linkage!");
1138 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1139 GlobalValue *GV = unwrap<GlobalValue>(Global);
1142 case LLVMExternalLinkage:
1143 GV->setLinkage(GlobalValue::ExternalLinkage);
1145 case LLVMAvailableExternallyLinkage:
1146 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1148 case LLVMLinkOnceAnyLinkage:
1149 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1151 case LLVMLinkOnceODRLinkage:
1152 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1154 case LLVMLinkOnceODRAutoHideLinkage:
1155 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1157 case LLVMWeakAnyLinkage:
1158 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1160 case LLVMWeakODRLinkage:
1161 GV->setLinkage(GlobalValue::WeakODRLinkage);
1163 case LLVMAppendingLinkage:
1164 GV->setLinkage(GlobalValue::AppendingLinkage);
1166 case LLVMInternalLinkage:
1167 GV->setLinkage(GlobalValue::InternalLinkage);
1169 case LLVMPrivateLinkage:
1170 GV->setLinkage(GlobalValue::PrivateLinkage);
1172 case LLVMLinkerPrivateLinkage:
1173 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1175 case LLVMLinkerPrivateWeakLinkage:
1176 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1178 case LLVMDLLImportLinkage:
1179 GV->setLinkage(GlobalValue::DLLImportLinkage);
1181 case LLVMDLLExportLinkage:
1182 GV->setLinkage(GlobalValue::DLLExportLinkage);
1184 case LLVMExternalWeakLinkage:
1185 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1187 case LLVMGhostLinkage:
1189 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1191 case LLVMCommonLinkage:
1192 GV->setLinkage(GlobalValue::CommonLinkage);
1197 const char *LLVMGetSection(LLVMValueRef Global) {
1198 return unwrap<GlobalValue>(Global)->getSection().c_str();
1201 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1202 unwrap<GlobalValue>(Global)->setSection(Section);
1205 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1206 return static_cast<LLVMVisibility>(
1207 unwrap<GlobalValue>(Global)->getVisibility());
1210 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1211 unwrap<GlobalValue>(Global)
1212 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1215 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1216 return unwrap<GlobalValue>(Global)->getAlignment();
1219 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1220 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1223 /*--.. Operations on global variables ......................................--*/
1225 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1226 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1227 GlobalValue::ExternalLinkage, 0, Name));
1230 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1232 unsigned AddressSpace) {
1233 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1234 GlobalValue::ExternalLinkage, 0, Name, 0,
1235 GlobalVariable::NotThreadLocal, AddressSpace));
1238 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1239 return wrap(unwrap(M)->getNamedGlobal(Name));
1242 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1243 Module *Mod = unwrap(M);
1244 Module::global_iterator I = Mod->global_begin();
1245 if (I == Mod->global_end())
1250 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1251 Module *Mod = unwrap(M);
1252 Module::global_iterator I = Mod->global_end();
1253 if (I == Mod->global_begin())
1258 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1259 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1260 Module::global_iterator I = GV;
1261 if (++I == GV->getParent()->global_end())
1266 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1267 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1268 Module::global_iterator I = GV;
1269 if (I == GV->getParent()->global_begin())
1274 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1275 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1278 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1279 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1280 if ( !GV->hasInitializer() )
1282 return wrap(GV->getInitializer());
1285 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1286 unwrap<GlobalVariable>(GlobalVar)
1287 ->setInitializer(unwrap<Constant>(ConstantVal));
1290 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1291 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1294 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1295 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1298 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1299 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1302 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1303 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1306 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1307 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1308 case GlobalVariable::NotThreadLocal:
1309 return LLVMNotThreadLocal;
1310 case GlobalVariable::GeneralDynamicTLSModel:
1311 return LLVMGeneralDynamicTLSModel;
1312 case GlobalVariable::LocalDynamicTLSModel:
1313 return LLVMLocalDynamicTLSModel;
1314 case GlobalVariable::InitialExecTLSModel:
1315 return LLVMInitialExecTLSModel;
1316 case GlobalVariable::LocalExecTLSModel:
1317 return LLVMLocalExecTLSModel;
1320 llvm_unreachable("Invalid GlobalVariable thread local mode");
1323 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1324 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1327 case LLVMNotThreadLocal:
1328 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1330 case LLVMGeneralDynamicTLSModel:
1331 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1333 case LLVMLocalDynamicTLSModel:
1334 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1336 case LLVMInitialExecTLSModel:
1337 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1339 case LLVMLocalExecTLSModel:
1340 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1345 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1346 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1349 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1350 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1353 /*--.. Operations on aliases ......................................--*/
1355 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1357 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1358 unwrap<Constant>(Aliasee), unwrap (M)));
1361 /*--.. Operations on functions .............................................--*/
1363 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1364 LLVMTypeRef FunctionTy) {
1365 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1366 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1369 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1370 return wrap(unwrap(M)->getFunction(Name));
1373 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1374 Module *Mod = unwrap(M);
1375 Module::iterator I = Mod->begin();
1376 if (I == Mod->end())
1381 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1382 Module *Mod = unwrap(M);
1383 Module::iterator I = Mod->end();
1384 if (I == Mod->begin())
1389 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1390 Function *Func = unwrap<Function>(Fn);
1391 Module::iterator I = Func;
1392 if (++I == Func->getParent()->end())
1397 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1398 Function *Func = unwrap<Function>(Fn);
1399 Module::iterator I = Func;
1400 if (I == Func->getParent()->begin())
1405 void LLVMDeleteFunction(LLVMValueRef Fn) {
1406 unwrap<Function>(Fn)->eraseFromParent();
1409 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1410 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1411 return F->getIntrinsicID();
1415 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1416 return unwrap<Function>(Fn)->getCallingConv();
1419 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1420 return unwrap<Function>(Fn)->setCallingConv(
1421 static_cast<CallingConv::ID>(CC));
1424 const char *LLVMGetGC(LLVMValueRef Fn) {
1425 Function *F = unwrap<Function>(Fn);
1426 return F->hasGC()? F->getGC() : 0;
1429 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1430 Function *F = unwrap<Function>(Fn);
1437 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1438 Function *Func = unwrap<Function>(Fn);
1439 const AttributeSet PAL = Func->getAttributes();
1441 const AttributeSet PALnew =
1442 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1443 AttributeSet::get(Func->getContext(),
1444 AttributeSet::FunctionIndex, B));
1445 Func->setAttributes(PALnew);
1448 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1450 Function *Func = unwrap<Function>(Fn);
1451 AttributeSet::AttrIndex Idx =
1452 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1455 B.addAttribute(A, V);
1456 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1457 Func->addAttributes(Idx, Set);
1460 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1461 Function *Func = unwrap<Function>(Fn);
1462 const AttributeSet PAL = Func->getAttributes();
1464 const AttributeSet PALnew =
1465 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1466 AttributeSet::get(Func->getContext(),
1467 AttributeSet::FunctionIndex, B));
1468 Func->setAttributes(PALnew);
1471 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1472 Function *Func = unwrap<Function>(Fn);
1473 const AttributeSet PAL = Func->getAttributes();
1474 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1477 /*--.. Operations on parameters ............................................--*/
1479 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1480 // This function is strictly redundant to
1481 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1482 return unwrap<Function>(FnRef)->arg_size();
1485 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1486 Function *Fn = unwrap<Function>(FnRef);
1487 for (Function::arg_iterator I = Fn->arg_begin(),
1488 E = Fn->arg_end(); I != E; I++)
1489 *ParamRefs++ = wrap(I);
1492 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1493 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1499 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1500 return wrap(unwrap<Argument>(V)->getParent());
1503 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1504 Function *Func = unwrap<Function>(Fn);
1505 Function::arg_iterator I = Func->arg_begin();
1506 if (I == Func->arg_end())
1511 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1512 Function *Func = unwrap<Function>(Fn);
1513 Function::arg_iterator I = Func->arg_end();
1514 if (I == Func->arg_begin())
1519 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1520 Argument *A = unwrap<Argument>(Arg);
1521 Function::arg_iterator I = A;
1522 if (++I == A->getParent()->arg_end())
1527 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1528 Argument *A = unwrap<Argument>(Arg);
1529 Function::arg_iterator I = A;
1530 if (I == A->getParent()->arg_begin())
1535 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1536 Argument *A = unwrap<Argument>(Arg);
1538 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1541 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1542 Argument *A = unwrap<Argument>(Arg);
1544 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1547 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1548 Argument *A = unwrap<Argument>(Arg);
1549 return (LLVMAttribute)A->getParent()->getAttributes().
1550 Raw(A->getArgNo()+1);
1554 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1555 Argument *A = unwrap<Argument>(Arg);
1557 B.addAlignmentAttr(align);
1558 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1561 /*--.. Operations on basic blocks ..........................................--*/
1563 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1564 return wrap(static_cast<Value*>(unwrap(BB)));
1567 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1568 return isa<BasicBlock>(unwrap(Val));
1571 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1572 return wrap(unwrap<BasicBlock>(Val));
1575 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1576 return wrap(unwrap(BB)->getParent());
1579 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1580 return wrap(unwrap(BB)->getTerminator());
1583 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1584 return unwrap<Function>(FnRef)->size();
1587 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1588 Function *Fn = unwrap<Function>(FnRef);
1589 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1590 *BasicBlocksRefs++ = wrap(I);
1593 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1594 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1597 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1598 Function *Func = unwrap<Function>(Fn);
1599 Function::iterator I = Func->begin();
1600 if (I == Func->end())
1605 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1606 Function *Func = unwrap<Function>(Fn);
1607 Function::iterator I = Func->end();
1608 if (I == Func->begin())
1613 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1614 BasicBlock *Block = unwrap(BB);
1615 Function::iterator I = Block;
1616 if (++I == Block->getParent()->end())
1621 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1622 BasicBlock *Block = unwrap(BB);
1623 Function::iterator I = Block;
1624 if (I == Block->getParent()->begin())
1629 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1632 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1635 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1636 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1639 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1640 LLVMBasicBlockRef BBRef,
1642 BasicBlock *BB = unwrap(BBRef);
1643 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1646 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1648 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1651 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1652 unwrap(BBRef)->eraseFromParent();
1655 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1656 unwrap(BBRef)->removeFromParent();
1659 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1660 unwrap(BB)->moveBefore(unwrap(MovePos));
1663 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1664 unwrap(BB)->moveAfter(unwrap(MovePos));
1667 /*--.. Operations on instructions ..........................................--*/
1669 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1670 return wrap(unwrap<Instruction>(Inst)->getParent());
1673 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1674 BasicBlock *Block = unwrap(BB);
1675 BasicBlock::iterator I = Block->begin();
1676 if (I == Block->end())
1681 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1682 BasicBlock *Block = unwrap(BB);
1683 BasicBlock::iterator I = Block->end();
1684 if (I == Block->begin())
1689 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1690 Instruction *Instr = unwrap<Instruction>(Inst);
1691 BasicBlock::iterator I = Instr;
1692 if (++I == Instr->getParent()->end())
1697 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1698 Instruction *Instr = unwrap<Instruction>(Inst);
1699 BasicBlock::iterator I = Instr;
1700 if (I == Instr->getParent()->begin())
1705 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1706 unwrap<Instruction>(Inst)->eraseFromParent();
1709 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1710 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1711 return (LLVMIntPredicate)I->getPredicate();
1712 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1713 if (CE->getOpcode() == Instruction::ICmp)
1714 return (LLVMIntPredicate)CE->getPredicate();
1715 return (LLVMIntPredicate)0;
1718 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1719 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1720 return map_to_llvmopcode(C->getOpcode());
1721 return (LLVMOpcode)0;
1724 /*--.. Call and invoke instructions ........................................--*/
1726 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1727 Value *V = unwrap(Instr);
1728 if (CallInst *CI = dyn_cast<CallInst>(V))
1729 return CI->getCallingConv();
1730 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1731 return II->getCallingConv();
1732 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1735 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1736 Value *V = unwrap(Instr);
1737 if (CallInst *CI = dyn_cast<CallInst>(V))
1738 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1739 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1740 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1741 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1744 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1746 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1749 Call.getAttributes().addAttributes(Call->getContext(), index,
1750 AttributeSet::get(Call->getContext(),
1754 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1756 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1758 Call.setAttributes(Call.getAttributes()
1759 .removeAttributes(Call->getContext(), index,
1760 AttributeSet::get(Call->getContext(),
1764 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1766 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1768 B.addAlignmentAttr(align);
1769 Call.setAttributes(Call.getAttributes()
1770 .addAttributes(Call->getContext(), index,
1771 AttributeSet::get(Call->getContext(),
1775 /*--.. Operations on call instructions (only) ..............................--*/
1777 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1778 return unwrap<CallInst>(Call)->isTailCall();
1781 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1782 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1785 /*--.. Operations on switch instructions (only) ............................--*/
1787 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1788 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1791 /*--.. Operations on phi nodes .............................................--*/
1793 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1794 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1795 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1796 for (unsigned I = 0; I != Count; ++I)
1797 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1800 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1801 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1804 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1805 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1808 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1809 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1813 /*===-- Instruction builders ----------------------------------------------===*/
1815 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1816 return wrap(new IRBuilder<>(*unwrap(C)));
1819 LLVMBuilderRef LLVMCreateBuilder(void) {
1820 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1823 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1824 LLVMValueRef Instr) {
1825 BasicBlock *BB = unwrap(Block);
1826 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1827 unwrap(Builder)->SetInsertPoint(BB, I);
1830 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1831 Instruction *I = unwrap<Instruction>(Instr);
1832 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1835 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1836 BasicBlock *BB = unwrap(Block);
1837 unwrap(Builder)->SetInsertPoint(BB);
1840 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1841 return wrap(unwrap(Builder)->GetInsertBlock());
1844 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1845 unwrap(Builder)->ClearInsertionPoint();
1848 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1849 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1852 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1854 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1857 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1858 delete unwrap(Builder);
1861 /*--.. Metadata builders ...................................................--*/
1863 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1864 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1865 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1868 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1869 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1870 .getAsMDNode(unwrap(Builder)->getContext()));
1873 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1874 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1878 /*--.. Instruction builders ................................................--*/
1880 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1881 return wrap(unwrap(B)->CreateRetVoid());
1884 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1885 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1888 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1890 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1893 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1894 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1897 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1898 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1899 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1902 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1903 LLVMBasicBlockRef Else, unsigned NumCases) {
1904 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1907 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1908 unsigned NumDests) {
1909 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1912 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1913 LLVMValueRef *Args, unsigned NumArgs,
1914 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1916 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1917 makeArrayRef(unwrap(Args), NumArgs),
1921 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1922 LLVMValueRef PersFn, unsigned NumClauses,
1924 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1925 cast<Function>(unwrap(PersFn)),
1929 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1930 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1933 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1934 return wrap(unwrap(B)->CreateUnreachable());
1937 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1938 LLVMBasicBlockRef Dest) {
1939 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1942 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1943 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1946 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1947 unwrap<LandingPadInst>(LandingPad)->
1948 addClause(cast<Constant>(unwrap(ClauseVal)));
1951 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1952 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1955 /*--.. Arithmetic ..........................................................--*/
1957 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1959 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1962 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1964 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1967 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1969 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1972 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1974 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1977 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1979 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1982 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1984 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1987 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1989 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1992 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1994 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1997 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1999 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2002 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2004 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2007 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2009 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2012 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2014 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2017 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2019 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2022 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2024 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2027 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2028 LLVMValueRef RHS, const char *Name) {
2029 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2032 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2034 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2037 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2039 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2042 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2044 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2047 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2049 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2052 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2054 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2057 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2059 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2062 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2064 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2067 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2069 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2072 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2074 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2077 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2079 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2082 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2083 LLVMValueRef LHS, LLVMValueRef RHS,
2085 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2086 unwrap(RHS), Name));
2089 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2090 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2093 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2095 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2098 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2100 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2103 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2104 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2107 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2108 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2111 /*--.. Memory ..............................................................--*/
2113 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2115 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2116 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2117 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2118 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2119 ITy, unwrap(Ty), AllocSize,
2121 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2124 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2125 LLVMValueRef Val, const char *Name) {
2126 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2127 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2128 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2129 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2130 ITy, unwrap(Ty), AllocSize,
2131 unwrap(Val), 0, "");
2132 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2135 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2137 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2140 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2141 LLVMValueRef Val, const char *Name) {
2142 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2145 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2146 return wrap(unwrap(B)->Insert(
2147 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2151 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2153 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2156 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2157 LLVMValueRef PointerVal) {
2158 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2161 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2162 LLVMValueRef *Indices, unsigned NumIndices,
2164 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2165 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2168 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2169 LLVMValueRef *Indices, unsigned NumIndices,
2171 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2172 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2175 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2176 unsigned Idx, const char *Name) {
2177 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2180 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2182 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2185 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2187 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2190 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2191 Value *P = unwrap<Value>(MemAccessInst);
2192 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2193 return LI->isVolatile();
2194 return cast<StoreInst>(P)->isVolatile();
2197 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2198 Value *P = unwrap<Value>(MemAccessInst);
2199 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2200 return LI->setVolatile(isVolatile);
2201 return cast<StoreInst>(P)->setVolatile(isVolatile);
2204 /*--.. Casts ...............................................................--*/
2206 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2207 LLVMTypeRef DestTy, const char *Name) {
2208 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2211 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2212 LLVMTypeRef DestTy, const char *Name) {
2213 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2216 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2217 LLVMTypeRef DestTy, const char *Name) {
2218 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2221 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2222 LLVMTypeRef DestTy, const char *Name) {
2223 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2226 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2227 LLVMTypeRef DestTy, const char *Name) {
2228 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2231 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2232 LLVMTypeRef DestTy, const char *Name) {
2233 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2236 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2237 LLVMTypeRef DestTy, const char *Name) {
2238 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2241 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2242 LLVMTypeRef DestTy, const char *Name) {
2243 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2246 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2247 LLVMTypeRef DestTy, const char *Name) {
2248 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2251 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2252 LLVMTypeRef DestTy, const char *Name) {
2253 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2256 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2257 LLVMTypeRef DestTy, const char *Name) {
2258 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2261 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2262 LLVMTypeRef DestTy, const char *Name) {
2263 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2266 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2267 LLVMTypeRef DestTy, const char *Name) {
2268 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2272 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2273 LLVMTypeRef DestTy, const char *Name) {
2274 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2278 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2279 LLVMTypeRef DestTy, const char *Name) {
2280 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2284 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2285 LLVMTypeRef DestTy, const char *Name) {
2286 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2287 unwrap(DestTy), Name));
2290 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2291 LLVMTypeRef DestTy, const char *Name) {
2292 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2295 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2296 LLVMTypeRef DestTy, const char *Name) {
2297 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2298 /*isSigned*/true, Name));
2301 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2302 LLVMTypeRef DestTy, const char *Name) {
2303 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2306 /*--.. Comparisons .........................................................--*/
2308 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2309 LLVMValueRef LHS, LLVMValueRef RHS,
2311 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2312 unwrap(LHS), unwrap(RHS), Name));
2315 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2316 LLVMValueRef LHS, LLVMValueRef RHS,
2318 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2319 unwrap(LHS), unwrap(RHS), Name));
2322 /*--.. Miscellaneous instructions ..........................................--*/
2324 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2325 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2328 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2329 LLVMValueRef *Args, unsigned NumArgs,
2331 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2332 makeArrayRef(unwrap(Args), NumArgs),
2336 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2337 LLVMValueRef Then, LLVMValueRef Else,
2339 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2343 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2344 LLVMTypeRef Ty, const char *Name) {
2345 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2348 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2349 LLVMValueRef Index, const char *Name) {
2350 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2354 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2355 LLVMValueRef EltVal, LLVMValueRef Index,
2357 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2358 unwrap(Index), Name));
2361 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2362 LLVMValueRef V2, LLVMValueRef Mask,
2364 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2365 unwrap(Mask), Name));
2368 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2369 unsigned Index, const char *Name) {
2370 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2373 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2374 LLVMValueRef EltVal, unsigned Index,
2376 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2380 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2382 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2385 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2387 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2390 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2391 LLVMValueRef RHS, const char *Name) {
2392 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2395 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2396 LLVMValueRef PTR, LLVMValueRef Val,
2397 LLVMAtomicOrdering ordering,
2398 LLVMBool singleThread) {
2399 AtomicRMWInst::BinOp intop;
2401 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2402 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2403 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2404 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2405 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2406 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2407 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2408 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2409 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2410 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2411 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2413 AtomicOrdering intordering;
2415 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2416 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2417 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2418 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2419 case LLVMAtomicOrderingRelease: intordering = Release; break;
2420 case LLVMAtomicOrderingAcquireRelease:
2421 intordering = AcquireRelease;
2423 case LLVMAtomicOrderingSequentiallyConsistent:
2424 intordering = SequentiallyConsistent;
2427 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2428 intordering, singleThread ? SingleThread : CrossThread));
2432 /*===-- Module providers --------------------------------------------------===*/
2434 LLVMModuleProviderRef
2435 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2436 return reinterpret_cast<LLVMModuleProviderRef>(M);
2439 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2444 /*===-- Memory buffers ----------------------------------------------------===*/
2446 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2448 LLVMMemoryBufferRef *OutMemBuf,
2449 char **OutMessage) {
2451 OwningPtr<MemoryBuffer> MB;
2453 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2454 *OutMemBuf = wrap(MB.take());
2458 *OutMessage = strdup(ec.message().c_str());
2462 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2463 char **OutMessage) {
2464 OwningPtr<MemoryBuffer> MB;
2466 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2467 *OutMemBuf = wrap(MB.take());
2471 *OutMessage = strdup(ec.message().c_str());
2475 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2476 const char *InputData,
2477 size_t InputDataLength,
2478 const char *BufferName,
2479 LLVMBool RequiresNullTerminator) {
2481 return wrap(MemoryBuffer::getMemBuffer(
2482 StringRef(InputData, InputDataLength),
2483 StringRef(BufferName),
2484 RequiresNullTerminator));
2487 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2488 const char *InputData,
2489 size_t InputDataLength,
2490 const char *BufferName) {
2492 return wrap(MemoryBuffer::getMemBufferCopy(
2493 StringRef(InputData, InputDataLength),
2494 StringRef(BufferName)));
2497 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2498 return unwrap(MemBuf)->getBufferStart();
2501 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2502 return unwrap(MemBuf)->getBufferSize();
2505 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2506 delete unwrap(MemBuf);
2509 /*===-- Pass Registry -----------------------------------------------------===*/
2511 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2512 return wrap(PassRegistry::getPassRegistry());
2515 /*===-- Pass Manager ------------------------------------------------------===*/
2517 LLVMPassManagerRef LLVMCreatePassManager() {
2518 return wrap(new PassManager());
2521 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2522 return wrap(new FunctionPassManager(unwrap(M)));
2525 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2526 return LLVMCreateFunctionPassManagerForModule(
2527 reinterpret_cast<LLVMModuleRef>(P));
2530 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2531 return unwrap<PassManager>(PM)->run(*unwrap(M));
2534 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2535 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2538 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2539 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2542 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2543 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2546 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2550 /*===-- Threading ------------------------------------------------------===*/
2552 LLVMBool LLVMStartMultithreaded() {
2553 return llvm_start_multithreaded();
2556 void LLVMStopMultithreaded() {
2557 llvm_stop_multithreaded();
2560 LLVMBool LLVMIsMultithreaded() {
2561 return llvm_is_multithreaded();