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::LinkOnceODRAutoHideLinkage:
1138 return LLVMLinkOnceODRAutoHideLinkage;
1139 case GlobalValue::WeakAnyLinkage:
1140 return LLVMWeakAnyLinkage;
1141 case GlobalValue::WeakODRLinkage:
1142 return LLVMWeakODRLinkage;
1143 case GlobalValue::AppendingLinkage:
1144 return LLVMAppendingLinkage;
1145 case GlobalValue::InternalLinkage:
1146 return LLVMInternalLinkage;
1147 case GlobalValue::PrivateLinkage:
1148 return LLVMPrivateLinkage;
1149 case GlobalValue::LinkerPrivateLinkage:
1150 return LLVMLinkerPrivateLinkage;
1151 case GlobalValue::LinkerPrivateWeakLinkage:
1152 return LLVMLinkerPrivateWeakLinkage;
1153 case GlobalValue::DLLImportLinkage:
1154 return LLVMDLLImportLinkage;
1155 case GlobalValue::DLLExportLinkage:
1156 return LLVMDLLExportLinkage;
1157 case GlobalValue::ExternalWeakLinkage:
1158 return LLVMExternalWeakLinkage;
1159 case GlobalValue::CommonLinkage:
1160 return LLVMCommonLinkage;
1163 llvm_unreachable("Invalid GlobalValue linkage!");
1166 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1167 GlobalValue *GV = unwrap<GlobalValue>(Global);
1170 case LLVMExternalLinkage:
1171 GV->setLinkage(GlobalValue::ExternalLinkage);
1173 case LLVMAvailableExternallyLinkage:
1174 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1176 case LLVMLinkOnceAnyLinkage:
1177 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1179 case LLVMLinkOnceODRLinkage:
1180 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1182 case LLVMLinkOnceODRAutoHideLinkage:
1183 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1185 case LLVMWeakAnyLinkage:
1186 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1188 case LLVMWeakODRLinkage:
1189 GV->setLinkage(GlobalValue::WeakODRLinkage);
1191 case LLVMAppendingLinkage:
1192 GV->setLinkage(GlobalValue::AppendingLinkage);
1194 case LLVMInternalLinkage:
1195 GV->setLinkage(GlobalValue::InternalLinkage);
1197 case LLVMPrivateLinkage:
1198 GV->setLinkage(GlobalValue::PrivateLinkage);
1200 case LLVMLinkerPrivateLinkage:
1201 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1203 case LLVMLinkerPrivateWeakLinkage:
1204 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1206 case LLVMDLLImportLinkage:
1207 GV->setLinkage(GlobalValue::DLLImportLinkage);
1209 case LLVMDLLExportLinkage:
1210 GV->setLinkage(GlobalValue::DLLExportLinkage);
1212 case LLVMExternalWeakLinkage:
1213 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1215 case LLVMGhostLinkage:
1217 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1219 case LLVMCommonLinkage:
1220 GV->setLinkage(GlobalValue::CommonLinkage);
1225 const char *LLVMGetSection(LLVMValueRef Global) {
1226 return unwrap<GlobalValue>(Global)->getSection().c_str();
1229 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1230 unwrap<GlobalValue>(Global)->setSection(Section);
1233 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1234 return static_cast<LLVMVisibility>(
1235 unwrap<GlobalValue>(Global)->getVisibility());
1238 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1239 unwrap<GlobalValue>(Global)
1240 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1243 /*--.. Operations on global variables, load and store instructions .........--*/
1245 unsigned LLVMGetAlignment(LLVMValueRef V) {
1246 Value *P = unwrap<Value>(V);
1247 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1248 return GV->getAlignment();
1249 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1250 return LI->getAlignment();
1251 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1252 return SI->getAlignment();
1254 llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1257 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1258 Value *P = unwrap<Value>(V);
1259 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1260 GV->setAlignment(Bytes);
1261 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1262 LI->setAlignment(Bytes);
1263 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1264 SI->setAlignment(Bytes);
1266 llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment");
1269 /*--.. Operations on global variables ......................................--*/
1271 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1272 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1273 GlobalValue::ExternalLinkage, 0, Name));
1276 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1278 unsigned AddressSpace) {
1279 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1280 GlobalValue::ExternalLinkage, 0, Name, 0,
1281 GlobalVariable::NotThreadLocal, AddressSpace));
1284 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1285 return wrap(unwrap(M)->getNamedGlobal(Name));
1288 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1289 Module *Mod = unwrap(M);
1290 Module::global_iterator I = Mod->global_begin();
1291 if (I == Mod->global_end())
1296 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1297 Module *Mod = unwrap(M);
1298 Module::global_iterator I = Mod->global_end();
1299 if (I == Mod->global_begin())
1304 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1305 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1306 Module::global_iterator I = GV;
1307 if (++I == GV->getParent()->global_end())
1312 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1313 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1314 Module::global_iterator I = GV;
1315 if (I == GV->getParent()->global_begin())
1320 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1321 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1324 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1325 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1326 if ( !GV->hasInitializer() )
1328 return wrap(GV->getInitializer());
1331 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1332 unwrap<GlobalVariable>(GlobalVar)
1333 ->setInitializer(unwrap<Constant>(ConstantVal));
1336 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1337 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1340 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1341 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1344 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1345 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1348 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1349 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1352 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1353 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1354 case GlobalVariable::NotThreadLocal:
1355 return LLVMNotThreadLocal;
1356 case GlobalVariable::GeneralDynamicTLSModel:
1357 return LLVMGeneralDynamicTLSModel;
1358 case GlobalVariable::LocalDynamicTLSModel:
1359 return LLVMLocalDynamicTLSModel;
1360 case GlobalVariable::InitialExecTLSModel:
1361 return LLVMInitialExecTLSModel;
1362 case GlobalVariable::LocalExecTLSModel:
1363 return LLVMLocalExecTLSModel;
1366 llvm_unreachable("Invalid GlobalVariable thread local mode");
1369 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1370 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1373 case LLVMNotThreadLocal:
1374 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1376 case LLVMGeneralDynamicTLSModel:
1377 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1379 case LLVMLocalDynamicTLSModel:
1380 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1382 case LLVMInitialExecTLSModel:
1383 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1385 case LLVMLocalExecTLSModel:
1386 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1391 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1392 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1395 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1396 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1399 /*--.. Operations on aliases ......................................--*/
1401 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1403 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1404 unwrap<Constant>(Aliasee), unwrap (M)));
1407 /*--.. Operations on functions .............................................--*/
1409 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1410 LLVMTypeRef FunctionTy) {
1411 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1412 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1415 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1416 return wrap(unwrap(M)->getFunction(Name));
1419 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1420 Module *Mod = unwrap(M);
1421 Module::iterator I = Mod->begin();
1422 if (I == Mod->end())
1427 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1428 Module *Mod = unwrap(M);
1429 Module::iterator I = Mod->end();
1430 if (I == Mod->begin())
1435 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1436 Function *Func = unwrap<Function>(Fn);
1437 Module::iterator I = Func;
1438 if (++I == Func->getParent()->end())
1443 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1444 Function *Func = unwrap<Function>(Fn);
1445 Module::iterator I = Func;
1446 if (I == Func->getParent()->begin())
1451 void LLVMDeleteFunction(LLVMValueRef Fn) {
1452 unwrap<Function>(Fn)->eraseFromParent();
1455 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1456 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1457 return F->getIntrinsicID();
1461 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1462 return unwrap<Function>(Fn)->getCallingConv();
1465 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1466 return unwrap<Function>(Fn)->setCallingConv(
1467 static_cast<CallingConv::ID>(CC));
1470 const char *LLVMGetGC(LLVMValueRef Fn) {
1471 Function *F = unwrap<Function>(Fn);
1472 return F->hasGC()? F->getGC() : 0;
1475 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1476 Function *F = unwrap<Function>(Fn);
1483 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1484 Function *Func = unwrap<Function>(Fn);
1485 const AttributeSet PAL = Func->getAttributes();
1487 const AttributeSet PALnew =
1488 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1489 AttributeSet::get(Func->getContext(),
1490 AttributeSet::FunctionIndex, B));
1491 Func->setAttributes(PALnew);
1494 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1496 Function *Func = unwrap<Function>(Fn);
1497 AttributeSet::AttrIndex Idx =
1498 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1501 B.addAttribute(A, V);
1502 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1503 Func->addAttributes(Idx, Set);
1506 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1507 Function *Func = unwrap<Function>(Fn);
1508 const AttributeSet PAL = Func->getAttributes();
1510 const AttributeSet PALnew =
1511 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1512 AttributeSet::get(Func->getContext(),
1513 AttributeSet::FunctionIndex, B));
1514 Func->setAttributes(PALnew);
1517 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1518 Function *Func = unwrap<Function>(Fn);
1519 const AttributeSet PAL = Func->getAttributes();
1520 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1523 /*--.. Operations on parameters ............................................--*/
1525 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1526 // This function is strictly redundant to
1527 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1528 return unwrap<Function>(FnRef)->arg_size();
1531 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1532 Function *Fn = unwrap<Function>(FnRef);
1533 for (Function::arg_iterator I = Fn->arg_begin(),
1534 E = Fn->arg_end(); I != E; I++)
1535 *ParamRefs++ = wrap(I);
1538 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1539 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1545 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1546 return wrap(unwrap<Argument>(V)->getParent());
1549 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1550 Function *Func = unwrap<Function>(Fn);
1551 Function::arg_iterator I = Func->arg_begin();
1552 if (I == Func->arg_end())
1557 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1558 Function *Func = unwrap<Function>(Fn);
1559 Function::arg_iterator I = Func->arg_end();
1560 if (I == Func->arg_begin())
1565 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1566 Argument *A = unwrap<Argument>(Arg);
1567 Function::arg_iterator I = A;
1568 if (++I == A->getParent()->arg_end())
1573 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1574 Argument *A = unwrap<Argument>(Arg);
1575 Function::arg_iterator I = A;
1576 if (I == A->getParent()->arg_begin())
1581 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1582 Argument *A = unwrap<Argument>(Arg);
1584 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1587 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1588 Argument *A = unwrap<Argument>(Arg);
1590 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1593 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1594 Argument *A = unwrap<Argument>(Arg);
1595 return (LLVMAttribute)A->getParent()->getAttributes().
1596 Raw(A->getArgNo()+1);
1600 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1601 Argument *A = unwrap<Argument>(Arg);
1603 B.addAlignmentAttr(align);
1604 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1607 /*--.. Operations on basic blocks ..........................................--*/
1609 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1610 return wrap(static_cast<Value*>(unwrap(BB)));
1613 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1614 return isa<BasicBlock>(unwrap(Val));
1617 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1618 return wrap(unwrap<BasicBlock>(Val));
1621 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1622 return wrap(unwrap(BB)->getParent());
1625 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1626 return wrap(unwrap(BB)->getTerminator());
1629 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1630 return unwrap<Function>(FnRef)->size();
1633 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1634 Function *Fn = unwrap<Function>(FnRef);
1635 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1636 *BasicBlocksRefs++ = wrap(I);
1639 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1640 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1643 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1644 Function *Func = unwrap<Function>(Fn);
1645 Function::iterator I = Func->begin();
1646 if (I == Func->end())
1651 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1652 Function *Func = unwrap<Function>(Fn);
1653 Function::iterator I = Func->end();
1654 if (I == Func->begin())
1659 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1660 BasicBlock *Block = unwrap(BB);
1661 Function::iterator I = Block;
1662 if (++I == Block->getParent()->end())
1667 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1668 BasicBlock *Block = unwrap(BB);
1669 Function::iterator I = Block;
1670 if (I == Block->getParent()->begin())
1675 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1678 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1681 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1682 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1685 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1686 LLVMBasicBlockRef BBRef,
1688 BasicBlock *BB = unwrap(BBRef);
1689 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1692 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1694 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1697 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1698 unwrap(BBRef)->eraseFromParent();
1701 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1702 unwrap(BBRef)->removeFromParent();
1705 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1706 unwrap(BB)->moveBefore(unwrap(MovePos));
1709 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1710 unwrap(BB)->moveAfter(unwrap(MovePos));
1713 /*--.. Operations on instructions ..........................................--*/
1715 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1716 return wrap(unwrap<Instruction>(Inst)->getParent());
1719 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1720 BasicBlock *Block = unwrap(BB);
1721 BasicBlock::iterator I = Block->begin();
1722 if (I == Block->end())
1727 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1728 BasicBlock *Block = unwrap(BB);
1729 BasicBlock::iterator I = Block->end();
1730 if (I == Block->begin())
1735 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1736 Instruction *Instr = unwrap<Instruction>(Inst);
1737 BasicBlock::iterator I = Instr;
1738 if (++I == Instr->getParent()->end())
1743 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1744 Instruction *Instr = unwrap<Instruction>(Inst);
1745 BasicBlock::iterator I = Instr;
1746 if (I == Instr->getParent()->begin())
1751 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1752 unwrap<Instruction>(Inst)->eraseFromParent();
1755 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1756 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1757 return (LLVMIntPredicate)I->getPredicate();
1758 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1759 if (CE->getOpcode() == Instruction::ICmp)
1760 return (LLVMIntPredicate)CE->getPredicate();
1761 return (LLVMIntPredicate)0;
1764 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1765 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1766 return map_to_llvmopcode(C->getOpcode());
1767 return (LLVMOpcode)0;
1770 /*--.. Call and invoke instructions ........................................--*/
1772 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1773 Value *V = unwrap(Instr);
1774 if (CallInst *CI = dyn_cast<CallInst>(V))
1775 return CI->getCallingConv();
1776 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1777 return II->getCallingConv();
1778 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1781 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1782 Value *V = unwrap(Instr);
1783 if (CallInst *CI = dyn_cast<CallInst>(V))
1784 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1785 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1786 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1787 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1790 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1792 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1795 Call.getAttributes().addAttributes(Call->getContext(), index,
1796 AttributeSet::get(Call->getContext(),
1800 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1802 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1804 Call.setAttributes(Call.getAttributes()
1805 .removeAttributes(Call->getContext(), index,
1806 AttributeSet::get(Call->getContext(),
1810 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1812 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1814 B.addAlignmentAttr(align);
1815 Call.setAttributes(Call.getAttributes()
1816 .addAttributes(Call->getContext(), index,
1817 AttributeSet::get(Call->getContext(),
1821 /*--.. Operations on call instructions (only) ..............................--*/
1823 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1824 return unwrap<CallInst>(Call)->isTailCall();
1827 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1828 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1831 /*--.. Operations on switch instructions (only) ............................--*/
1833 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1834 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1837 /*--.. Operations on phi nodes .............................................--*/
1839 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1840 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1841 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1842 for (unsigned I = 0; I != Count; ++I)
1843 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1846 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1847 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1850 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1851 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1854 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1855 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1859 /*===-- Instruction builders ----------------------------------------------===*/
1861 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1862 return wrap(new IRBuilder<>(*unwrap(C)));
1865 LLVMBuilderRef LLVMCreateBuilder(void) {
1866 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1869 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1870 LLVMValueRef Instr) {
1871 BasicBlock *BB = unwrap(Block);
1872 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1873 unwrap(Builder)->SetInsertPoint(BB, I);
1876 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1877 Instruction *I = unwrap<Instruction>(Instr);
1878 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1881 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1882 BasicBlock *BB = unwrap(Block);
1883 unwrap(Builder)->SetInsertPoint(BB);
1886 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1887 return wrap(unwrap(Builder)->GetInsertBlock());
1890 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1891 unwrap(Builder)->ClearInsertionPoint();
1894 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1895 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1898 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1900 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1903 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1904 delete unwrap(Builder);
1907 /*--.. Metadata builders ...................................................--*/
1909 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1910 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1911 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1914 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1915 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1916 .getAsMDNode(unwrap(Builder)->getContext()));
1919 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1920 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1924 /*--.. Instruction builders ................................................--*/
1926 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1927 return wrap(unwrap(B)->CreateRetVoid());
1930 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1931 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1934 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1936 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1939 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1940 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1943 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1944 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1945 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1948 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1949 LLVMBasicBlockRef Else, unsigned NumCases) {
1950 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1953 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1954 unsigned NumDests) {
1955 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1958 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1959 LLVMValueRef *Args, unsigned NumArgs,
1960 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1962 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1963 makeArrayRef(unwrap(Args), NumArgs),
1967 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1968 LLVMValueRef PersFn, unsigned NumClauses,
1970 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1971 cast<Function>(unwrap(PersFn)),
1975 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1976 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1979 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1980 return wrap(unwrap(B)->CreateUnreachable());
1983 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1984 LLVMBasicBlockRef Dest) {
1985 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1988 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1989 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1992 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1993 unwrap<LandingPadInst>(LandingPad)->
1994 addClause(cast<Constant>(unwrap(ClauseVal)));
1997 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1998 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2001 /*--.. Arithmetic ..........................................................--*/
2003 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2005 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2008 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2010 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2013 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2015 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2018 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2020 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2023 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2025 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2028 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2030 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2033 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2035 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2038 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2040 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2043 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2045 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2048 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2050 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2053 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2055 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2058 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2060 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2063 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2065 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2068 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2070 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2073 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2074 LLVMValueRef RHS, const char *Name) {
2075 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2078 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2080 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2083 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2085 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2088 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2090 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2093 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2095 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2098 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2100 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2103 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2105 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2108 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2110 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2113 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2115 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2118 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2120 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2123 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2125 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2128 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2129 LLVMValueRef LHS, LLVMValueRef RHS,
2131 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2132 unwrap(RHS), Name));
2135 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2136 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2139 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2141 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2144 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2146 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2149 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2150 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2153 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2154 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2157 /*--.. Memory ..............................................................--*/
2159 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2161 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2162 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2163 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2164 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2165 ITy, unwrap(Ty), AllocSize,
2167 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2170 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2171 LLVMValueRef Val, const char *Name) {
2172 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2173 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2174 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2175 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2176 ITy, unwrap(Ty), AllocSize,
2177 unwrap(Val), 0, "");
2178 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2181 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2183 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2186 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2187 LLVMValueRef Val, const char *Name) {
2188 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2191 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2192 return wrap(unwrap(B)->Insert(
2193 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2197 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2199 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2202 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2203 LLVMValueRef PointerVal) {
2204 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2207 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2208 LLVMValueRef *Indices, unsigned NumIndices,
2210 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2211 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2214 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2215 LLVMValueRef *Indices, unsigned NumIndices,
2217 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2218 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2221 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2222 unsigned Idx, const char *Name) {
2223 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2226 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2228 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2231 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2233 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2236 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2237 Value *P = unwrap<Value>(MemAccessInst);
2238 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2239 return LI->isVolatile();
2240 return cast<StoreInst>(P)->isVolatile();
2243 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2244 Value *P = unwrap<Value>(MemAccessInst);
2245 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2246 return LI->setVolatile(isVolatile);
2247 return cast<StoreInst>(P)->setVolatile(isVolatile);
2250 /*--.. Casts ...............................................................--*/
2252 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2253 LLVMTypeRef DestTy, const char *Name) {
2254 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2257 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2258 LLVMTypeRef DestTy, const char *Name) {
2259 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2262 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2263 LLVMTypeRef DestTy, const char *Name) {
2264 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2267 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2268 LLVMTypeRef DestTy, const char *Name) {
2269 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2272 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2273 LLVMTypeRef DestTy, const char *Name) {
2274 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2277 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2278 LLVMTypeRef DestTy, const char *Name) {
2279 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2282 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2283 LLVMTypeRef DestTy, const char *Name) {
2284 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2287 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2288 LLVMTypeRef DestTy, const char *Name) {
2289 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2292 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2293 LLVMTypeRef DestTy, const char *Name) {
2294 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2297 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2298 LLVMTypeRef DestTy, const char *Name) {
2299 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2302 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2303 LLVMTypeRef DestTy, const char *Name) {
2304 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2307 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2308 LLVMTypeRef DestTy, const char *Name) {
2309 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2312 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2313 LLVMTypeRef DestTy, const char *Name) {
2314 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2318 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2319 LLVMTypeRef DestTy, const char *Name) {
2320 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2324 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2325 LLVMTypeRef DestTy, const char *Name) {
2326 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2330 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2331 LLVMTypeRef DestTy, const char *Name) {
2332 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2333 unwrap(DestTy), Name));
2336 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2337 LLVMTypeRef DestTy, const char *Name) {
2338 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2341 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2342 LLVMTypeRef DestTy, const char *Name) {
2343 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2344 /*isSigned*/true, Name));
2347 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2348 LLVMTypeRef DestTy, const char *Name) {
2349 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2352 /*--.. Comparisons .........................................................--*/
2354 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2355 LLVMValueRef LHS, LLVMValueRef RHS,
2357 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2358 unwrap(LHS), unwrap(RHS), Name));
2361 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2362 LLVMValueRef LHS, LLVMValueRef RHS,
2364 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2365 unwrap(LHS), unwrap(RHS), Name));
2368 /*--.. Miscellaneous instructions ..........................................--*/
2370 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2371 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2374 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2375 LLVMValueRef *Args, unsigned NumArgs,
2377 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2378 makeArrayRef(unwrap(Args), NumArgs),
2382 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2383 LLVMValueRef Then, LLVMValueRef Else,
2385 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2389 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2390 LLVMTypeRef Ty, const char *Name) {
2391 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2394 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2395 LLVMValueRef Index, const char *Name) {
2396 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2400 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2401 LLVMValueRef EltVal, LLVMValueRef Index,
2403 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2404 unwrap(Index), Name));
2407 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2408 LLVMValueRef V2, LLVMValueRef Mask,
2410 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2411 unwrap(Mask), Name));
2414 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2415 unsigned Index, const char *Name) {
2416 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2419 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2420 LLVMValueRef EltVal, unsigned Index,
2422 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2426 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2428 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2431 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2433 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2436 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2437 LLVMValueRef RHS, const char *Name) {
2438 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2441 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2442 LLVMValueRef PTR, LLVMValueRef Val,
2443 LLVMAtomicOrdering ordering,
2444 LLVMBool singleThread) {
2445 AtomicRMWInst::BinOp intop;
2447 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2448 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2449 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2450 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2451 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2452 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2453 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2454 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2455 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2456 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2457 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2459 AtomicOrdering intordering;
2461 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2462 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2463 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2464 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2465 case LLVMAtomicOrderingRelease: intordering = Release; break;
2466 case LLVMAtomicOrderingAcquireRelease:
2467 intordering = AcquireRelease;
2469 case LLVMAtomicOrderingSequentiallyConsistent:
2470 intordering = SequentiallyConsistent;
2473 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2474 intordering, singleThread ? SingleThread : CrossThread));
2478 /*===-- Module providers --------------------------------------------------===*/
2480 LLVMModuleProviderRef
2481 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2482 return reinterpret_cast<LLVMModuleProviderRef>(M);
2485 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2490 /*===-- Memory buffers ----------------------------------------------------===*/
2492 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2494 LLVMMemoryBufferRef *OutMemBuf,
2495 char **OutMessage) {
2497 OwningPtr<MemoryBuffer> MB;
2499 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2500 *OutMemBuf = wrap(MB.take());
2504 *OutMessage = strdup(ec.message().c_str());
2508 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2509 char **OutMessage) {
2510 OwningPtr<MemoryBuffer> MB;
2512 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2513 *OutMemBuf = wrap(MB.take());
2517 *OutMessage = strdup(ec.message().c_str());
2521 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2522 const char *InputData,
2523 size_t InputDataLength,
2524 const char *BufferName,
2525 LLVMBool RequiresNullTerminator) {
2527 return wrap(MemoryBuffer::getMemBuffer(
2528 StringRef(InputData, InputDataLength),
2529 StringRef(BufferName),
2530 RequiresNullTerminator));
2533 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2534 const char *InputData,
2535 size_t InputDataLength,
2536 const char *BufferName) {
2538 return wrap(MemoryBuffer::getMemBufferCopy(
2539 StringRef(InputData, InputDataLength),
2540 StringRef(BufferName)));
2543 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2544 return unwrap(MemBuf)->getBufferStart();
2547 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2548 return unwrap(MemBuf)->getBufferSize();
2551 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2552 delete unwrap(MemBuf);
2555 /*===-- Pass Registry -----------------------------------------------------===*/
2557 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2558 return wrap(PassRegistry::getPassRegistry());
2561 /*===-- Pass Manager ------------------------------------------------------===*/
2563 LLVMPassManagerRef LLVMCreatePassManager() {
2564 return wrap(new PassManager());
2567 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2568 return wrap(new FunctionPassManager(unwrap(M)));
2571 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2572 return LLVMCreateFunctionPassManagerForModule(
2573 reinterpret_cast<LLVMModuleRef>(P));
2576 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2577 return unwrap<PassManager>(PM)->run(*unwrap(M));
2580 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2581 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2584 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2585 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2588 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2589 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2592 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2596 /*===-- Threading ------------------------------------------------------===*/
2598 LLVMBool LLVMStartMultithreaded() {
2599 return llvm_start_multithreaded();
2602 void LLVMStopMultithreaded() {
2603 llvm_stop_multithreaded();
2606 LLVMBool LLVMIsMultithreaded() {
2607 return llvm_is_multithreaded();