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 char *LLVMCreateMessage(const char *Message) {
62 return strdup(Message);
65 void LLVMDisposeMessage(char *Message) {
70 /*===-- Operations on contexts --------------------------------------------===*/
72 LLVMContextRef LLVMContextCreate() {
73 return wrap(new LLVMContext());
76 LLVMContextRef LLVMGetGlobalContext() {
77 return wrap(&getGlobalContext());
80 void LLVMContextDispose(LLVMContextRef C) {
84 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
86 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
89 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
90 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
94 /*===-- Operations on modules ---------------------------------------------===*/
96 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
97 return wrap(new Module(ModuleID, getGlobalContext()));
100 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
102 return wrap(new Module(ModuleID, *unwrap(C)));
105 void LLVMDisposeModule(LLVMModuleRef M) {
109 /*--.. Data layout .........................................................--*/
110 const char * LLVMGetDataLayout(LLVMModuleRef M) {
111 return unwrap(M)->getDataLayout().c_str();
114 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
115 unwrap(M)->setDataLayout(Triple);
118 /*--.. Target triple .......................................................--*/
119 const char * LLVMGetTarget(LLVMModuleRef M) {
120 return unwrap(M)->getTargetTriple().c_str();
123 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
124 unwrap(M)->setTargetTriple(Triple);
127 void LLVMDumpModule(LLVMModuleRef M) {
131 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
132 char **ErrorMessage) {
134 raw_fd_ostream dest(Filename, error);
135 if (!error.empty()) {
136 *ErrorMessage = strdup(error.c_str());
140 unwrap(M)->print(dest, NULL);
142 if (!error.empty()) {
143 *ErrorMessage = strdup(error.c_str());
150 /*--.. Operations on inline assembler ......................................--*/
151 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
152 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
156 /*--.. Operations on module contexts ......................................--*/
157 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
158 return wrap(&unwrap(M)->getContext());
162 /*===-- Operations on types -----------------------------------------------===*/
164 /*--.. Operations on all types (mostly) ....................................--*/
166 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
167 switch (unwrap(Ty)->getTypeID()) {
168 default: llvm_unreachable("Unhandled TypeID.");
170 return LLVMVoidTypeKind;
172 return LLVMHalfTypeKind;
173 case Type::FloatTyID:
174 return LLVMFloatTypeKind;
175 case Type::DoubleTyID:
176 return LLVMDoubleTypeKind;
177 case Type::X86_FP80TyID:
178 return LLVMX86_FP80TypeKind;
179 case Type::FP128TyID:
180 return LLVMFP128TypeKind;
181 case Type::PPC_FP128TyID:
182 return LLVMPPC_FP128TypeKind;
183 case Type::LabelTyID:
184 return LLVMLabelTypeKind;
185 case Type::MetadataTyID:
186 return LLVMMetadataTypeKind;
187 case Type::IntegerTyID:
188 return LLVMIntegerTypeKind;
189 case Type::FunctionTyID:
190 return LLVMFunctionTypeKind;
191 case Type::StructTyID:
192 return LLVMStructTypeKind;
193 case Type::ArrayTyID:
194 return LLVMArrayTypeKind;
195 case Type::PointerTyID:
196 return LLVMPointerTypeKind;
197 case Type::VectorTyID:
198 return LLVMVectorTypeKind;
199 case Type::X86_MMXTyID:
200 return LLVMX86_MMXTypeKind;
204 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
206 return unwrap(Ty)->isSized();
209 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
210 return wrap(&unwrap(Ty)->getContext());
213 /*--.. Operations on integer types .........................................--*/
215 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
216 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
218 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
219 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
221 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
222 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
224 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
225 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
227 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
228 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
230 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
231 return wrap(IntegerType::get(*unwrap(C), NumBits));
234 LLVMTypeRef LLVMInt1Type(void) {
235 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
237 LLVMTypeRef LLVMInt8Type(void) {
238 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
240 LLVMTypeRef LLVMInt16Type(void) {
241 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
243 LLVMTypeRef LLVMInt32Type(void) {
244 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
246 LLVMTypeRef LLVMInt64Type(void) {
247 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
249 LLVMTypeRef LLVMIntType(unsigned NumBits) {
250 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
253 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
254 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
257 /*--.. Operations on real types ............................................--*/
259 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
260 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
262 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
263 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
265 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
266 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
268 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
269 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
271 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
272 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
274 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
275 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
277 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
278 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
281 LLVMTypeRef LLVMHalfType(void) {
282 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
284 LLVMTypeRef LLVMFloatType(void) {
285 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
287 LLVMTypeRef LLVMDoubleType(void) {
288 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
290 LLVMTypeRef LLVMX86FP80Type(void) {
291 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
293 LLVMTypeRef LLVMFP128Type(void) {
294 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
296 LLVMTypeRef LLVMPPCFP128Type(void) {
297 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
299 LLVMTypeRef LLVMX86MMXType(void) {
300 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
303 /*--.. Operations on function types ........................................--*/
305 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
306 LLVMTypeRef *ParamTypes, unsigned ParamCount,
308 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
309 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
312 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
313 return unwrap<FunctionType>(FunctionTy)->isVarArg();
316 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
317 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
320 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
321 return unwrap<FunctionType>(FunctionTy)->getNumParams();
324 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
325 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
326 for (FunctionType::param_iterator I = Ty->param_begin(),
327 E = Ty->param_end(); I != E; ++I)
331 /*--.. Operations on struct types ..........................................--*/
333 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
334 unsigned ElementCount, LLVMBool Packed) {
335 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
336 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
339 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
340 unsigned ElementCount, LLVMBool Packed) {
341 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
342 ElementCount, Packed);
345 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
347 return wrap(StructType::create(*unwrap(C), Name));
350 const char *LLVMGetStructName(LLVMTypeRef Ty)
352 StructType *Type = unwrap<StructType>(Ty);
353 if (!Type->hasName())
355 return Type->getName().data();
358 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
359 unsigned ElementCount, LLVMBool Packed) {
360 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
361 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
364 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
365 return unwrap<StructType>(StructTy)->getNumElements();
368 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
369 StructType *Ty = unwrap<StructType>(StructTy);
370 for (StructType::element_iterator I = Ty->element_begin(),
371 E = Ty->element_end(); I != E; ++I)
375 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
376 return unwrap<StructType>(StructTy)->isPacked();
379 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
380 return unwrap<StructType>(StructTy)->isOpaque();
383 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
384 return wrap(unwrap(M)->getTypeByName(Name));
387 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
389 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
390 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
393 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
394 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
397 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
398 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
401 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
402 return wrap(unwrap<SequentialType>(Ty)->getElementType());
405 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
406 return unwrap<ArrayType>(ArrayTy)->getNumElements();
409 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
410 return unwrap<PointerType>(PointerTy)->getAddressSpace();
413 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
414 return unwrap<VectorType>(VectorTy)->getNumElements();
417 /*--.. Operations on other types ...........................................--*/
419 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
420 return wrap(Type::getVoidTy(*unwrap(C)));
422 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
423 return wrap(Type::getLabelTy(*unwrap(C)));
426 LLVMTypeRef LLVMVoidType(void) {
427 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
429 LLVMTypeRef LLVMLabelType(void) {
430 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
433 /*===-- Operations on values ----------------------------------------------===*/
435 /*--.. Operations on all values ............................................--*/
437 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
438 return wrap(unwrap(Val)->getType());
441 const char *LLVMGetValueName(LLVMValueRef Val) {
442 return unwrap(Val)->getName().data();
445 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
446 unwrap(Val)->setName(Name);
449 void LLVMDumpValue(LLVMValueRef Val) {
453 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
454 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
457 int LLVMHasMetadata(LLVMValueRef Inst) {
458 return unwrap<Instruction>(Inst)->hasMetadata();
461 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
462 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
465 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
466 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
469 /*--.. Conversion functions ................................................--*/
471 #define LLVM_DEFINE_VALUE_CAST(name) \
472 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
473 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
476 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
478 /*--.. Operations on Uses ..................................................--*/
479 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
480 Value *V = unwrap(Val);
481 Value::use_iterator I = V->use_begin();
482 if (I == V->use_end())
484 return wrap(&(I.getUse()));
487 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
488 Use *Next = unwrap(U)->getNext();
494 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
495 return wrap(unwrap(U)->getUser());
498 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
499 return wrap(unwrap(U)->get());
502 /*--.. Operations on Users .................................................--*/
503 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
504 Value *V = unwrap(Val);
505 if (MDNode *MD = dyn_cast<MDNode>(V))
506 return wrap(MD->getOperand(Index));
507 return wrap(cast<User>(V)->getOperand(Index));
510 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
511 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
514 int LLVMGetNumOperands(LLVMValueRef Val) {
515 Value *V = unwrap(Val);
516 if (MDNode *MD = dyn_cast<MDNode>(V))
517 return MD->getNumOperands();
518 return cast<User>(V)->getNumOperands();
521 /*--.. Operations on constants of any type .................................--*/
523 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
524 return wrap(Constant::getNullValue(unwrap(Ty)));
527 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
528 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
531 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
532 return wrap(UndefValue::get(unwrap(Ty)));
535 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
536 return isa<Constant>(unwrap(Ty));
539 LLVMBool LLVMIsNull(LLVMValueRef Val) {
540 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
541 return C->isNullValue();
545 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
546 return isa<UndefValue>(unwrap(Val));
549 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
551 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
554 /*--.. Operations on metadata nodes ........................................--*/
556 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
558 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
561 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
562 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
565 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
567 return wrap(MDNode::get(*unwrap(C),
568 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
571 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
572 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
575 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
576 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
577 *Len = S->getString().size();
578 return S->getString().data();
584 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
586 return cast<MDNode>(unwrap(V))->getNumOperands();
589 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
591 const MDNode *N = cast<MDNode>(unwrap(V));
592 const unsigned numOperands = N->getNumOperands();
593 for (unsigned i = 0; i < numOperands; i++)
594 Dest[i] = wrap(N->getOperand(i));
597 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
599 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
600 return N->getNumOperands();
605 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
607 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
610 for (unsigned i=0;i<N->getNumOperands();i++)
611 Dest[i] = wrap(N->getOperand(i));
614 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
617 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
620 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
625 /*--.. Operations on scalar constants ......................................--*/
627 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
628 LLVMBool SignExtend) {
629 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
632 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
634 const uint64_t Words[]) {
635 IntegerType *Ty = unwrap<IntegerType>(IntTy);
636 return wrap(ConstantInt::get(Ty->getContext(),
637 APInt(Ty->getBitWidth(),
638 makeArrayRef(Words, NumWords))));
641 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
643 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
647 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
648 unsigned SLen, uint8_t Radix) {
649 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
653 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
654 return wrap(ConstantFP::get(unwrap(RealTy), N));
657 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
658 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
661 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
663 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
666 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
667 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
670 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
671 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
674 /*--.. Operations on composite constants ...................................--*/
676 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
678 LLVMBool DontNullTerminate) {
679 /* Inverted the sense of AddNull because ', 0)' is a
680 better mnemonic for null termination than ', 1)'. */
681 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
682 DontNullTerminate == 0));
684 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
685 LLVMValueRef *ConstantVals,
686 unsigned Count, LLVMBool Packed) {
687 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
688 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
692 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
693 LLVMBool DontNullTerminate) {
694 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
697 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
698 LLVMValueRef *ConstantVals, unsigned Length) {
699 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
700 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
702 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
704 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
708 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
709 LLVMValueRef *ConstantVals,
711 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
712 StructType *Ty = cast<StructType>(unwrap(StructTy));
714 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
717 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
718 return wrap(ConstantVector::get(makeArrayRef(
719 unwrap<Constant>(ScalarConstantVals, Size), Size)));
722 /*-- Opcode mapping */
724 static LLVMOpcode map_to_llvmopcode(int opcode)
727 default: llvm_unreachable("Unhandled Opcode.");
728 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
729 #include "llvm/IR/Instruction.def"
734 static int map_from_llvmopcode(LLVMOpcode code)
737 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
738 #include "llvm/IR/Instruction.def"
741 llvm_unreachable("Unhandled Opcode.");
744 /*--.. Constant expressions ................................................--*/
746 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
747 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
750 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
751 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
754 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
755 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
758 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
759 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
762 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
763 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
766 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
767 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
771 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
772 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
775 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
776 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
779 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
780 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
781 unwrap<Constant>(RHSConstant)));
784 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
785 LLVMValueRef RHSConstant) {
786 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
787 unwrap<Constant>(RHSConstant)));
790 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
791 LLVMValueRef RHSConstant) {
792 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
793 unwrap<Constant>(RHSConstant)));
796 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
797 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
798 unwrap<Constant>(RHSConstant)));
801 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
802 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
803 unwrap<Constant>(RHSConstant)));
806 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
807 LLVMValueRef RHSConstant) {
808 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
809 unwrap<Constant>(RHSConstant)));
812 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
813 LLVMValueRef RHSConstant) {
814 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
815 unwrap<Constant>(RHSConstant)));
818 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
819 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
820 unwrap<Constant>(RHSConstant)));
823 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
824 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
825 unwrap<Constant>(RHSConstant)));
828 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
829 LLVMValueRef RHSConstant) {
830 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
831 unwrap<Constant>(RHSConstant)));
834 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
835 LLVMValueRef RHSConstant) {
836 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
837 unwrap<Constant>(RHSConstant)));
840 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
841 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
842 unwrap<Constant>(RHSConstant)));
845 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
846 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
847 unwrap<Constant>(RHSConstant)));
850 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
851 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
852 unwrap<Constant>(RHSConstant)));
855 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
856 LLVMValueRef RHSConstant) {
857 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
858 unwrap<Constant>(RHSConstant)));
861 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
862 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
863 unwrap<Constant>(RHSConstant)));
866 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
867 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
868 unwrap<Constant>(RHSConstant)));
871 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
872 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
873 unwrap<Constant>(RHSConstant)));
876 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
877 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
878 unwrap<Constant>(RHSConstant)));
881 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
882 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
883 unwrap<Constant>(RHSConstant)));
886 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
887 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
888 unwrap<Constant>(RHSConstant)));
891 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
892 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
893 unwrap<Constant>(RHSConstant)));
896 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
897 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
898 return wrap(ConstantExpr::getICmp(Predicate,
899 unwrap<Constant>(LHSConstant),
900 unwrap<Constant>(RHSConstant)));
903 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
904 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
905 return wrap(ConstantExpr::getFCmp(Predicate,
906 unwrap<Constant>(LHSConstant),
907 unwrap<Constant>(RHSConstant)));
910 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
911 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
912 unwrap<Constant>(RHSConstant)));
915 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
916 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
921 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
922 unwrap<Constant>(RHSConstant)));
925 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
926 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
927 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
929 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
933 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
934 LLVMValueRef *ConstantIndices,
935 unsigned NumIndices) {
936 Constant* Val = unwrap<Constant>(ConstantVal);
937 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
939 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
942 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
943 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
947 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
948 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
952 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
953 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
957 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
958 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
962 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
963 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
967 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
968 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
972 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
973 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
977 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
978 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
982 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
983 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
987 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
988 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
992 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
993 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
997 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
998 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1002 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1003 LLVMTypeRef ToType) {
1004 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1008 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1009 LLVMTypeRef ToType) {
1010 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1014 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1015 LLVMTypeRef ToType) {
1016 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1020 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1021 LLVMTypeRef ToType) {
1022 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1026 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1027 LLVMBool isSigned) {
1028 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1029 unwrap(ToType), isSigned));
1032 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1033 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1037 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1038 LLVMValueRef ConstantIfTrue,
1039 LLVMValueRef ConstantIfFalse) {
1040 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1041 unwrap<Constant>(ConstantIfTrue),
1042 unwrap<Constant>(ConstantIfFalse)));
1045 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1046 LLVMValueRef IndexConstant) {
1047 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1048 unwrap<Constant>(IndexConstant)));
1051 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1052 LLVMValueRef ElementValueConstant,
1053 LLVMValueRef IndexConstant) {
1054 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1055 unwrap<Constant>(ElementValueConstant),
1056 unwrap<Constant>(IndexConstant)));
1059 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1060 LLVMValueRef VectorBConstant,
1061 LLVMValueRef MaskConstant) {
1062 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1063 unwrap<Constant>(VectorBConstant),
1064 unwrap<Constant>(MaskConstant)));
1067 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1069 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1070 makeArrayRef(IdxList, NumIdx)));
1073 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1074 LLVMValueRef ElementValueConstant,
1075 unsigned *IdxList, unsigned NumIdx) {
1076 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1077 unwrap<Constant>(ElementValueConstant),
1078 makeArrayRef(IdxList, NumIdx)));
1081 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1082 const char *Constraints,
1083 LLVMBool HasSideEffects,
1084 LLVMBool IsAlignStack) {
1085 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1086 Constraints, HasSideEffects, IsAlignStack));
1089 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1090 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1093 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1095 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1096 return wrap(unwrap<GlobalValue>(Global)->getParent());
1099 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1100 return unwrap<GlobalValue>(Global)->isDeclaration();
1103 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1104 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1105 case GlobalValue::ExternalLinkage:
1106 return LLVMExternalLinkage;
1107 case GlobalValue::AvailableExternallyLinkage:
1108 return LLVMAvailableExternallyLinkage;
1109 case GlobalValue::LinkOnceAnyLinkage:
1110 return LLVMLinkOnceAnyLinkage;
1111 case GlobalValue::LinkOnceODRLinkage:
1112 return LLVMLinkOnceODRLinkage;
1113 case GlobalValue::LinkOnceODRAutoHideLinkage:
1114 return LLVMLinkOnceODRAutoHideLinkage;
1115 case GlobalValue::WeakAnyLinkage:
1116 return LLVMWeakAnyLinkage;
1117 case GlobalValue::WeakODRLinkage:
1118 return LLVMWeakODRLinkage;
1119 case GlobalValue::AppendingLinkage:
1120 return LLVMAppendingLinkage;
1121 case GlobalValue::InternalLinkage:
1122 return LLVMInternalLinkage;
1123 case GlobalValue::PrivateLinkage:
1124 return LLVMPrivateLinkage;
1125 case GlobalValue::LinkerPrivateLinkage:
1126 return LLVMLinkerPrivateLinkage;
1127 case GlobalValue::LinkerPrivateWeakLinkage:
1128 return LLVMLinkerPrivateWeakLinkage;
1129 case GlobalValue::DLLImportLinkage:
1130 return LLVMDLLImportLinkage;
1131 case GlobalValue::DLLExportLinkage:
1132 return LLVMDLLExportLinkage;
1133 case GlobalValue::ExternalWeakLinkage:
1134 return LLVMExternalWeakLinkage;
1135 case GlobalValue::CommonLinkage:
1136 return LLVMCommonLinkage;
1139 llvm_unreachable("Invalid GlobalValue linkage!");
1142 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1143 GlobalValue *GV = unwrap<GlobalValue>(Global);
1146 case LLVMExternalLinkage:
1147 GV->setLinkage(GlobalValue::ExternalLinkage);
1149 case LLVMAvailableExternallyLinkage:
1150 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1152 case LLVMLinkOnceAnyLinkage:
1153 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1155 case LLVMLinkOnceODRLinkage:
1156 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1158 case LLVMLinkOnceODRAutoHideLinkage:
1159 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1161 case LLVMWeakAnyLinkage:
1162 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1164 case LLVMWeakODRLinkage:
1165 GV->setLinkage(GlobalValue::WeakODRLinkage);
1167 case LLVMAppendingLinkage:
1168 GV->setLinkage(GlobalValue::AppendingLinkage);
1170 case LLVMInternalLinkage:
1171 GV->setLinkage(GlobalValue::InternalLinkage);
1173 case LLVMPrivateLinkage:
1174 GV->setLinkage(GlobalValue::PrivateLinkage);
1176 case LLVMLinkerPrivateLinkage:
1177 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1179 case LLVMLinkerPrivateWeakLinkage:
1180 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1182 case LLVMDLLImportLinkage:
1183 GV->setLinkage(GlobalValue::DLLImportLinkage);
1185 case LLVMDLLExportLinkage:
1186 GV->setLinkage(GlobalValue::DLLExportLinkage);
1188 case LLVMExternalWeakLinkage:
1189 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1191 case LLVMGhostLinkage:
1193 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1195 case LLVMCommonLinkage:
1196 GV->setLinkage(GlobalValue::CommonLinkage);
1201 const char *LLVMGetSection(LLVMValueRef Global) {
1202 return unwrap<GlobalValue>(Global)->getSection().c_str();
1205 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1206 unwrap<GlobalValue>(Global)->setSection(Section);
1209 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1210 return static_cast<LLVMVisibility>(
1211 unwrap<GlobalValue>(Global)->getVisibility());
1214 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1215 unwrap<GlobalValue>(Global)
1216 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1219 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1220 return unwrap<GlobalValue>(Global)->getAlignment();
1223 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1224 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1227 /*--.. Operations on global variables ......................................--*/
1229 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1230 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1231 GlobalValue::ExternalLinkage, 0, Name));
1234 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1236 unsigned AddressSpace) {
1237 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1238 GlobalValue::ExternalLinkage, 0, Name, 0,
1239 GlobalVariable::NotThreadLocal, AddressSpace));
1242 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1243 return wrap(unwrap(M)->getNamedGlobal(Name));
1246 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1247 Module *Mod = unwrap(M);
1248 Module::global_iterator I = Mod->global_begin();
1249 if (I == Mod->global_end())
1254 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1255 Module *Mod = unwrap(M);
1256 Module::global_iterator I = Mod->global_end();
1257 if (I == Mod->global_begin())
1262 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1263 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1264 Module::global_iterator I = GV;
1265 if (++I == GV->getParent()->global_end())
1270 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1271 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1272 Module::global_iterator I = GV;
1273 if (I == GV->getParent()->global_begin())
1278 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1279 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1282 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1283 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1284 if ( !GV->hasInitializer() )
1286 return wrap(GV->getInitializer());
1289 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1290 unwrap<GlobalVariable>(GlobalVar)
1291 ->setInitializer(unwrap<Constant>(ConstantVal));
1294 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1295 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1298 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1299 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1302 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1303 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1306 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1307 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1310 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1311 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1312 case GlobalVariable::NotThreadLocal:
1313 return LLVMNotThreadLocal;
1314 case GlobalVariable::GeneralDynamicTLSModel:
1315 return LLVMGeneralDynamicTLSModel;
1316 case GlobalVariable::LocalDynamicTLSModel:
1317 return LLVMLocalDynamicTLSModel;
1318 case GlobalVariable::InitialExecTLSModel:
1319 return LLVMInitialExecTLSModel;
1320 case GlobalVariable::LocalExecTLSModel:
1321 return LLVMLocalExecTLSModel;
1324 llvm_unreachable("Invalid GlobalVariable thread local mode");
1327 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1328 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1331 case LLVMNotThreadLocal:
1332 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1334 case LLVMGeneralDynamicTLSModel:
1335 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1337 case LLVMLocalDynamicTLSModel:
1338 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1340 case LLVMInitialExecTLSModel:
1341 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1343 case LLVMLocalExecTLSModel:
1344 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1349 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1350 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1353 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1354 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1357 /*--.. Operations on aliases ......................................--*/
1359 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1361 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1362 unwrap<Constant>(Aliasee), unwrap (M)));
1365 /*--.. Operations on functions .............................................--*/
1367 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1368 LLVMTypeRef FunctionTy) {
1369 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1370 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1373 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1374 return wrap(unwrap(M)->getFunction(Name));
1377 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1378 Module *Mod = unwrap(M);
1379 Module::iterator I = Mod->begin();
1380 if (I == Mod->end())
1385 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1386 Module *Mod = unwrap(M);
1387 Module::iterator I = Mod->end();
1388 if (I == Mod->begin())
1393 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1394 Function *Func = unwrap<Function>(Fn);
1395 Module::iterator I = Func;
1396 if (++I == Func->getParent()->end())
1401 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1402 Function *Func = unwrap<Function>(Fn);
1403 Module::iterator I = Func;
1404 if (I == Func->getParent()->begin())
1409 void LLVMDeleteFunction(LLVMValueRef Fn) {
1410 unwrap<Function>(Fn)->eraseFromParent();
1413 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1414 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1415 return F->getIntrinsicID();
1419 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1420 return unwrap<Function>(Fn)->getCallingConv();
1423 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1424 return unwrap<Function>(Fn)->setCallingConv(
1425 static_cast<CallingConv::ID>(CC));
1428 const char *LLVMGetGC(LLVMValueRef Fn) {
1429 Function *F = unwrap<Function>(Fn);
1430 return F->hasGC()? F->getGC() : 0;
1433 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1434 Function *F = unwrap<Function>(Fn);
1441 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1442 Function *Func = unwrap<Function>(Fn);
1443 const AttributeSet PAL = Func->getAttributes();
1445 const AttributeSet PALnew =
1446 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1447 AttributeSet::get(Func->getContext(),
1448 AttributeSet::FunctionIndex, B));
1449 Func->setAttributes(PALnew);
1452 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1454 Function *Func = unwrap<Function>(Fn);
1455 AttributeSet::AttrIndex Idx =
1456 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1459 B.addAttribute(A, V);
1460 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1461 Func->addAttributes(Idx, Set);
1464 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1465 Function *Func = unwrap<Function>(Fn);
1466 const AttributeSet PAL = Func->getAttributes();
1468 const AttributeSet PALnew =
1469 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1470 AttributeSet::get(Func->getContext(),
1471 AttributeSet::FunctionIndex, B));
1472 Func->setAttributes(PALnew);
1475 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1476 Function *Func = unwrap<Function>(Fn);
1477 const AttributeSet PAL = Func->getAttributes();
1478 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1481 /*--.. Operations on parameters ............................................--*/
1483 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1484 // This function is strictly redundant to
1485 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1486 return unwrap<Function>(FnRef)->arg_size();
1489 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1490 Function *Fn = unwrap<Function>(FnRef);
1491 for (Function::arg_iterator I = Fn->arg_begin(),
1492 E = Fn->arg_end(); I != E; I++)
1493 *ParamRefs++ = wrap(I);
1496 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1497 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1503 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1504 return wrap(unwrap<Argument>(V)->getParent());
1507 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1508 Function *Func = unwrap<Function>(Fn);
1509 Function::arg_iterator I = Func->arg_begin();
1510 if (I == Func->arg_end())
1515 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1516 Function *Func = unwrap<Function>(Fn);
1517 Function::arg_iterator I = Func->arg_end();
1518 if (I == Func->arg_begin())
1523 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1524 Argument *A = unwrap<Argument>(Arg);
1525 Function::arg_iterator I = A;
1526 if (++I == A->getParent()->arg_end())
1531 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1532 Argument *A = unwrap<Argument>(Arg);
1533 Function::arg_iterator I = A;
1534 if (I == A->getParent()->arg_begin())
1539 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1540 Argument *A = unwrap<Argument>(Arg);
1542 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1545 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1546 Argument *A = unwrap<Argument>(Arg);
1548 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1551 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1552 Argument *A = unwrap<Argument>(Arg);
1553 return (LLVMAttribute)A->getParent()->getAttributes().
1554 Raw(A->getArgNo()+1);
1558 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1559 Argument *A = unwrap<Argument>(Arg);
1561 B.addAlignmentAttr(align);
1562 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1565 /*--.. Operations on basic blocks ..........................................--*/
1567 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1568 return wrap(static_cast<Value*>(unwrap(BB)));
1571 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1572 return isa<BasicBlock>(unwrap(Val));
1575 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1576 return wrap(unwrap<BasicBlock>(Val));
1579 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1580 return wrap(unwrap(BB)->getParent());
1583 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1584 return wrap(unwrap(BB)->getTerminator());
1587 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1588 return unwrap<Function>(FnRef)->size();
1591 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1592 Function *Fn = unwrap<Function>(FnRef);
1593 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1594 *BasicBlocksRefs++ = wrap(I);
1597 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1598 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1601 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1602 Function *Func = unwrap<Function>(Fn);
1603 Function::iterator I = Func->begin();
1604 if (I == Func->end())
1609 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1610 Function *Func = unwrap<Function>(Fn);
1611 Function::iterator I = Func->end();
1612 if (I == Func->begin())
1617 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1618 BasicBlock *Block = unwrap(BB);
1619 Function::iterator I = Block;
1620 if (++I == Block->getParent()->end())
1625 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1626 BasicBlock *Block = unwrap(BB);
1627 Function::iterator I = Block;
1628 if (I == Block->getParent()->begin())
1633 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1636 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1639 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1640 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1643 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1644 LLVMBasicBlockRef BBRef,
1646 BasicBlock *BB = unwrap(BBRef);
1647 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1650 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1652 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1655 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1656 unwrap(BBRef)->eraseFromParent();
1659 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1660 unwrap(BBRef)->removeFromParent();
1663 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1664 unwrap(BB)->moveBefore(unwrap(MovePos));
1667 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1668 unwrap(BB)->moveAfter(unwrap(MovePos));
1671 /*--.. Operations on instructions ..........................................--*/
1673 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1674 return wrap(unwrap<Instruction>(Inst)->getParent());
1677 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1678 BasicBlock *Block = unwrap(BB);
1679 BasicBlock::iterator I = Block->begin();
1680 if (I == Block->end())
1685 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1686 BasicBlock *Block = unwrap(BB);
1687 BasicBlock::iterator I = Block->end();
1688 if (I == Block->begin())
1693 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1694 Instruction *Instr = unwrap<Instruction>(Inst);
1695 BasicBlock::iterator I = Instr;
1696 if (++I == Instr->getParent()->end())
1701 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1702 Instruction *Instr = unwrap<Instruction>(Inst);
1703 BasicBlock::iterator I = Instr;
1704 if (I == Instr->getParent()->begin())
1709 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1710 unwrap<Instruction>(Inst)->eraseFromParent();
1713 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1714 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1715 return (LLVMIntPredicate)I->getPredicate();
1716 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1717 if (CE->getOpcode() == Instruction::ICmp)
1718 return (LLVMIntPredicate)CE->getPredicate();
1719 return (LLVMIntPredicate)0;
1722 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1723 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1724 return map_to_llvmopcode(C->getOpcode());
1725 return (LLVMOpcode)0;
1728 /*--.. Call and invoke instructions ........................................--*/
1730 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1731 Value *V = unwrap(Instr);
1732 if (CallInst *CI = dyn_cast<CallInst>(V))
1733 return CI->getCallingConv();
1734 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1735 return II->getCallingConv();
1736 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1739 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1740 Value *V = unwrap(Instr);
1741 if (CallInst *CI = dyn_cast<CallInst>(V))
1742 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1743 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1744 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1745 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1748 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1750 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1753 Call.getAttributes().addAttributes(Call->getContext(), index,
1754 AttributeSet::get(Call->getContext(),
1758 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1760 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1762 Call.setAttributes(Call.getAttributes()
1763 .removeAttributes(Call->getContext(), index,
1764 AttributeSet::get(Call->getContext(),
1768 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1770 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1772 B.addAlignmentAttr(align);
1773 Call.setAttributes(Call.getAttributes()
1774 .addAttributes(Call->getContext(), index,
1775 AttributeSet::get(Call->getContext(),
1779 /*--.. Operations on call instructions (only) ..............................--*/
1781 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1782 return unwrap<CallInst>(Call)->isTailCall();
1785 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1786 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1789 /*--.. Operations on switch instructions (only) ............................--*/
1791 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1792 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1795 /*--.. Operations on phi nodes .............................................--*/
1797 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1798 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1799 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1800 for (unsigned I = 0; I != Count; ++I)
1801 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1804 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1805 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1808 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1809 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1812 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1813 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1817 /*===-- Instruction builders ----------------------------------------------===*/
1819 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1820 return wrap(new IRBuilder<>(*unwrap(C)));
1823 LLVMBuilderRef LLVMCreateBuilder(void) {
1824 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1827 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1828 LLVMValueRef Instr) {
1829 BasicBlock *BB = unwrap(Block);
1830 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1831 unwrap(Builder)->SetInsertPoint(BB, I);
1834 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1835 Instruction *I = unwrap<Instruction>(Instr);
1836 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1839 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1840 BasicBlock *BB = unwrap(Block);
1841 unwrap(Builder)->SetInsertPoint(BB);
1844 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1845 return wrap(unwrap(Builder)->GetInsertBlock());
1848 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1849 unwrap(Builder)->ClearInsertionPoint();
1852 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1853 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1856 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1858 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1861 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1862 delete unwrap(Builder);
1865 /*--.. Metadata builders ...................................................--*/
1867 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1868 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1869 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1872 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1873 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1874 .getAsMDNode(unwrap(Builder)->getContext()));
1877 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1878 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1882 /*--.. Instruction builders ................................................--*/
1884 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1885 return wrap(unwrap(B)->CreateRetVoid());
1888 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1889 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1892 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1894 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1897 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1898 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1901 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1902 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1903 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1906 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1907 LLVMBasicBlockRef Else, unsigned NumCases) {
1908 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1911 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1912 unsigned NumDests) {
1913 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1916 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1917 LLVMValueRef *Args, unsigned NumArgs,
1918 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1920 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1921 makeArrayRef(unwrap(Args), NumArgs),
1925 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1926 LLVMValueRef PersFn, unsigned NumClauses,
1928 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1929 cast<Function>(unwrap(PersFn)),
1933 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1934 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1937 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1938 return wrap(unwrap(B)->CreateUnreachable());
1941 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1942 LLVMBasicBlockRef Dest) {
1943 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1946 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1947 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1950 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1951 unwrap<LandingPadInst>(LandingPad)->
1952 addClause(cast<Constant>(unwrap(ClauseVal)));
1955 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1956 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1959 /*--.. Arithmetic ..........................................................--*/
1961 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1963 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1966 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1968 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1971 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1973 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1976 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1978 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1981 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1983 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1986 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1988 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1991 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1993 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1996 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1998 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2001 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2003 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2006 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2008 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2011 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2013 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2016 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2018 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2021 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2023 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2026 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2028 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2031 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2032 LLVMValueRef RHS, const char *Name) {
2033 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2036 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2038 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2041 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2043 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2046 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2048 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2051 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2053 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2056 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2058 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2061 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2063 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2066 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2068 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2071 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2073 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2076 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2078 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2081 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2083 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2086 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2087 LLVMValueRef LHS, LLVMValueRef RHS,
2089 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2090 unwrap(RHS), Name));
2093 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2094 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2097 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2099 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2102 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2104 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2107 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2108 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2111 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2112 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2115 /*--.. Memory ..............................................................--*/
2117 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2119 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2120 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2121 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2122 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2123 ITy, unwrap(Ty), AllocSize,
2125 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2128 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2129 LLVMValueRef Val, const char *Name) {
2130 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2131 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2132 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2133 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2134 ITy, unwrap(Ty), AllocSize,
2135 unwrap(Val), 0, "");
2136 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2139 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2141 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2144 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2145 LLVMValueRef Val, const char *Name) {
2146 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2149 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2150 return wrap(unwrap(B)->Insert(
2151 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2155 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2157 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2160 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2161 LLVMValueRef PointerVal) {
2162 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2165 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2166 LLVMValueRef *Indices, unsigned NumIndices,
2168 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2169 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2172 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2173 LLVMValueRef *Indices, unsigned NumIndices,
2175 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2176 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2179 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2180 unsigned Idx, const char *Name) {
2181 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2184 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2186 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2189 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2191 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2194 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2195 Value *P = unwrap<Value>(MemAccessInst);
2196 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2197 return LI->isVolatile();
2198 return cast<StoreInst>(P)->isVolatile();
2201 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2202 Value *P = unwrap<Value>(MemAccessInst);
2203 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2204 return LI->setVolatile(isVolatile);
2205 return cast<StoreInst>(P)->setVolatile(isVolatile);
2208 /*--.. Casts ...............................................................--*/
2210 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2211 LLVMTypeRef DestTy, const char *Name) {
2212 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2215 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2216 LLVMTypeRef DestTy, const char *Name) {
2217 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2220 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2221 LLVMTypeRef DestTy, const char *Name) {
2222 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2225 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2226 LLVMTypeRef DestTy, const char *Name) {
2227 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2230 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2231 LLVMTypeRef DestTy, const char *Name) {
2232 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2235 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2236 LLVMTypeRef DestTy, const char *Name) {
2237 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2240 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2241 LLVMTypeRef DestTy, const char *Name) {
2242 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2245 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2246 LLVMTypeRef DestTy, const char *Name) {
2247 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2250 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2251 LLVMTypeRef DestTy, const char *Name) {
2252 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2255 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2256 LLVMTypeRef DestTy, const char *Name) {
2257 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2260 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2261 LLVMTypeRef DestTy, const char *Name) {
2262 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2265 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2266 LLVMTypeRef DestTy, const char *Name) {
2267 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2270 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2271 LLVMTypeRef DestTy, const char *Name) {
2272 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2276 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2277 LLVMTypeRef DestTy, const char *Name) {
2278 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2282 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2283 LLVMTypeRef DestTy, const char *Name) {
2284 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2288 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2289 LLVMTypeRef DestTy, const char *Name) {
2290 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2291 unwrap(DestTy), Name));
2294 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2295 LLVMTypeRef DestTy, const char *Name) {
2296 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2299 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2300 LLVMTypeRef DestTy, const char *Name) {
2301 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2302 /*isSigned*/true, Name));
2305 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2306 LLVMTypeRef DestTy, const char *Name) {
2307 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2310 /*--.. Comparisons .........................................................--*/
2312 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2313 LLVMValueRef LHS, LLVMValueRef RHS,
2315 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2316 unwrap(LHS), unwrap(RHS), Name));
2319 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2320 LLVMValueRef LHS, LLVMValueRef RHS,
2322 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2323 unwrap(LHS), unwrap(RHS), Name));
2326 /*--.. Miscellaneous instructions ..........................................--*/
2328 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2329 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2332 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2333 LLVMValueRef *Args, unsigned NumArgs,
2335 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2336 makeArrayRef(unwrap(Args), NumArgs),
2340 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2341 LLVMValueRef Then, LLVMValueRef Else,
2343 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2347 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2348 LLVMTypeRef Ty, const char *Name) {
2349 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2352 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2353 LLVMValueRef Index, const char *Name) {
2354 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2358 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2359 LLVMValueRef EltVal, LLVMValueRef Index,
2361 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2362 unwrap(Index), Name));
2365 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2366 LLVMValueRef V2, LLVMValueRef Mask,
2368 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2369 unwrap(Mask), Name));
2372 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2373 unsigned Index, const char *Name) {
2374 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2377 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2378 LLVMValueRef EltVal, unsigned Index,
2380 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2384 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2386 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2389 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2391 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2394 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2395 LLVMValueRef RHS, const char *Name) {
2396 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2399 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2400 LLVMValueRef PTR, LLVMValueRef Val,
2401 LLVMAtomicOrdering ordering,
2402 LLVMBool singleThread) {
2403 AtomicRMWInst::BinOp intop;
2405 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2406 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2407 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2408 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2409 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2410 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2411 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2412 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2413 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2414 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2415 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2417 AtomicOrdering intordering;
2419 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2420 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2421 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2422 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2423 case LLVMAtomicOrderingRelease: intordering = Release; break;
2424 case LLVMAtomicOrderingAcquireRelease:
2425 intordering = AcquireRelease;
2427 case LLVMAtomicOrderingSequentiallyConsistent:
2428 intordering = SequentiallyConsistent;
2431 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2432 intordering, singleThread ? SingleThread : CrossThread));
2436 /*===-- Module providers --------------------------------------------------===*/
2438 LLVMModuleProviderRef
2439 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2440 return reinterpret_cast<LLVMModuleProviderRef>(M);
2443 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2448 /*===-- Memory buffers ----------------------------------------------------===*/
2450 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2452 LLVMMemoryBufferRef *OutMemBuf,
2453 char **OutMessage) {
2455 OwningPtr<MemoryBuffer> MB;
2457 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2458 *OutMemBuf = wrap(MB.take());
2462 *OutMessage = strdup(ec.message().c_str());
2466 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2467 char **OutMessage) {
2468 OwningPtr<MemoryBuffer> MB;
2470 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2471 *OutMemBuf = wrap(MB.take());
2475 *OutMessage = strdup(ec.message().c_str());
2479 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2480 const char *InputData,
2481 size_t InputDataLength,
2482 const char *BufferName,
2483 LLVMBool RequiresNullTerminator) {
2485 return wrap(MemoryBuffer::getMemBuffer(
2486 StringRef(InputData, InputDataLength),
2487 StringRef(BufferName),
2488 RequiresNullTerminator));
2491 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2492 const char *InputData,
2493 size_t InputDataLength,
2494 const char *BufferName) {
2496 return wrap(MemoryBuffer::getMemBufferCopy(
2497 StringRef(InputData, InputDataLength),
2498 StringRef(BufferName)));
2501 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2502 return unwrap(MemBuf)->getBufferStart();
2505 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2506 return unwrap(MemBuf)->getBufferSize();
2509 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2510 delete unwrap(MemBuf);
2513 /*===-- Pass Registry -----------------------------------------------------===*/
2515 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2516 return wrap(PassRegistry::getPassRegistry());
2519 /*===-- Pass Manager ------------------------------------------------------===*/
2521 LLVMPassManagerRef LLVMCreatePassManager() {
2522 return wrap(new PassManager());
2525 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2526 return wrap(new FunctionPassManager(unwrap(M)));
2529 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2530 return LLVMCreateFunctionPassManagerForModule(
2531 reinterpret_cast<LLVMModuleRef>(P));
2534 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2535 return unwrap<PassManager>(PM)->run(*unwrap(M));
2538 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2539 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2542 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2543 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2546 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2547 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2550 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2554 /*===-- Threading ------------------------------------------------------===*/
2556 LLVMBool LLVMStartMultithreaded() {
2557 return llvm_start_multithreaded();
2560 void LLVMStopMultithreaded() {
2561 llvm_stop_multithreaded();
2564 LLVMBool LLVMIsMultithreaded() {
2565 return llvm_is_multithreaded();