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 char *LLVMPrintModuleToString(LLVMModuleRef M) {
152 raw_string_ostream os(buf);
154 unwrap(M)->print(os, NULL);
157 return strdup(buf.c_str());
160 /*--.. Operations on inline assembler ......................................--*/
161 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
162 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
166 /*--.. Operations on module contexts ......................................--*/
167 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
168 return wrap(&unwrap(M)->getContext());
172 /*===-- Operations on types -----------------------------------------------===*/
174 /*--.. Operations on all types (mostly) ....................................--*/
176 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
177 switch (unwrap(Ty)->getTypeID()) {
178 default: llvm_unreachable("Unhandled TypeID.");
180 return LLVMVoidTypeKind;
182 return LLVMHalfTypeKind;
183 case Type::FloatTyID:
184 return LLVMFloatTypeKind;
185 case Type::DoubleTyID:
186 return LLVMDoubleTypeKind;
187 case Type::X86_FP80TyID:
188 return LLVMX86_FP80TypeKind;
189 case Type::FP128TyID:
190 return LLVMFP128TypeKind;
191 case Type::PPC_FP128TyID:
192 return LLVMPPC_FP128TypeKind;
193 case Type::LabelTyID:
194 return LLVMLabelTypeKind;
195 case Type::MetadataTyID:
196 return LLVMMetadataTypeKind;
197 case Type::IntegerTyID:
198 return LLVMIntegerTypeKind;
199 case Type::FunctionTyID:
200 return LLVMFunctionTypeKind;
201 case Type::StructTyID:
202 return LLVMStructTypeKind;
203 case Type::ArrayTyID:
204 return LLVMArrayTypeKind;
205 case Type::PointerTyID:
206 return LLVMPointerTypeKind;
207 case Type::VectorTyID:
208 return LLVMVectorTypeKind;
209 case Type::X86_MMXTyID:
210 return LLVMX86_MMXTypeKind;
214 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
216 return unwrap(Ty)->isSized();
219 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
220 return wrap(&unwrap(Ty)->getContext());
223 void LLVMDumpType(LLVMTypeRef Ty) {
224 return unwrap(Ty)->dump();
227 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
229 raw_string_ostream os(buf);
231 unwrap(Ty)->print(os);
234 return strdup(buf.c_str());
237 /*--.. Operations on integer types .........................................--*/
239 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
240 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
242 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
243 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
245 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
246 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
248 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
249 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
251 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
252 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
254 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
255 return wrap(IntegerType::get(*unwrap(C), NumBits));
258 LLVMTypeRef LLVMInt1Type(void) {
259 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
261 LLVMTypeRef LLVMInt8Type(void) {
262 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
264 LLVMTypeRef LLVMInt16Type(void) {
265 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
267 LLVMTypeRef LLVMInt32Type(void) {
268 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
270 LLVMTypeRef LLVMInt64Type(void) {
271 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
273 LLVMTypeRef LLVMIntType(unsigned NumBits) {
274 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
277 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
278 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
281 /*--.. Operations on real types ............................................--*/
283 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
284 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
286 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
287 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
289 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
290 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
292 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
293 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
295 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
296 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
298 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
299 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
301 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
302 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
305 LLVMTypeRef LLVMHalfType(void) {
306 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
308 LLVMTypeRef LLVMFloatType(void) {
309 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
311 LLVMTypeRef LLVMDoubleType(void) {
312 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
314 LLVMTypeRef LLVMX86FP80Type(void) {
315 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
317 LLVMTypeRef LLVMFP128Type(void) {
318 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
320 LLVMTypeRef LLVMPPCFP128Type(void) {
321 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
323 LLVMTypeRef LLVMX86MMXType(void) {
324 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
327 /*--.. Operations on function types ........................................--*/
329 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
330 LLVMTypeRef *ParamTypes, unsigned ParamCount,
332 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
333 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
336 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
337 return unwrap<FunctionType>(FunctionTy)->isVarArg();
340 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
341 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
344 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
345 return unwrap<FunctionType>(FunctionTy)->getNumParams();
348 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
349 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
350 for (FunctionType::param_iterator I = Ty->param_begin(),
351 E = Ty->param_end(); I != E; ++I)
355 /*--.. Operations on struct types ..........................................--*/
357 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
358 unsigned ElementCount, LLVMBool Packed) {
359 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
360 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
363 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
364 unsigned ElementCount, LLVMBool Packed) {
365 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
366 ElementCount, Packed);
369 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
371 return wrap(StructType::create(*unwrap(C), Name));
374 const char *LLVMGetStructName(LLVMTypeRef Ty)
376 StructType *Type = unwrap<StructType>(Ty);
377 if (!Type->hasName())
379 return Type->getName().data();
382 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
383 unsigned ElementCount, LLVMBool Packed) {
384 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
385 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
388 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
389 return unwrap<StructType>(StructTy)->getNumElements();
392 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
393 StructType *Ty = unwrap<StructType>(StructTy);
394 for (StructType::element_iterator I = Ty->element_begin(),
395 E = Ty->element_end(); I != E; ++I)
399 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
400 return unwrap<StructType>(StructTy)->isPacked();
403 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
404 return unwrap<StructType>(StructTy)->isOpaque();
407 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
408 return wrap(unwrap(M)->getTypeByName(Name));
411 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
413 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
414 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
417 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
418 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
421 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
422 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
425 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
426 return wrap(unwrap<SequentialType>(Ty)->getElementType());
429 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
430 return unwrap<ArrayType>(ArrayTy)->getNumElements();
433 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
434 return unwrap<PointerType>(PointerTy)->getAddressSpace();
437 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
438 return unwrap<VectorType>(VectorTy)->getNumElements();
441 /*--.. Operations on other types ...........................................--*/
443 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
444 return wrap(Type::getVoidTy(*unwrap(C)));
446 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
447 return wrap(Type::getLabelTy(*unwrap(C)));
450 LLVMTypeRef LLVMVoidType(void) {
451 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
453 LLVMTypeRef LLVMLabelType(void) {
454 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
457 /*===-- Operations on values ----------------------------------------------===*/
459 /*--.. Operations on all values ............................................--*/
461 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
462 return wrap(unwrap(Val)->getType());
465 const char *LLVMGetValueName(LLVMValueRef Val) {
466 return unwrap(Val)->getName().data();
469 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
470 unwrap(Val)->setName(Name);
473 void LLVMDumpValue(LLVMValueRef Val) {
477 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
478 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
481 int LLVMHasMetadata(LLVMValueRef Inst) {
482 return unwrap<Instruction>(Inst)->hasMetadata();
485 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
486 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
489 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
490 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
493 /*--.. Conversion functions ................................................--*/
495 #define LLVM_DEFINE_VALUE_CAST(name) \
496 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
497 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
500 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
502 /*--.. Operations on Uses ..................................................--*/
503 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
504 Value *V = unwrap(Val);
505 Value::use_iterator I = V->use_begin();
506 if (I == V->use_end())
508 return wrap(&(I.getUse()));
511 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
512 Use *Next = unwrap(U)->getNext();
518 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
519 return wrap(unwrap(U)->getUser());
522 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
523 return wrap(unwrap(U)->get());
526 /*--.. Operations on Users .................................................--*/
527 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
528 Value *V = unwrap(Val);
529 if (MDNode *MD = dyn_cast<MDNode>(V))
530 return wrap(MD->getOperand(Index));
531 return wrap(cast<User>(V)->getOperand(Index));
534 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
535 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
538 int LLVMGetNumOperands(LLVMValueRef Val) {
539 Value *V = unwrap(Val);
540 if (MDNode *MD = dyn_cast<MDNode>(V))
541 return MD->getNumOperands();
542 return cast<User>(V)->getNumOperands();
545 /*--.. Operations on constants of any type .................................--*/
547 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
548 return wrap(Constant::getNullValue(unwrap(Ty)));
551 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
552 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
555 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
556 return wrap(UndefValue::get(unwrap(Ty)));
559 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
560 return isa<Constant>(unwrap(Ty));
563 LLVMBool LLVMIsNull(LLVMValueRef Val) {
564 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
565 return C->isNullValue();
569 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
570 return isa<UndefValue>(unwrap(Val));
573 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
575 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
578 /*--.. Operations on metadata nodes ........................................--*/
580 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
582 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
585 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
586 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
589 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
591 return wrap(MDNode::get(*unwrap(C),
592 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
595 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
596 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
599 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
600 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
601 *Len = S->getString().size();
602 return S->getString().data();
608 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
610 return cast<MDNode>(unwrap(V))->getNumOperands();
613 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
615 const MDNode *N = cast<MDNode>(unwrap(V));
616 const unsigned numOperands = N->getNumOperands();
617 for (unsigned i = 0; i < numOperands; i++)
618 Dest[i] = wrap(N->getOperand(i));
621 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
623 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
624 return N->getNumOperands();
629 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
631 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
634 for (unsigned i=0;i<N->getNumOperands();i++)
635 Dest[i] = wrap(N->getOperand(i));
638 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
641 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
644 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
649 /*--.. Operations on scalar constants ......................................--*/
651 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
652 LLVMBool SignExtend) {
653 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
656 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
658 const uint64_t Words[]) {
659 IntegerType *Ty = unwrap<IntegerType>(IntTy);
660 return wrap(ConstantInt::get(Ty->getContext(),
661 APInt(Ty->getBitWidth(),
662 makeArrayRef(Words, NumWords))));
665 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
667 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
671 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
672 unsigned SLen, uint8_t Radix) {
673 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
677 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
678 return wrap(ConstantFP::get(unwrap(RealTy), N));
681 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
682 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
685 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
687 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
690 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
691 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
694 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
695 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
698 /*--.. Operations on composite constants ...................................--*/
700 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
702 LLVMBool DontNullTerminate) {
703 /* Inverted the sense of AddNull because ', 0)' is a
704 better mnemonic for null termination than ', 1)'. */
705 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
706 DontNullTerminate == 0));
708 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
709 LLVMValueRef *ConstantVals,
710 unsigned Count, LLVMBool Packed) {
711 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
712 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
716 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
717 LLVMBool DontNullTerminate) {
718 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
721 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
722 LLVMValueRef *ConstantVals, unsigned Length) {
723 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
724 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
726 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
728 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
732 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
733 LLVMValueRef *ConstantVals,
735 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
736 StructType *Ty = cast<StructType>(unwrap(StructTy));
738 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
741 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
742 return wrap(ConstantVector::get(makeArrayRef(
743 unwrap<Constant>(ScalarConstantVals, Size), Size)));
746 /*-- Opcode mapping */
748 static LLVMOpcode map_to_llvmopcode(int opcode)
751 default: llvm_unreachable("Unhandled Opcode.");
752 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
753 #include "llvm/IR/Instruction.def"
758 static int map_from_llvmopcode(LLVMOpcode code)
761 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
762 #include "llvm/IR/Instruction.def"
765 llvm_unreachable("Unhandled Opcode.");
768 /*--.. Constant expressions ................................................--*/
770 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
771 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
774 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
775 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
778 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
779 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
782 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
783 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
786 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
787 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
790 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
791 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
795 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
796 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
799 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
800 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
803 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
804 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
805 unwrap<Constant>(RHSConstant)));
808 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
809 LLVMValueRef RHSConstant) {
810 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
811 unwrap<Constant>(RHSConstant)));
814 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
815 LLVMValueRef RHSConstant) {
816 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
817 unwrap<Constant>(RHSConstant)));
820 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
821 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
822 unwrap<Constant>(RHSConstant)));
825 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
826 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
827 unwrap<Constant>(RHSConstant)));
830 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
831 LLVMValueRef RHSConstant) {
832 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
833 unwrap<Constant>(RHSConstant)));
836 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
837 LLVMValueRef RHSConstant) {
838 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
839 unwrap<Constant>(RHSConstant)));
842 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
843 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
844 unwrap<Constant>(RHSConstant)));
847 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
848 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
849 unwrap<Constant>(RHSConstant)));
852 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
853 LLVMValueRef RHSConstant) {
854 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
855 unwrap<Constant>(RHSConstant)));
858 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
859 LLVMValueRef RHSConstant) {
860 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
861 unwrap<Constant>(RHSConstant)));
864 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
865 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
866 unwrap<Constant>(RHSConstant)));
869 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
870 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
871 unwrap<Constant>(RHSConstant)));
874 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
875 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
876 unwrap<Constant>(RHSConstant)));
879 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
880 LLVMValueRef RHSConstant) {
881 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
882 unwrap<Constant>(RHSConstant)));
885 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
886 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
887 unwrap<Constant>(RHSConstant)));
890 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
891 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
892 unwrap<Constant>(RHSConstant)));
895 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
896 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
897 unwrap<Constant>(RHSConstant)));
900 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
901 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
902 unwrap<Constant>(RHSConstant)));
905 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
906 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
907 unwrap<Constant>(RHSConstant)));
910 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
911 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
912 unwrap<Constant>(RHSConstant)));
915 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
916 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
921 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
922 return wrap(ConstantExpr::getICmp(Predicate,
923 unwrap<Constant>(LHSConstant),
924 unwrap<Constant>(RHSConstant)));
927 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
928 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
929 return wrap(ConstantExpr::getFCmp(Predicate,
930 unwrap<Constant>(LHSConstant),
931 unwrap<Constant>(RHSConstant)));
934 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
935 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
936 unwrap<Constant>(RHSConstant)));
939 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
940 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
941 unwrap<Constant>(RHSConstant)));
944 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
945 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
946 unwrap<Constant>(RHSConstant)));
949 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
950 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
951 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
953 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
957 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
958 LLVMValueRef *ConstantIndices,
959 unsigned NumIndices) {
960 Constant* Val = unwrap<Constant>(ConstantVal);
961 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
963 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
966 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
967 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
971 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
972 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
976 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
977 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
981 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
982 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
986 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
987 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
991 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
992 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
996 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
997 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1001 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1002 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1006 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1007 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1011 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1012 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1016 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1017 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1021 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1022 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1026 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1027 LLVMTypeRef ToType) {
1028 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1032 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1033 LLVMTypeRef ToType) {
1034 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1038 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1039 LLVMTypeRef ToType) {
1040 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1044 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1045 LLVMTypeRef ToType) {
1046 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1050 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1051 LLVMBool isSigned) {
1052 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1053 unwrap(ToType), isSigned));
1056 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1057 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1061 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1062 LLVMValueRef ConstantIfTrue,
1063 LLVMValueRef ConstantIfFalse) {
1064 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1065 unwrap<Constant>(ConstantIfTrue),
1066 unwrap<Constant>(ConstantIfFalse)));
1069 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1070 LLVMValueRef IndexConstant) {
1071 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1072 unwrap<Constant>(IndexConstant)));
1075 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1076 LLVMValueRef ElementValueConstant,
1077 LLVMValueRef IndexConstant) {
1078 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1079 unwrap<Constant>(ElementValueConstant),
1080 unwrap<Constant>(IndexConstant)));
1083 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1084 LLVMValueRef VectorBConstant,
1085 LLVMValueRef MaskConstant) {
1086 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1087 unwrap<Constant>(VectorBConstant),
1088 unwrap<Constant>(MaskConstant)));
1091 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1093 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1094 makeArrayRef(IdxList, NumIdx)));
1097 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1098 LLVMValueRef ElementValueConstant,
1099 unsigned *IdxList, unsigned NumIdx) {
1100 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1101 unwrap<Constant>(ElementValueConstant),
1102 makeArrayRef(IdxList, NumIdx)));
1105 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1106 const char *Constraints,
1107 LLVMBool HasSideEffects,
1108 LLVMBool IsAlignStack) {
1109 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1110 Constraints, HasSideEffects, IsAlignStack));
1113 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1114 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1117 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1119 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1120 return wrap(unwrap<GlobalValue>(Global)->getParent());
1123 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1124 return unwrap<GlobalValue>(Global)->isDeclaration();
1127 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1128 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1129 case GlobalValue::ExternalLinkage:
1130 return LLVMExternalLinkage;
1131 case GlobalValue::AvailableExternallyLinkage:
1132 return LLVMAvailableExternallyLinkage;
1133 case GlobalValue::LinkOnceAnyLinkage:
1134 return LLVMLinkOnceAnyLinkage;
1135 case GlobalValue::LinkOnceODRLinkage:
1136 return LLVMLinkOnceODRLinkage;
1137 case GlobalValue::WeakAnyLinkage:
1138 return LLVMWeakAnyLinkage;
1139 case GlobalValue::WeakODRLinkage:
1140 return LLVMWeakODRLinkage;
1141 case GlobalValue::AppendingLinkage:
1142 return LLVMAppendingLinkage;
1143 case GlobalValue::InternalLinkage:
1144 return LLVMInternalLinkage;
1145 case GlobalValue::PrivateLinkage:
1146 return LLVMPrivateLinkage;
1147 case GlobalValue::LinkerPrivateLinkage:
1148 return LLVMLinkerPrivateLinkage;
1149 case GlobalValue::LinkerPrivateWeakLinkage:
1150 return LLVMLinkerPrivateWeakLinkage;
1151 case GlobalValue::DLLImportLinkage:
1152 return LLVMDLLImportLinkage;
1153 case GlobalValue::DLLExportLinkage:
1154 return LLVMDLLExportLinkage;
1155 case GlobalValue::ExternalWeakLinkage:
1156 return LLVMExternalWeakLinkage;
1157 case GlobalValue::CommonLinkage:
1158 return LLVMCommonLinkage;
1161 llvm_unreachable("Invalid GlobalValue linkage!");
1164 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1165 GlobalValue *GV = unwrap<GlobalValue>(Global);
1168 case LLVMExternalLinkage:
1169 GV->setLinkage(GlobalValue::ExternalLinkage);
1171 case LLVMAvailableExternallyLinkage:
1172 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1174 case LLVMLinkOnceAnyLinkage:
1175 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1177 case LLVMLinkOnceODRLinkage:
1178 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1180 case LLVMLinkOnceODRAutoHideLinkage:
1181 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1182 "longer supported.");
1184 case LLVMWeakAnyLinkage:
1185 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1187 case LLVMWeakODRLinkage:
1188 GV->setLinkage(GlobalValue::WeakODRLinkage);
1190 case LLVMAppendingLinkage:
1191 GV->setLinkage(GlobalValue::AppendingLinkage);
1193 case LLVMInternalLinkage:
1194 GV->setLinkage(GlobalValue::InternalLinkage);
1196 case LLVMPrivateLinkage:
1197 GV->setLinkage(GlobalValue::PrivateLinkage);
1199 case LLVMLinkerPrivateLinkage:
1200 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1202 case LLVMLinkerPrivateWeakLinkage:
1203 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1205 case LLVMDLLImportLinkage:
1206 GV->setLinkage(GlobalValue::DLLImportLinkage);
1208 case LLVMDLLExportLinkage:
1209 GV->setLinkage(GlobalValue::DLLExportLinkage);
1211 case LLVMExternalWeakLinkage:
1212 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1214 case LLVMGhostLinkage:
1216 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1218 case LLVMCommonLinkage:
1219 GV->setLinkage(GlobalValue::CommonLinkage);
1224 const char *LLVMGetSection(LLVMValueRef Global) {
1225 return unwrap<GlobalValue>(Global)->getSection().c_str();
1228 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1229 unwrap<GlobalValue>(Global)->setSection(Section);
1232 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1233 return static_cast<LLVMVisibility>(
1234 unwrap<GlobalValue>(Global)->getVisibility());
1237 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1238 unwrap<GlobalValue>(Global)
1239 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1242 /*--.. Operations on global variables, load and store instructions .........--*/
1244 unsigned LLVMGetAlignment(LLVMValueRef V) {
1245 Value *P = unwrap<Value>(V);
1246 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1247 return GV->getAlignment();
1248 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1249 return LI->getAlignment();
1250 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1251 return SI->getAlignment();
1253 llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1256 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1257 Value *P = unwrap<Value>(V);
1258 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1259 GV->setAlignment(Bytes);
1260 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1261 LI->setAlignment(Bytes);
1262 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1263 SI->setAlignment(Bytes);
1265 llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1268 /*--.. Operations on global variables ......................................--*/
1270 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1271 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1272 GlobalValue::ExternalLinkage, 0, Name));
1275 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1277 unsigned AddressSpace) {
1278 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1279 GlobalValue::ExternalLinkage, 0, Name, 0,
1280 GlobalVariable::NotThreadLocal, AddressSpace));
1283 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1284 return wrap(unwrap(M)->getNamedGlobal(Name));
1287 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1288 Module *Mod = unwrap(M);
1289 Module::global_iterator I = Mod->global_begin();
1290 if (I == Mod->global_end())
1295 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1296 Module *Mod = unwrap(M);
1297 Module::global_iterator I = Mod->global_end();
1298 if (I == Mod->global_begin())
1303 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1304 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1305 Module::global_iterator I = GV;
1306 if (++I == GV->getParent()->global_end())
1311 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1312 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1313 Module::global_iterator I = GV;
1314 if (I == GV->getParent()->global_begin())
1319 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1320 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1323 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1324 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1325 if ( !GV->hasInitializer() )
1327 return wrap(GV->getInitializer());
1330 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1331 unwrap<GlobalVariable>(GlobalVar)
1332 ->setInitializer(unwrap<Constant>(ConstantVal));
1335 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1336 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1339 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1340 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1343 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1344 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1347 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1348 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1351 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1352 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1353 case GlobalVariable::NotThreadLocal:
1354 return LLVMNotThreadLocal;
1355 case GlobalVariable::GeneralDynamicTLSModel:
1356 return LLVMGeneralDynamicTLSModel;
1357 case GlobalVariable::LocalDynamicTLSModel:
1358 return LLVMLocalDynamicTLSModel;
1359 case GlobalVariable::InitialExecTLSModel:
1360 return LLVMInitialExecTLSModel;
1361 case GlobalVariable::LocalExecTLSModel:
1362 return LLVMLocalExecTLSModel;
1365 llvm_unreachable("Invalid GlobalVariable thread local mode");
1368 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1369 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1372 case LLVMNotThreadLocal:
1373 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1375 case LLVMGeneralDynamicTLSModel:
1376 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1378 case LLVMLocalDynamicTLSModel:
1379 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1381 case LLVMInitialExecTLSModel:
1382 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1384 case LLVMLocalExecTLSModel:
1385 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1390 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1391 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1394 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1395 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1398 /*--.. Operations on aliases ......................................--*/
1400 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1402 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1403 unwrap<Constant>(Aliasee), unwrap (M)));
1406 /*--.. Operations on functions .............................................--*/
1408 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1409 LLVMTypeRef FunctionTy) {
1410 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1411 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1414 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1415 return wrap(unwrap(M)->getFunction(Name));
1418 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1419 Module *Mod = unwrap(M);
1420 Module::iterator I = Mod->begin();
1421 if (I == Mod->end())
1426 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1427 Module *Mod = unwrap(M);
1428 Module::iterator I = Mod->end();
1429 if (I == Mod->begin())
1434 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1435 Function *Func = unwrap<Function>(Fn);
1436 Module::iterator I = Func;
1437 if (++I == Func->getParent()->end())
1442 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1443 Function *Func = unwrap<Function>(Fn);
1444 Module::iterator I = Func;
1445 if (I == Func->getParent()->begin())
1450 void LLVMDeleteFunction(LLVMValueRef Fn) {
1451 unwrap<Function>(Fn)->eraseFromParent();
1454 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1455 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1456 return F->getIntrinsicID();
1460 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1461 return unwrap<Function>(Fn)->getCallingConv();
1464 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1465 return unwrap<Function>(Fn)->setCallingConv(
1466 static_cast<CallingConv::ID>(CC));
1469 const char *LLVMGetGC(LLVMValueRef Fn) {
1470 Function *F = unwrap<Function>(Fn);
1471 return F->hasGC()? F->getGC() : 0;
1474 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1475 Function *F = unwrap<Function>(Fn);
1482 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1483 Function *Func = unwrap<Function>(Fn);
1484 const AttributeSet PAL = Func->getAttributes();
1486 const AttributeSet PALnew =
1487 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1488 AttributeSet::get(Func->getContext(),
1489 AttributeSet::FunctionIndex, B));
1490 Func->setAttributes(PALnew);
1493 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1495 Function *Func = unwrap<Function>(Fn);
1496 AttributeSet::AttrIndex Idx =
1497 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1500 B.addAttribute(A, V);
1501 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1502 Func->addAttributes(Idx, Set);
1505 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1506 Function *Func = unwrap<Function>(Fn);
1507 const AttributeSet PAL = Func->getAttributes();
1509 const AttributeSet PALnew =
1510 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1511 AttributeSet::get(Func->getContext(),
1512 AttributeSet::FunctionIndex, B));
1513 Func->setAttributes(PALnew);
1516 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1517 Function *Func = unwrap<Function>(Fn);
1518 const AttributeSet PAL = Func->getAttributes();
1519 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1522 /*--.. Operations on parameters ............................................--*/
1524 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1525 // This function is strictly redundant to
1526 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1527 return unwrap<Function>(FnRef)->arg_size();
1530 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1531 Function *Fn = unwrap<Function>(FnRef);
1532 for (Function::arg_iterator I = Fn->arg_begin(),
1533 E = Fn->arg_end(); I != E; I++)
1534 *ParamRefs++ = wrap(I);
1537 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1538 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1544 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1545 return wrap(unwrap<Argument>(V)->getParent());
1548 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1549 Function *Func = unwrap<Function>(Fn);
1550 Function::arg_iterator I = Func->arg_begin();
1551 if (I == Func->arg_end())
1556 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1557 Function *Func = unwrap<Function>(Fn);
1558 Function::arg_iterator I = Func->arg_end();
1559 if (I == Func->arg_begin())
1564 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1565 Argument *A = unwrap<Argument>(Arg);
1566 Function::arg_iterator I = A;
1567 if (++I == A->getParent()->arg_end())
1572 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1573 Argument *A = unwrap<Argument>(Arg);
1574 Function::arg_iterator I = A;
1575 if (I == A->getParent()->arg_begin())
1580 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1581 Argument *A = unwrap<Argument>(Arg);
1583 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1586 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1587 Argument *A = unwrap<Argument>(Arg);
1589 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1592 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1593 Argument *A = unwrap<Argument>(Arg);
1594 return (LLVMAttribute)A->getParent()->getAttributes().
1595 Raw(A->getArgNo()+1);
1599 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1600 Argument *A = unwrap<Argument>(Arg);
1602 B.addAlignmentAttr(align);
1603 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1606 /*--.. Operations on basic blocks ..........................................--*/
1608 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1609 return wrap(static_cast<Value*>(unwrap(BB)));
1612 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1613 return isa<BasicBlock>(unwrap(Val));
1616 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1617 return wrap(unwrap<BasicBlock>(Val));
1620 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1621 return wrap(unwrap(BB)->getParent());
1624 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1625 return wrap(unwrap(BB)->getTerminator());
1628 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1629 return unwrap<Function>(FnRef)->size();
1632 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1633 Function *Fn = unwrap<Function>(FnRef);
1634 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1635 *BasicBlocksRefs++ = wrap(I);
1638 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1639 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1642 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1643 Function *Func = unwrap<Function>(Fn);
1644 Function::iterator I = Func->begin();
1645 if (I == Func->end())
1650 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1651 Function *Func = unwrap<Function>(Fn);
1652 Function::iterator I = Func->end();
1653 if (I == Func->begin())
1658 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1659 BasicBlock *Block = unwrap(BB);
1660 Function::iterator I = Block;
1661 if (++I == Block->getParent()->end())
1666 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1667 BasicBlock *Block = unwrap(BB);
1668 Function::iterator I = Block;
1669 if (I == Block->getParent()->begin())
1674 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1677 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1680 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1681 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1684 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1685 LLVMBasicBlockRef BBRef,
1687 BasicBlock *BB = unwrap(BBRef);
1688 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1691 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1693 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1696 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1697 unwrap(BBRef)->eraseFromParent();
1700 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1701 unwrap(BBRef)->removeFromParent();
1704 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1705 unwrap(BB)->moveBefore(unwrap(MovePos));
1708 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1709 unwrap(BB)->moveAfter(unwrap(MovePos));
1712 /*--.. Operations on instructions ..........................................--*/
1714 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1715 return wrap(unwrap<Instruction>(Inst)->getParent());
1718 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1719 BasicBlock *Block = unwrap(BB);
1720 BasicBlock::iterator I = Block->begin();
1721 if (I == Block->end())
1726 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1727 BasicBlock *Block = unwrap(BB);
1728 BasicBlock::iterator I = Block->end();
1729 if (I == Block->begin())
1734 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1735 Instruction *Instr = unwrap<Instruction>(Inst);
1736 BasicBlock::iterator I = Instr;
1737 if (++I == Instr->getParent()->end())
1742 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1743 Instruction *Instr = unwrap<Instruction>(Inst);
1744 BasicBlock::iterator I = Instr;
1745 if (I == Instr->getParent()->begin())
1750 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1751 unwrap<Instruction>(Inst)->eraseFromParent();
1754 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1755 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1756 return (LLVMIntPredicate)I->getPredicate();
1757 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1758 if (CE->getOpcode() == Instruction::ICmp)
1759 return (LLVMIntPredicate)CE->getPredicate();
1760 return (LLVMIntPredicate)0;
1763 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1764 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1765 return map_to_llvmopcode(C->getOpcode());
1766 return (LLVMOpcode)0;
1769 /*--.. Call and invoke instructions ........................................--*/
1771 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1772 Value *V = unwrap(Instr);
1773 if (CallInst *CI = dyn_cast<CallInst>(V))
1774 return CI->getCallingConv();
1775 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1776 return II->getCallingConv();
1777 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1780 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1781 Value *V = unwrap(Instr);
1782 if (CallInst *CI = dyn_cast<CallInst>(V))
1783 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1784 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1785 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1786 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1789 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1791 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1794 Call.getAttributes().addAttributes(Call->getContext(), index,
1795 AttributeSet::get(Call->getContext(),
1799 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1801 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1803 Call.setAttributes(Call.getAttributes()
1804 .removeAttributes(Call->getContext(), index,
1805 AttributeSet::get(Call->getContext(),
1809 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1811 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1813 B.addAlignmentAttr(align);
1814 Call.setAttributes(Call.getAttributes()
1815 .addAttributes(Call->getContext(), index,
1816 AttributeSet::get(Call->getContext(),
1820 /*--.. Operations on call instructions (only) ..............................--*/
1822 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1823 return unwrap<CallInst>(Call)->isTailCall();
1826 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1827 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1830 /*--.. Operations on switch instructions (only) ............................--*/
1832 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1833 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1836 /*--.. Operations on phi nodes .............................................--*/
1838 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1839 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1840 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1841 for (unsigned I = 0; I != Count; ++I)
1842 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1845 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1846 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1849 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1850 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1853 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1854 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1858 /*===-- Instruction builders ----------------------------------------------===*/
1860 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1861 return wrap(new IRBuilder<>(*unwrap(C)));
1864 LLVMBuilderRef LLVMCreateBuilder(void) {
1865 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1868 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1869 LLVMValueRef Instr) {
1870 BasicBlock *BB = unwrap(Block);
1871 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1872 unwrap(Builder)->SetInsertPoint(BB, I);
1875 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1876 Instruction *I = unwrap<Instruction>(Instr);
1877 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1880 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1881 BasicBlock *BB = unwrap(Block);
1882 unwrap(Builder)->SetInsertPoint(BB);
1885 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1886 return wrap(unwrap(Builder)->GetInsertBlock());
1889 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1890 unwrap(Builder)->ClearInsertionPoint();
1893 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1894 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1897 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1899 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1902 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1903 delete unwrap(Builder);
1906 /*--.. Metadata builders ...................................................--*/
1908 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1909 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1910 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1913 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1914 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1915 .getAsMDNode(unwrap(Builder)->getContext()));
1918 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1919 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1923 /*--.. Instruction builders ................................................--*/
1925 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1926 return wrap(unwrap(B)->CreateRetVoid());
1929 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1930 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1933 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1935 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1938 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1939 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1942 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1943 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1944 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1947 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1948 LLVMBasicBlockRef Else, unsigned NumCases) {
1949 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1952 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1953 unsigned NumDests) {
1954 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1957 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1958 LLVMValueRef *Args, unsigned NumArgs,
1959 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1961 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1962 makeArrayRef(unwrap(Args), NumArgs),
1966 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1967 LLVMValueRef PersFn, unsigned NumClauses,
1969 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1970 cast<Function>(unwrap(PersFn)),
1974 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1975 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1978 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1979 return wrap(unwrap(B)->CreateUnreachable());
1982 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1983 LLVMBasicBlockRef Dest) {
1984 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1987 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1988 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1991 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1992 unwrap<LandingPadInst>(LandingPad)->
1993 addClause(cast<Constant>(unwrap(ClauseVal)));
1996 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1997 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2000 /*--.. Arithmetic ..........................................................--*/
2002 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2004 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2007 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2009 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2012 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2014 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2017 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2019 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2022 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2024 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2027 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2029 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2032 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2034 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2037 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2039 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2042 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2044 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2047 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2049 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2052 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2054 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2057 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2059 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2062 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2064 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2067 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2069 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2072 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2073 LLVMValueRef RHS, const char *Name) {
2074 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2077 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2079 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2082 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2084 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2087 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2089 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2092 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2094 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2097 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2099 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2102 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2104 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2107 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2109 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2112 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2114 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2117 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2119 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2122 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2124 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2127 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2128 LLVMValueRef LHS, LLVMValueRef RHS,
2130 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2131 unwrap(RHS), Name));
2134 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2135 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2138 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2140 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2143 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2145 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2148 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2149 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2152 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2153 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2156 /*--.. Memory ..............................................................--*/
2158 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2160 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2161 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2162 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2163 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2164 ITy, unwrap(Ty), AllocSize,
2166 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2169 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2170 LLVMValueRef Val, const char *Name) {
2171 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2172 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2173 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2174 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2175 ITy, unwrap(Ty), AllocSize,
2176 unwrap(Val), 0, "");
2177 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2180 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2182 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2185 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2186 LLVMValueRef Val, const char *Name) {
2187 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2190 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2191 return wrap(unwrap(B)->Insert(
2192 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2196 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2198 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2201 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2202 LLVMValueRef PointerVal) {
2203 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2206 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2207 LLVMValueRef *Indices, unsigned NumIndices,
2209 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2210 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2213 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2214 LLVMValueRef *Indices, unsigned NumIndices,
2216 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2217 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2220 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2221 unsigned Idx, const char *Name) {
2222 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2225 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2227 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2230 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2232 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2235 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2236 Value *P = unwrap<Value>(MemAccessInst);
2237 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2238 return LI->isVolatile();
2239 return cast<StoreInst>(P)->isVolatile();
2242 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2243 Value *P = unwrap<Value>(MemAccessInst);
2244 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2245 return LI->setVolatile(isVolatile);
2246 return cast<StoreInst>(P)->setVolatile(isVolatile);
2249 /*--.. Casts ...............................................................--*/
2251 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2252 LLVMTypeRef DestTy, const char *Name) {
2253 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2256 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2257 LLVMTypeRef DestTy, const char *Name) {
2258 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2261 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2262 LLVMTypeRef DestTy, const char *Name) {
2263 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2266 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2267 LLVMTypeRef DestTy, const char *Name) {
2268 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2271 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2272 LLVMTypeRef DestTy, const char *Name) {
2273 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2276 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2277 LLVMTypeRef DestTy, const char *Name) {
2278 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2281 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2282 LLVMTypeRef DestTy, const char *Name) {
2283 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2286 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2287 LLVMTypeRef DestTy, const char *Name) {
2288 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2291 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2292 LLVMTypeRef DestTy, const char *Name) {
2293 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2296 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2297 LLVMTypeRef DestTy, const char *Name) {
2298 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2301 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2302 LLVMTypeRef DestTy, const char *Name) {
2303 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2306 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2307 LLVMTypeRef DestTy, const char *Name) {
2308 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2311 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2312 LLVMTypeRef DestTy, const char *Name) {
2313 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2317 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2318 LLVMTypeRef DestTy, const char *Name) {
2319 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2323 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2324 LLVMTypeRef DestTy, const char *Name) {
2325 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2329 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2330 LLVMTypeRef DestTy, const char *Name) {
2331 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2332 unwrap(DestTy), Name));
2335 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2336 LLVMTypeRef DestTy, const char *Name) {
2337 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2340 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2341 LLVMTypeRef DestTy, const char *Name) {
2342 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2343 /*isSigned*/true, Name));
2346 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2347 LLVMTypeRef DestTy, const char *Name) {
2348 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2351 /*--.. Comparisons .........................................................--*/
2353 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2354 LLVMValueRef LHS, LLVMValueRef RHS,
2356 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2357 unwrap(LHS), unwrap(RHS), Name));
2360 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2361 LLVMValueRef LHS, LLVMValueRef RHS,
2363 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2364 unwrap(LHS), unwrap(RHS), Name));
2367 /*--.. Miscellaneous instructions ..........................................--*/
2369 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2370 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2373 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2374 LLVMValueRef *Args, unsigned NumArgs,
2376 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2377 makeArrayRef(unwrap(Args), NumArgs),
2381 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2382 LLVMValueRef Then, LLVMValueRef Else,
2384 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2388 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2389 LLVMTypeRef Ty, const char *Name) {
2390 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2393 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2394 LLVMValueRef Index, const char *Name) {
2395 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2399 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2400 LLVMValueRef EltVal, LLVMValueRef Index,
2402 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2403 unwrap(Index), Name));
2406 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2407 LLVMValueRef V2, LLVMValueRef Mask,
2409 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2410 unwrap(Mask), Name));
2413 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2414 unsigned Index, const char *Name) {
2415 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2418 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2419 LLVMValueRef EltVal, unsigned Index,
2421 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2425 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2427 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2430 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2432 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2435 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2436 LLVMValueRef RHS, const char *Name) {
2437 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2440 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2441 LLVMValueRef PTR, LLVMValueRef Val,
2442 LLVMAtomicOrdering ordering,
2443 LLVMBool singleThread) {
2444 AtomicRMWInst::BinOp intop;
2446 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2447 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2448 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2449 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2450 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2451 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2452 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2453 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2454 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2455 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2456 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2458 AtomicOrdering intordering;
2460 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2461 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2462 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2463 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2464 case LLVMAtomicOrderingRelease: intordering = Release; break;
2465 case LLVMAtomicOrderingAcquireRelease:
2466 intordering = AcquireRelease;
2468 case LLVMAtomicOrderingSequentiallyConsistent:
2469 intordering = SequentiallyConsistent;
2472 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2473 intordering, singleThread ? SingleThread : CrossThread));
2477 /*===-- Module providers --------------------------------------------------===*/
2479 LLVMModuleProviderRef
2480 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2481 return reinterpret_cast<LLVMModuleProviderRef>(M);
2484 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2489 /*===-- Memory buffers ----------------------------------------------------===*/
2491 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2493 LLVMMemoryBufferRef *OutMemBuf,
2494 char **OutMessage) {
2496 OwningPtr<MemoryBuffer> MB;
2498 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2499 *OutMemBuf = wrap(MB.take());
2503 *OutMessage = strdup(ec.message().c_str());
2507 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2508 char **OutMessage) {
2509 OwningPtr<MemoryBuffer> MB;
2511 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2512 *OutMemBuf = wrap(MB.take());
2516 *OutMessage = strdup(ec.message().c_str());
2520 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2521 const char *InputData,
2522 size_t InputDataLength,
2523 const char *BufferName,
2524 LLVMBool RequiresNullTerminator) {
2526 return wrap(MemoryBuffer::getMemBuffer(
2527 StringRef(InputData, InputDataLength),
2528 StringRef(BufferName),
2529 RequiresNullTerminator));
2532 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2533 const char *InputData,
2534 size_t InputDataLength,
2535 const char *BufferName) {
2537 return wrap(MemoryBuffer::getMemBufferCopy(
2538 StringRef(InputData, InputDataLength),
2539 StringRef(BufferName)));
2542 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2543 return unwrap(MemBuf)->getBufferStart();
2546 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2547 return unwrap(MemBuf)->getBufferSize();
2550 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2551 delete unwrap(MemBuf);
2554 /*===-- Pass Registry -----------------------------------------------------===*/
2556 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2557 return wrap(PassRegistry::getPassRegistry());
2560 /*===-- Pass Manager ------------------------------------------------------===*/
2562 LLVMPassManagerRef LLVMCreatePassManager() {
2563 return wrap(new PassManager());
2566 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2567 return wrap(new FunctionPassManager(unwrap(M)));
2570 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2571 return LLVMCreateFunctionPassManagerForModule(
2572 reinterpret_cast<LLVMModuleRef>(P));
2575 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2576 return unwrap<PassManager>(PM)->run(*unwrap(M));
2579 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2580 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2583 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2584 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2587 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2588 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2591 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2595 /*===-- Threading ------------------------------------------------------===*/
2597 LLVMBool LLVMStartMultithreaded() {
2598 return llvm_start_multithreaded();
2601 void LLVMStopMultithreaded() {
2602 llvm_stop_multithreaded();
2605 LLVMBool LLVMIsMultithreaded() {
2606 return llvm_is_multithreaded();