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 char* LLVMPrintValueToString(LLVMValueRef Val) {
479 raw_string_ostream os(buf);
481 unwrap(Val)->print(os);
484 return strdup(buf.c_str());
487 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
488 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
491 int LLVMHasMetadata(LLVMValueRef Inst) {
492 return unwrap<Instruction>(Inst)->hasMetadata();
495 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
496 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
499 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
500 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
503 /*--.. Conversion functions ................................................--*/
505 #define LLVM_DEFINE_VALUE_CAST(name) \
506 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
507 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
510 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
512 /*--.. Operations on Uses ..................................................--*/
513 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
514 Value *V = unwrap(Val);
515 Value::use_iterator I = V->use_begin();
516 if (I == V->use_end())
518 return wrap(&(I.getUse()));
521 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
522 Use *Next = unwrap(U)->getNext();
528 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
529 return wrap(unwrap(U)->getUser());
532 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
533 return wrap(unwrap(U)->get());
536 /*--.. Operations on Users .................................................--*/
537 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
538 Value *V = unwrap(Val);
539 if (MDNode *MD = dyn_cast<MDNode>(V))
540 return wrap(MD->getOperand(Index));
541 return wrap(cast<User>(V)->getOperand(Index));
544 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
545 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
548 int LLVMGetNumOperands(LLVMValueRef Val) {
549 Value *V = unwrap(Val);
550 if (MDNode *MD = dyn_cast<MDNode>(V))
551 return MD->getNumOperands();
552 return cast<User>(V)->getNumOperands();
555 /*--.. Operations on constants of any type .................................--*/
557 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
558 return wrap(Constant::getNullValue(unwrap(Ty)));
561 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
562 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
565 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
566 return wrap(UndefValue::get(unwrap(Ty)));
569 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
570 return isa<Constant>(unwrap(Ty));
573 LLVMBool LLVMIsNull(LLVMValueRef Val) {
574 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
575 return C->isNullValue();
579 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
580 return isa<UndefValue>(unwrap(Val));
583 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
585 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
588 /*--.. Operations on metadata nodes ........................................--*/
590 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
592 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
595 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
596 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
599 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
601 return wrap(MDNode::get(*unwrap(C),
602 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
605 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
606 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
609 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
610 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
611 *Len = S->getString().size();
612 return S->getString().data();
618 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
620 return cast<MDNode>(unwrap(V))->getNumOperands();
623 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
625 const MDNode *N = cast<MDNode>(unwrap(V));
626 const unsigned numOperands = N->getNumOperands();
627 for (unsigned i = 0; i < numOperands; i++)
628 Dest[i] = wrap(N->getOperand(i));
631 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
633 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
634 return N->getNumOperands();
639 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
641 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
644 for (unsigned i=0;i<N->getNumOperands();i++)
645 Dest[i] = wrap(N->getOperand(i));
648 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
651 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
654 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
659 /*--.. Operations on scalar constants ......................................--*/
661 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
662 LLVMBool SignExtend) {
663 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
666 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
668 const uint64_t Words[]) {
669 IntegerType *Ty = unwrap<IntegerType>(IntTy);
670 return wrap(ConstantInt::get(Ty->getContext(),
671 APInt(Ty->getBitWidth(),
672 makeArrayRef(Words, NumWords))));
675 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
677 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
681 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
682 unsigned SLen, uint8_t Radix) {
683 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
687 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
688 return wrap(ConstantFP::get(unwrap(RealTy), N));
691 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
692 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
695 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
697 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
700 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
701 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
704 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
705 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
708 /*--.. Operations on composite constants ...................................--*/
710 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
712 LLVMBool DontNullTerminate) {
713 /* Inverted the sense of AddNull because ', 0)' is a
714 better mnemonic for null termination than ', 1)'. */
715 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
716 DontNullTerminate == 0));
718 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
719 LLVMValueRef *ConstantVals,
720 unsigned Count, LLVMBool Packed) {
721 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
722 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
726 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
727 LLVMBool DontNullTerminate) {
728 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
731 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
732 LLVMValueRef *ConstantVals, unsigned Length) {
733 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
734 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
736 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
738 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
742 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
743 LLVMValueRef *ConstantVals,
745 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
746 StructType *Ty = cast<StructType>(unwrap(StructTy));
748 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
751 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
752 return wrap(ConstantVector::get(makeArrayRef(
753 unwrap<Constant>(ScalarConstantVals, Size), Size)));
756 /*-- Opcode mapping */
758 static LLVMOpcode map_to_llvmopcode(int opcode)
761 default: llvm_unreachable("Unhandled Opcode.");
762 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
763 #include "llvm/IR/Instruction.def"
768 static int map_from_llvmopcode(LLVMOpcode code)
771 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
772 #include "llvm/IR/Instruction.def"
775 llvm_unreachable("Unhandled Opcode.");
778 /*--.. Constant expressions ................................................--*/
780 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
781 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
784 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
785 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
788 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
789 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
792 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
793 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
796 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
797 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
800 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
801 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
805 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
806 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
809 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
810 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
813 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
814 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
815 unwrap<Constant>(RHSConstant)));
818 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
819 LLVMValueRef RHSConstant) {
820 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
821 unwrap<Constant>(RHSConstant)));
824 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
825 LLVMValueRef RHSConstant) {
826 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
827 unwrap<Constant>(RHSConstant)));
830 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
831 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
832 unwrap<Constant>(RHSConstant)));
835 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
836 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
837 unwrap<Constant>(RHSConstant)));
840 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
841 LLVMValueRef RHSConstant) {
842 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
843 unwrap<Constant>(RHSConstant)));
846 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
847 LLVMValueRef RHSConstant) {
848 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
849 unwrap<Constant>(RHSConstant)));
852 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
853 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
854 unwrap<Constant>(RHSConstant)));
857 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
858 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
859 unwrap<Constant>(RHSConstant)));
862 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
863 LLVMValueRef RHSConstant) {
864 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
865 unwrap<Constant>(RHSConstant)));
868 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
869 LLVMValueRef RHSConstant) {
870 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
871 unwrap<Constant>(RHSConstant)));
874 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
875 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
876 unwrap<Constant>(RHSConstant)));
879 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
880 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
881 unwrap<Constant>(RHSConstant)));
884 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
885 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
886 unwrap<Constant>(RHSConstant)));
889 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
890 LLVMValueRef RHSConstant) {
891 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
892 unwrap<Constant>(RHSConstant)));
895 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
896 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
897 unwrap<Constant>(RHSConstant)));
900 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
901 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
902 unwrap<Constant>(RHSConstant)));
905 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
906 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
907 unwrap<Constant>(RHSConstant)));
910 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
911 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
912 unwrap<Constant>(RHSConstant)));
915 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
916 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
921 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
922 unwrap<Constant>(RHSConstant)));
925 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
926 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
927 unwrap<Constant>(RHSConstant)));
930 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
931 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
932 return wrap(ConstantExpr::getICmp(Predicate,
933 unwrap<Constant>(LHSConstant),
934 unwrap<Constant>(RHSConstant)));
937 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
938 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
939 return wrap(ConstantExpr::getFCmp(Predicate,
940 unwrap<Constant>(LHSConstant),
941 unwrap<Constant>(RHSConstant)));
944 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
945 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
946 unwrap<Constant>(RHSConstant)));
949 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
950 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
951 unwrap<Constant>(RHSConstant)));
954 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
955 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
956 unwrap<Constant>(RHSConstant)));
959 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
960 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
961 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
963 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
967 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
968 LLVMValueRef *ConstantIndices,
969 unsigned NumIndices) {
970 Constant* Val = unwrap<Constant>(ConstantVal);
971 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
973 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
976 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
977 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
981 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
982 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
986 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
987 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
991 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
992 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
996 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
997 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1001 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1002 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1006 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1007 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1011 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1012 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1016 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1017 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1021 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1022 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1026 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1027 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1031 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1032 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1036 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1037 LLVMTypeRef ToType) {
1038 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1042 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1043 LLVMTypeRef ToType) {
1044 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1048 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1049 LLVMTypeRef ToType) {
1050 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1054 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1055 LLVMTypeRef ToType) {
1056 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1060 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1061 LLVMBool isSigned) {
1062 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1063 unwrap(ToType), isSigned));
1066 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1067 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1071 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1072 LLVMValueRef ConstantIfTrue,
1073 LLVMValueRef ConstantIfFalse) {
1074 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1075 unwrap<Constant>(ConstantIfTrue),
1076 unwrap<Constant>(ConstantIfFalse)));
1079 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1080 LLVMValueRef IndexConstant) {
1081 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1082 unwrap<Constant>(IndexConstant)));
1085 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1086 LLVMValueRef ElementValueConstant,
1087 LLVMValueRef IndexConstant) {
1088 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1089 unwrap<Constant>(ElementValueConstant),
1090 unwrap<Constant>(IndexConstant)));
1093 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1094 LLVMValueRef VectorBConstant,
1095 LLVMValueRef MaskConstant) {
1096 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1097 unwrap<Constant>(VectorBConstant),
1098 unwrap<Constant>(MaskConstant)));
1101 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1103 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1104 makeArrayRef(IdxList, NumIdx)));
1107 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1108 LLVMValueRef ElementValueConstant,
1109 unsigned *IdxList, unsigned NumIdx) {
1110 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1111 unwrap<Constant>(ElementValueConstant),
1112 makeArrayRef(IdxList, NumIdx)));
1115 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1116 const char *Constraints,
1117 LLVMBool HasSideEffects,
1118 LLVMBool IsAlignStack) {
1119 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1120 Constraints, HasSideEffects, IsAlignStack));
1123 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1124 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1127 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1129 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1130 return wrap(unwrap<GlobalValue>(Global)->getParent());
1133 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1134 return unwrap<GlobalValue>(Global)->isDeclaration();
1137 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1138 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1139 case GlobalValue::ExternalLinkage:
1140 return LLVMExternalLinkage;
1141 case GlobalValue::AvailableExternallyLinkage:
1142 return LLVMAvailableExternallyLinkage;
1143 case GlobalValue::LinkOnceAnyLinkage:
1144 return LLVMLinkOnceAnyLinkage;
1145 case GlobalValue::LinkOnceODRLinkage:
1146 return LLVMLinkOnceODRLinkage;
1147 case GlobalValue::WeakAnyLinkage:
1148 return LLVMWeakAnyLinkage;
1149 case GlobalValue::WeakODRLinkage:
1150 return LLVMWeakODRLinkage;
1151 case GlobalValue::AppendingLinkage:
1152 return LLVMAppendingLinkage;
1153 case GlobalValue::InternalLinkage:
1154 return LLVMInternalLinkage;
1155 case GlobalValue::PrivateLinkage:
1156 return LLVMPrivateLinkage;
1157 case GlobalValue::LinkerPrivateLinkage:
1158 return LLVMLinkerPrivateLinkage;
1159 case GlobalValue::LinkerPrivateWeakLinkage:
1160 return LLVMLinkerPrivateWeakLinkage;
1161 case GlobalValue::DLLImportLinkage:
1162 return LLVMDLLImportLinkage;
1163 case GlobalValue::DLLExportLinkage:
1164 return LLVMDLLExportLinkage;
1165 case GlobalValue::ExternalWeakLinkage:
1166 return LLVMExternalWeakLinkage;
1167 case GlobalValue::CommonLinkage:
1168 return LLVMCommonLinkage;
1171 llvm_unreachable("Invalid GlobalValue linkage!");
1174 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1175 GlobalValue *GV = unwrap<GlobalValue>(Global);
1178 case LLVMExternalLinkage:
1179 GV->setLinkage(GlobalValue::ExternalLinkage);
1181 case LLVMAvailableExternallyLinkage:
1182 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1184 case LLVMLinkOnceAnyLinkage:
1185 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1187 case LLVMLinkOnceODRLinkage:
1188 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1190 case LLVMLinkOnceODRAutoHideLinkage:
1191 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1192 "longer supported.");
1194 case LLVMWeakAnyLinkage:
1195 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1197 case LLVMWeakODRLinkage:
1198 GV->setLinkage(GlobalValue::WeakODRLinkage);
1200 case LLVMAppendingLinkage:
1201 GV->setLinkage(GlobalValue::AppendingLinkage);
1203 case LLVMInternalLinkage:
1204 GV->setLinkage(GlobalValue::InternalLinkage);
1206 case LLVMPrivateLinkage:
1207 GV->setLinkage(GlobalValue::PrivateLinkage);
1209 case LLVMLinkerPrivateLinkage:
1210 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1212 case LLVMLinkerPrivateWeakLinkage:
1213 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1215 case LLVMDLLImportLinkage:
1216 GV->setLinkage(GlobalValue::DLLImportLinkage);
1218 case LLVMDLLExportLinkage:
1219 GV->setLinkage(GlobalValue::DLLExportLinkage);
1221 case LLVMExternalWeakLinkage:
1222 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1224 case LLVMGhostLinkage:
1226 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1228 case LLVMCommonLinkage:
1229 GV->setLinkage(GlobalValue::CommonLinkage);
1234 const char *LLVMGetSection(LLVMValueRef Global) {
1235 return unwrap<GlobalValue>(Global)->getSection().c_str();
1238 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1239 unwrap<GlobalValue>(Global)->setSection(Section);
1242 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1243 return static_cast<LLVMVisibility>(
1244 unwrap<GlobalValue>(Global)->getVisibility());
1247 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1248 unwrap<GlobalValue>(Global)
1249 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1252 /*--.. Operations on global variables, load and store instructions .........--*/
1254 unsigned LLVMGetAlignment(LLVMValueRef V) {
1255 Value *P = unwrap<Value>(V);
1256 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1257 return GV->getAlignment();
1258 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1259 return LI->getAlignment();
1260 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1261 return SI->getAlignment();
1263 llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1266 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1267 Value *P = unwrap<Value>(V);
1268 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1269 GV->setAlignment(Bytes);
1270 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1271 LI->setAlignment(Bytes);
1272 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1273 SI->setAlignment(Bytes);
1275 llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1278 /*--.. Operations on global variables ......................................--*/
1280 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1281 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1282 GlobalValue::ExternalLinkage, 0, Name));
1285 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1287 unsigned AddressSpace) {
1288 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1289 GlobalValue::ExternalLinkage, 0, Name, 0,
1290 GlobalVariable::NotThreadLocal, AddressSpace));
1293 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1294 return wrap(unwrap(M)->getNamedGlobal(Name));
1297 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1298 Module *Mod = unwrap(M);
1299 Module::global_iterator I = Mod->global_begin();
1300 if (I == Mod->global_end())
1305 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1306 Module *Mod = unwrap(M);
1307 Module::global_iterator I = Mod->global_end();
1308 if (I == Mod->global_begin())
1313 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1314 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1315 Module::global_iterator I = GV;
1316 if (++I == GV->getParent()->global_end())
1321 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1322 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1323 Module::global_iterator I = GV;
1324 if (I == GV->getParent()->global_begin())
1329 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1330 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1333 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1334 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1335 if ( !GV->hasInitializer() )
1337 return wrap(GV->getInitializer());
1340 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1341 unwrap<GlobalVariable>(GlobalVar)
1342 ->setInitializer(unwrap<Constant>(ConstantVal));
1345 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1346 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1349 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1350 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1353 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1354 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1357 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1358 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1361 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1362 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1363 case GlobalVariable::NotThreadLocal:
1364 return LLVMNotThreadLocal;
1365 case GlobalVariable::GeneralDynamicTLSModel:
1366 return LLVMGeneralDynamicTLSModel;
1367 case GlobalVariable::LocalDynamicTLSModel:
1368 return LLVMLocalDynamicTLSModel;
1369 case GlobalVariable::InitialExecTLSModel:
1370 return LLVMInitialExecTLSModel;
1371 case GlobalVariable::LocalExecTLSModel:
1372 return LLVMLocalExecTLSModel;
1375 llvm_unreachable("Invalid GlobalVariable thread local mode");
1378 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1379 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1382 case LLVMNotThreadLocal:
1383 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1385 case LLVMGeneralDynamicTLSModel:
1386 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1388 case LLVMLocalDynamicTLSModel:
1389 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1391 case LLVMInitialExecTLSModel:
1392 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1394 case LLVMLocalExecTLSModel:
1395 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1400 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1401 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1404 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1405 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1408 /*--.. Operations on aliases ......................................--*/
1410 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1412 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1413 unwrap<Constant>(Aliasee), unwrap (M)));
1416 /*--.. Operations on functions .............................................--*/
1418 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1419 LLVMTypeRef FunctionTy) {
1420 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1421 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1424 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1425 return wrap(unwrap(M)->getFunction(Name));
1428 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1429 Module *Mod = unwrap(M);
1430 Module::iterator I = Mod->begin();
1431 if (I == Mod->end())
1436 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1437 Module *Mod = unwrap(M);
1438 Module::iterator I = Mod->end();
1439 if (I == Mod->begin())
1444 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1445 Function *Func = unwrap<Function>(Fn);
1446 Module::iterator I = Func;
1447 if (++I == Func->getParent()->end())
1452 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1453 Function *Func = unwrap<Function>(Fn);
1454 Module::iterator I = Func;
1455 if (I == Func->getParent()->begin())
1460 void LLVMDeleteFunction(LLVMValueRef Fn) {
1461 unwrap<Function>(Fn)->eraseFromParent();
1464 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1465 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1466 return F->getIntrinsicID();
1470 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1471 return unwrap<Function>(Fn)->getCallingConv();
1474 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1475 return unwrap<Function>(Fn)->setCallingConv(
1476 static_cast<CallingConv::ID>(CC));
1479 const char *LLVMGetGC(LLVMValueRef Fn) {
1480 Function *F = unwrap<Function>(Fn);
1481 return F->hasGC()? F->getGC() : 0;
1484 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1485 Function *F = unwrap<Function>(Fn);
1492 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1493 Function *Func = unwrap<Function>(Fn);
1494 const AttributeSet PAL = Func->getAttributes();
1496 const AttributeSet PALnew =
1497 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1498 AttributeSet::get(Func->getContext(),
1499 AttributeSet::FunctionIndex, B));
1500 Func->setAttributes(PALnew);
1503 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1505 Function *Func = unwrap<Function>(Fn);
1506 AttributeSet::AttrIndex Idx =
1507 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1510 B.addAttribute(A, V);
1511 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1512 Func->addAttributes(Idx, Set);
1515 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1516 Function *Func = unwrap<Function>(Fn);
1517 const AttributeSet PAL = Func->getAttributes();
1519 const AttributeSet PALnew =
1520 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1521 AttributeSet::get(Func->getContext(),
1522 AttributeSet::FunctionIndex, B));
1523 Func->setAttributes(PALnew);
1526 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1527 Function *Func = unwrap<Function>(Fn);
1528 const AttributeSet PAL = Func->getAttributes();
1529 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1532 /*--.. Operations on parameters ............................................--*/
1534 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1535 // This function is strictly redundant to
1536 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1537 return unwrap<Function>(FnRef)->arg_size();
1540 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1541 Function *Fn = unwrap<Function>(FnRef);
1542 for (Function::arg_iterator I = Fn->arg_begin(),
1543 E = Fn->arg_end(); I != E; I++)
1544 *ParamRefs++ = wrap(I);
1547 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1548 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1554 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1555 return wrap(unwrap<Argument>(V)->getParent());
1558 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1559 Function *Func = unwrap<Function>(Fn);
1560 Function::arg_iterator I = Func->arg_begin();
1561 if (I == Func->arg_end())
1566 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1567 Function *Func = unwrap<Function>(Fn);
1568 Function::arg_iterator I = Func->arg_end();
1569 if (I == Func->arg_begin())
1574 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1575 Argument *A = unwrap<Argument>(Arg);
1576 Function::arg_iterator I = A;
1577 if (++I == A->getParent()->arg_end())
1582 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1583 Argument *A = unwrap<Argument>(Arg);
1584 Function::arg_iterator I = A;
1585 if (I == A->getParent()->arg_begin())
1590 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1591 Argument *A = unwrap<Argument>(Arg);
1593 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1596 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1597 Argument *A = unwrap<Argument>(Arg);
1599 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1602 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1603 Argument *A = unwrap<Argument>(Arg);
1604 return (LLVMAttribute)A->getParent()->getAttributes().
1605 Raw(A->getArgNo()+1);
1609 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1610 Argument *A = unwrap<Argument>(Arg);
1612 B.addAlignmentAttr(align);
1613 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1616 /*--.. Operations on basic blocks ..........................................--*/
1618 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1619 return wrap(static_cast<Value*>(unwrap(BB)));
1622 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1623 return isa<BasicBlock>(unwrap(Val));
1626 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1627 return wrap(unwrap<BasicBlock>(Val));
1630 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1631 return wrap(unwrap(BB)->getParent());
1634 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1635 return wrap(unwrap(BB)->getTerminator());
1638 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1639 return unwrap<Function>(FnRef)->size();
1642 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1643 Function *Fn = unwrap<Function>(FnRef);
1644 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1645 *BasicBlocksRefs++ = wrap(I);
1648 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1649 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1652 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1653 Function *Func = unwrap<Function>(Fn);
1654 Function::iterator I = Func->begin();
1655 if (I == Func->end())
1660 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1661 Function *Func = unwrap<Function>(Fn);
1662 Function::iterator I = Func->end();
1663 if (I == Func->begin())
1668 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1669 BasicBlock *Block = unwrap(BB);
1670 Function::iterator I = Block;
1671 if (++I == Block->getParent()->end())
1676 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1677 BasicBlock *Block = unwrap(BB);
1678 Function::iterator I = Block;
1679 if (I == Block->getParent()->begin())
1684 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1687 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1690 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1691 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1694 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1695 LLVMBasicBlockRef BBRef,
1697 BasicBlock *BB = unwrap(BBRef);
1698 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1701 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1703 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1706 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1707 unwrap(BBRef)->eraseFromParent();
1710 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1711 unwrap(BBRef)->removeFromParent();
1714 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1715 unwrap(BB)->moveBefore(unwrap(MovePos));
1718 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1719 unwrap(BB)->moveAfter(unwrap(MovePos));
1722 /*--.. Operations on instructions ..........................................--*/
1724 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1725 return wrap(unwrap<Instruction>(Inst)->getParent());
1728 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1729 BasicBlock *Block = unwrap(BB);
1730 BasicBlock::iterator I = Block->begin();
1731 if (I == Block->end())
1736 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1737 BasicBlock *Block = unwrap(BB);
1738 BasicBlock::iterator I = Block->end();
1739 if (I == Block->begin())
1744 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1745 Instruction *Instr = unwrap<Instruction>(Inst);
1746 BasicBlock::iterator I = Instr;
1747 if (++I == Instr->getParent()->end())
1752 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1753 Instruction *Instr = unwrap<Instruction>(Inst);
1754 BasicBlock::iterator I = Instr;
1755 if (I == Instr->getParent()->begin())
1760 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1761 unwrap<Instruction>(Inst)->eraseFromParent();
1764 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1765 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1766 return (LLVMIntPredicate)I->getPredicate();
1767 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1768 if (CE->getOpcode() == Instruction::ICmp)
1769 return (LLVMIntPredicate)CE->getPredicate();
1770 return (LLVMIntPredicate)0;
1773 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1774 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1775 return map_to_llvmopcode(C->getOpcode());
1776 return (LLVMOpcode)0;
1779 /*--.. Call and invoke instructions ........................................--*/
1781 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1782 Value *V = unwrap(Instr);
1783 if (CallInst *CI = dyn_cast<CallInst>(V))
1784 return CI->getCallingConv();
1785 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1786 return II->getCallingConv();
1787 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1790 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1791 Value *V = unwrap(Instr);
1792 if (CallInst *CI = dyn_cast<CallInst>(V))
1793 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1794 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1795 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1796 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1799 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1801 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1804 Call.getAttributes().addAttributes(Call->getContext(), index,
1805 AttributeSet::get(Call->getContext(),
1809 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1811 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1813 Call.setAttributes(Call.getAttributes()
1814 .removeAttributes(Call->getContext(), index,
1815 AttributeSet::get(Call->getContext(),
1819 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1821 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1823 B.addAlignmentAttr(align);
1824 Call.setAttributes(Call.getAttributes()
1825 .addAttributes(Call->getContext(), index,
1826 AttributeSet::get(Call->getContext(),
1830 /*--.. Operations on call instructions (only) ..............................--*/
1832 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1833 return unwrap<CallInst>(Call)->isTailCall();
1836 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1837 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1840 /*--.. Operations on switch instructions (only) ............................--*/
1842 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1843 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1846 /*--.. Operations on phi nodes .............................................--*/
1848 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1849 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1850 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1851 for (unsigned I = 0; I != Count; ++I)
1852 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1855 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1856 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1859 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1860 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1863 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1864 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1868 /*===-- Instruction builders ----------------------------------------------===*/
1870 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1871 return wrap(new IRBuilder<>(*unwrap(C)));
1874 LLVMBuilderRef LLVMCreateBuilder(void) {
1875 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1878 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1879 LLVMValueRef Instr) {
1880 BasicBlock *BB = unwrap(Block);
1881 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1882 unwrap(Builder)->SetInsertPoint(BB, I);
1885 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1886 Instruction *I = unwrap<Instruction>(Instr);
1887 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1890 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1891 BasicBlock *BB = unwrap(Block);
1892 unwrap(Builder)->SetInsertPoint(BB);
1895 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1896 return wrap(unwrap(Builder)->GetInsertBlock());
1899 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1900 unwrap(Builder)->ClearInsertionPoint();
1903 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1904 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1907 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1909 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1912 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1913 delete unwrap(Builder);
1916 /*--.. Metadata builders ...................................................--*/
1918 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1919 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1920 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1923 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1924 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1925 .getAsMDNode(unwrap(Builder)->getContext()));
1928 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1929 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1933 /*--.. Instruction builders ................................................--*/
1935 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1936 return wrap(unwrap(B)->CreateRetVoid());
1939 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1940 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1943 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1945 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1948 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1949 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1952 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1953 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1954 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1957 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1958 LLVMBasicBlockRef Else, unsigned NumCases) {
1959 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1962 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1963 unsigned NumDests) {
1964 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1967 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1968 LLVMValueRef *Args, unsigned NumArgs,
1969 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1971 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1972 makeArrayRef(unwrap(Args), NumArgs),
1976 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1977 LLVMValueRef PersFn, unsigned NumClauses,
1979 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1980 cast<Function>(unwrap(PersFn)),
1984 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1985 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1988 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1989 return wrap(unwrap(B)->CreateUnreachable());
1992 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1993 LLVMBasicBlockRef Dest) {
1994 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1997 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1998 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2001 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2002 unwrap<LandingPadInst>(LandingPad)->
2003 addClause(cast<Constant>(unwrap(ClauseVal)));
2006 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2007 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2010 /*--.. Arithmetic ..........................................................--*/
2012 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2014 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2017 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2019 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2022 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2024 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2027 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2029 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2032 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2034 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2037 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2039 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2042 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2044 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2047 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2049 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2052 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2054 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2057 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2059 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2062 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2064 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2067 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2069 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2072 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2074 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2077 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2079 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2082 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2083 LLVMValueRef RHS, const char *Name) {
2084 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2087 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2089 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2092 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2094 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2097 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2099 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2102 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2104 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2107 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2109 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2112 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2114 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2117 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2119 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2122 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2124 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2127 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2129 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2132 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2134 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2137 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2138 LLVMValueRef LHS, LLVMValueRef RHS,
2140 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2141 unwrap(RHS), Name));
2144 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2145 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2148 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2150 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2153 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2155 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2158 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2159 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2162 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2163 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2166 /*--.. Memory ..............................................................--*/
2168 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2170 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2171 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2172 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2173 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2174 ITy, unwrap(Ty), AllocSize,
2176 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2179 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2180 LLVMValueRef Val, const char *Name) {
2181 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2182 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2183 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2184 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2185 ITy, unwrap(Ty), AllocSize,
2186 unwrap(Val), 0, "");
2187 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2190 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2192 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2195 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2196 LLVMValueRef Val, const char *Name) {
2197 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2200 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2201 return wrap(unwrap(B)->Insert(
2202 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2206 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2208 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2211 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2212 LLVMValueRef PointerVal) {
2213 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2216 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2217 LLVMValueRef *Indices, unsigned NumIndices,
2219 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2220 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2223 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2224 LLVMValueRef *Indices, unsigned NumIndices,
2226 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2227 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2230 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2231 unsigned Idx, const char *Name) {
2232 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2235 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2237 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2240 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2242 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2245 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2246 Value *P = unwrap<Value>(MemAccessInst);
2247 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2248 return LI->isVolatile();
2249 return cast<StoreInst>(P)->isVolatile();
2252 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2253 Value *P = unwrap<Value>(MemAccessInst);
2254 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2255 return LI->setVolatile(isVolatile);
2256 return cast<StoreInst>(P)->setVolatile(isVolatile);
2259 /*--.. Casts ...............................................................--*/
2261 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2262 LLVMTypeRef DestTy, const char *Name) {
2263 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2266 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2267 LLVMTypeRef DestTy, const char *Name) {
2268 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2271 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2272 LLVMTypeRef DestTy, const char *Name) {
2273 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2276 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2277 LLVMTypeRef DestTy, const char *Name) {
2278 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2281 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2282 LLVMTypeRef DestTy, const char *Name) {
2283 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2286 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2287 LLVMTypeRef DestTy, const char *Name) {
2288 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2291 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2292 LLVMTypeRef DestTy, const char *Name) {
2293 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2296 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2297 LLVMTypeRef DestTy, const char *Name) {
2298 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2301 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2302 LLVMTypeRef DestTy, const char *Name) {
2303 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2306 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2307 LLVMTypeRef DestTy, const char *Name) {
2308 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2311 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2312 LLVMTypeRef DestTy, const char *Name) {
2313 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2316 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2317 LLVMTypeRef DestTy, const char *Name) {
2318 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2321 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2322 LLVMTypeRef DestTy, const char *Name) {
2323 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2327 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2328 LLVMTypeRef DestTy, const char *Name) {
2329 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2333 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2334 LLVMTypeRef DestTy, const char *Name) {
2335 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2339 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2340 LLVMTypeRef DestTy, const char *Name) {
2341 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2342 unwrap(DestTy), Name));
2345 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2346 LLVMTypeRef DestTy, const char *Name) {
2347 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2350 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2351 LLVMTypeRef DestTy, const char *Name) {
2352 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2353 /*isSigned*/true, Name));
2356 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2357 LLVMTypeRef DestTy, const char *Name) {
2358 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2361 /*--.. Comparisons .........................................................--*/
2363 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2364 LLVMValueRef LHS, LLVMValueRef RHS,
2366 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2367 unwrap(LHS), unwrap(RHS), Name));
2370 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2371 LLVMValueRef LHS, LLVMValueRef RHS,
2373 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2374 unwrap(LHS), unwrap(RHS), Name));
2377 /*--.. Miscellaneous instructions ..........................................--*/
2379 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2380 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2383 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2384 LLVMValueRef *Args, unsigned NumArgs,
2386 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2387 makeArrayRef(unwrap(Args), NumArgs),
2391 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2392 LLVMValueRef Then, LLVMValueRef Else,
2394 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2398 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2399 LLVMTypeRef Ty, const char *Name) {
2400 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2403 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2404 LLVMValueRef Index, const char *Name) {
2405 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2409 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2410 LLVMValueRef EltVal, LLVMValueRef Index,
2412 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2413 unwrap(Index), Name));
2416 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2417 LLVMValueRef V2, LLVMValueRef Mask,
2419 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2420 unwrap(Mask), Name));
2423 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2424 unsigned Index, const char *Name) {
2425 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2428 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2429 LLVMValueRef EltVal, unsigned Index,
2431 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2435 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2437 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2440 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2442 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2445 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2446 LLVMValueRef RHS, const char *Name) {
2447 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2450 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2451 LLVMValueRef PTR, LLVMValueRef Val,
2452 LLVMAtomicOrdering ordering,
2453 LLVMBool singleThread) {
2454 AtomicRMWInst::BinOp intop;
2456 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2457 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2458 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2459 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2460 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2461 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2462 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2463 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2464 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2465 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2466 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2468 AtomicOrdering intordering;
2470 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2471 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2472 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2473 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2474 case LLVMAtomicOrderingRelease: intordering = Release; break;
2475 case LLVMAtomicOrderingAcquireRelease:
2476 intordering = AcquireRelease;
2478 case LLVMAtomicOrderingSequentiallyConsistent:
2479 intordering = SequentiallyConsistent;
2482 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2483 intordering, singleThread ? SingleThread : CrossThread));
2487 /*===-- Module providers --------------------------------------------------===*/
2489 LLVMModuleProviderRef
2490 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2491 return reinterpret_cast<LLVMModuleProviderRef>(M);
2494 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2499 /*===-- Memory buffers ----------------------------------------------------===*/
2501 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2503 LLVMMemoryBufferRef *OutMemBuf,
2504 char **OutMessage) {
2506 OwningPtr<MemoryBuffer> MB;
2508 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2509 *OutMemBuf = wrap(MB.take());
2513 *OutMessage = strdup(ec.message().c_str());
2517 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2518 char **OutMessage) {
2519 OwningPtr<MemoryBuffer> MB;
2521 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2522 *OutMemBuf = wrap(MB.take());
2526 *OutMessage = strdup(ec.message().c_str());
2530 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2531 const char *InputData,
2532 size_t InputDataLength,
2533 const char *BufferName,
2534 LLVMBool RequiresNullTerminator) {
2536 return wrap(MemoryBuffer::getMemBuffer(
2537 StringRef(InputData, InputDataLength),
2538 StringRef(BufferName),
2539 RequiresNullTerminator));
2542 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2543 const char *InputData,
2544 size_t InputDataLength,
2545 const char *BufferName) {
2547 return wrap(MemoryBuffer::getMemBufferCopy(
2548 StringRef(InputData, InputDataLength),
2549 StringRef(BufferName)));
2552 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2553 return unwrap(MemBuf)->getBufferStart();
2556 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2557 return unwrap(MemBuf)->getBufferSize();
2560 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2561 delete unwrap(MemBuf);
2564 /*===-- Pass Registry -----------------------------------------------------===*/
2566 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2567 return wrap(PassRegistry::getPassRegistry());
2570 /*===-- Pass Manager ------------------------------------------------------===*/
2572 LLVMPassManagerRef LLVMCreatePassManager() {
2573 return wrap(new PassManager());
2576 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2577 return wrap(new FunctionPassManager(unwrap(M)));
2580 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2581 return LLVMCreateFunctionPassManagerForModule(
2582 reinterpret_cast<LLVMModuleRef>(P));
2585 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2586 return unwrap<PassManager>(PM)->run(*unwrap(M));
2589 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2590 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2593 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2594 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2597 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2598 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2601 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2605 /*===-- Threading ------------------------------------------------------===*/
2607 LLVMBool LLVMStartMultithreaded() {
2608 return llvm_start_multithreaded();
2611 void LLVMStopMultithreaded() {
2612 llvm_stop_multithreaded();
2615 LLVMBool LLVMIsMultithreaded() {
2616 return llvm_is_multithreaded();