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 /*--.. Operations on integer types .........................................--*/
229 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
230 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
232 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
233 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
235 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
236 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
238 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
239 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
241 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
242 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
244 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
245 return wrap(IntegerType::get(*unwrap(C), NumBits));
248 LLVMTypeRef LLVMInt1Type(void) {
249 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
251 LLVMTypeRef LLVMInt8Type(void) {
252 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
254 LLVMTypeRef LLVMInt16Type(void) {
255 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
257 LLVMTypeRef LLVMInt32Type(void) {
258 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
260 LLVMTypeRef LLVMInt64Type(void) {
261 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
263 LLVMTypeRef LLVMIntType(unsigned NumBits) {
264 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
267 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
268 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
271 /*--.. Operations on real types ............................................--*/
273 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
274 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
276 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
277 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
279 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
280 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
282 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
283 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
285 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
286 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
288 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
289 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
291 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
292 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
295 LLVMTypeRef LLVMHalfType(void) {
296 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
298 LLVMTypeRef LLVMFloatType(void) {
299 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
301 LLVMTypeRef LLVMDoubleType(void) {
302 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
304 LLVMTypeRef LLVMX86FP80Type(void) {
305 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
307 LLVMTypeRef LLVMFP128Type(void) {
308 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
310 LLVMTypeRef LLVMPPCFP128Type(void) {
311 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
313 LLVMTypeRef LLVMX86MMXType(void) {
314 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
317 /*--.. Operations on function types ........................................--*/
319 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
320 LLVMTypeRef *ParamTypes, unsigned ParamCount,
322 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
323 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
326 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
327 return unwrap<FunctionType>(FunctionTy)->isVarArg();
330 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
331 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
334 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
335 return unwrap<FunctionType>(FunctionTy)->getNumParams();
338 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
339 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
340 for (FunctionType::param_iterator I = Ty->param_begin(),
341 E = Ty->param_end(); I != E; ++I)
345 /*--.. Operations on struct types ..........................................--*/
347 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
348 unsigned ElementCount, LLVMBool Packed) {
349 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
350 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
353 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
354 unsigned ElementCount, LLVMBool Packed) {
355 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
356 ElementCount, Packed);
359 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
361 return wrap(StructType::create(*unwrap(C), Name));
364 const char *LLVMGetStructName(LLVMTypeRef Ty)
366 StructType *Type = unwrap<StructType>(Ty);
367 if (!Type->hasName())
369 return Type->getName().data();
372 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
373 unsigned ElementCount, LLVMBool Packed) {
374 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
375 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
378 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
379 return unwrap<StructType>(StructTy)->getNumElements();
382 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
383 StructType *Ty = unwrap<StructType>(StructTy);
384 for (StructType::element_iterator I = Ty->element_begin(),
385 E = Ty->element_end(); I != E; ++I)
389 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
390 return unwrap<StructType>(StructTy)->isPacked();
393 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
394 return unwrap<StructType>(StructTy)->isOpaque();
397 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
398 return wrap(unwrap(M)->getTypeByName(Name));
401 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
403 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
404 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
407 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
408 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
411 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
412 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
415 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
416 return wrap(unwrap<SequentialType>(Ty)->getElementType());
419 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
420 return unwrap<ArrayType>(ArrayTy)->getNumElements();
423 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
424 return unwrap<PointerType>(PointerTy)->getAddressSpace();
427 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
428 return unwrap<VectorType>(VectorTy)->getNumElements();
431 /*--.. Operations on other types ...........................................--*/
433 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
434 return wrap(Type::getVoidTy(*unwrap(C)));
436 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
437 return wrap(Type::getLabelTy(*unwrap(C)));
440 LLVMTypeRef LLVMVoidType(void) {
441 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
443 LLVMTypeRef LLVMLabelType(void) {
444 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
447 /*===-- Operations on values ----------------------------------------------===*/
449 /*--.. Operations on all values ............................................--*/
451 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
452 return wrap(unwrap(Val)->getType());
455 const char *LLVMGetValueName(LLVMValueRef Val) {
456 return unwrap(Val)->getName().data();
459 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
460 unwrap(Val)->setName(Name);
463 void LLVMDumpValue(LLVMValueRef Val) {
467 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
468 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
471 int LLVMHasMetadata(LLVMValueRef Inst) {
472 return unwrap<Instruction>(Inst)->hasMetadata();
475 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
476 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
479 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
480 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
483 /*--.. Conversion functions ................................................--*/
485 #define LLVM_DEFINE_VALUE_CAST(name) \
486 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
487 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
490 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
492 /*--.. Operations on Uses ..................................................--*/
493 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
494 Value *V = unwrap(Val);
495 Value::use_iterator I = V->use_begin();
496 if (I == V->use_end())
498 return wrap(&(I.getUse()));
501 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
502 Use *Next = unwrap(U)->getNext();
508 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
509 return wrap(unwrap(U)->getUser());
512 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
513 return wrap(unwrap(U)->get());
516 /*--.. Operations on Users .................................................--*/
517 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
518 Value *V = unwrap(Val);
519 if (MDNode *MD = dyn_cast<MDNode>(V))
520 return wrap(MD->getOperand(Index));
521 return wrap(cast<User>(V)->getOperand(Index));
524 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
525 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
528 int LLVMGetNumOperands(LLVMValueRef Val) {
529 Value *V = unwrap(Val);
530 if (MDNode *MD = dyn_cast<MDNode>(V))
531 return MD->getNumOperands();
532 return cast<User>(V)->getNumOperands();
535 /*--.. Operations on constants of any type .................................--*/
537 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
538 return wrap(Constant::getNullValue(unwrap(Ty)));
541 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
542 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
545 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
546 return wrap(UndefValue::get(unwrap(Ty)));
549 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
550 return isa<Constant>(unwrap(Ty));
553 LLVMBool LLVMIsNull(LLVMValueRef Val) {
554 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
555 return C->isNullValue();
559 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
560 return isa<UndefValue>(unwrap(Val));
563 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
565 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
568 /*--.. Operations on metadata nodes ........................................--*/
570 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
572 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
575 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
576 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
579 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
581 return wrap(MDNode::get(*unwrap(C),
582 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
585 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
586 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
589 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
590 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
591 *Len = S->getString().size();
592 return S->getString().data();
598 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
600 return cast<MDNode>(unwrap(V))->getNumOperands();
603 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
605 const MDNode *N = cast<MDNode>(unwrap(V));
606 const unsigned numOperands = N->getNumOperands();
607 for (unsigned i = 0; i < numOperands; i++)
608 Dest[i] = wrap(N->getOperand(i));
611 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
613 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
614 return N->getNumOperands();
619 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
621 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
624 for (unsigned i=0;i<N->getNumOperands();i++)
625 Dest[i] = wrap(N->getOperand(i));
628 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
631 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
634 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
639 /*--.. Operations on scalar constants ......................................--*/
641 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
642 LLVMBool SignExtend) {
643 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
646 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
648 const uint64_t Words[]) {
649 IntegerType *Ty = unwrap<IntegerType>(IntTy);
650 return wrap(ConstantInt::get(Ty->getContext(),
651 APInt(Ty->getBitWidth(),
652 makeArrayRef(Words, NumWords))));
655 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
657 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
661 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
662 unsigned SLen, uint8_t Radix) {
663 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
667 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
668 return wrap(ConstantFP::get(unwrap(RealTy), N));
671 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
672 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
675 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
677 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
680 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
681 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
684 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
685 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
688 /*--.. Operations on composite constants ...................................--*/
690 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
692 LLVMBool DontNullTerminate) {
693 /* Inverted the sense of AddNull because ', 0)' is a
694 better mnemonic for null termination than ', 1)'. */
695 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
696 DontNullTerminate == 0));
698 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
699 LLVMValueRef *ConstantVals,
700 unsigned Count, LLVMBool Packed) {
701 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
702 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
706 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
707 LLVMBool DontNullTerminate) {
708 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
711 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
712 LLVMValueRef *ConstantVals, unsigned Length) {
713 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
714 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
716 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
718 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
722 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
723 LLVMValueRef *ConstantVals,
725 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
726 StructType *Ty = cast<StructType>(unwrap(StructTy));
728 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
731 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
732 return wrap(ConstantVector::get(makeArrayRef(
733 unwrap<Constant>(ScalarConstantVals, Size), Size)));
736 /*-- Opcode mapping */
738 static LLVMOpcode map_to_llvmopcode(int opcode)
741 default: llvm_unreachable("Unhandled Opcode.");
742 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
743 #include "llvm/IR/Instruction.def"
748 static int map_from_llvmopcode(LLVMOpcode code)
751 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
752 #include "llvm/IR/Instruction.def"
755 llvm_unreachable("Unhandled Opcode.");
758 /*--.. Constant expressions ................................................--*/
760 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
761 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
764 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
765 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
768 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
769 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
772 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
773 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
776 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
777 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
780 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
781 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
785 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
786 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
789 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
790 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
793 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
794 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
795 unwrap<Constant>(RHSConstant)));
798 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
799 LLVMValueRef RHSConstant) {
800 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
801 unwrap<Constant>(RHSConstant)));
804 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
805 LLVMValueRef RHSConstant) {
806 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
807 unwrap<Constant>(RHSConstant)));
810 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
811 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
812 unwrap<Constant>(RHSConstant)));
815 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
816 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
817 unwrap<Constant>(RHSConstant)));
820 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
821 LLVMValueRef RHSConstant) {
822 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
823 unwrap<Constant>(RHSConstant)));
826 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
827 LLVMValueRef RHSConstant) {
828 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
829 unwrap<Constant>(RHSConstant)));
832 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
833 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
834 unwrap<Constant>(RHSConstant)));
837 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
838 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
839 unwrap<Constant>(RHSConstant)));
842 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
843 LLVMValueRef RHSConstant) {
844 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
845 unwrap<Constant>(RHSConstant)));
848 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
849 LLVMValueRef RHSConstant) {
850 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
851 unwrap<Constant>(RHSConstant)));
854 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
855 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
856 unwrap<Constant>(RHSConstant)));
859 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
860 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
861 unwrap<Constant>(RHSConstant)));
864 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
865 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
866 unwrap<Constant>(RHSConstant)));
869 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
870 LLVMValueRef RHSConstant) {
871 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
872 unwrap<Constant>(RHSConstant)));
875 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
876 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
877 unwrap<Constant>(RHSConstant)));
880 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
881 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
882 unwrap<Constant>(RHSConstant)));
885 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
886 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
887 unwrap<Constant>(RHSConstant)));
890 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
891 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
892 unwrap<Constant>(RHSConstant)));
895 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
896 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
897 unwrap<Constant>(RHSConstant)));
900 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
901 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
902 unwrap<Constant>(RHSConstant)));
905 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
906 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
907 unwrap<Constant>(RHSConstant)));
910 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
911 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
912 return wrap(ConstantExpr::getICmp(Predicate,
913 unwrap<Constant>(LHSConstant),
914 unwrap<Constant>(RHSConstant)));
917 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
918 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
919 return wrap(ConstantExpr::getFCmp(Predicate,
920 unwrap<Constant>(LHSConstant),
921 unwrap<Constant>(RHSConstant)));
924 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
925 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
926 unwrap<Constant>(RHSConstant)));
929 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
930 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
931 unwrap<Constant>(RHSConstant)));
934 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
935 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
936 unwrap<Constant>(RHSConstant)));
939 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
940 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
941 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
943 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
947 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
948 LLVMValueRef *ConstantIndices,
949 unsigned NumIndices) {
950 Constant* Val = unwrap<Constant>(ConstantVal);
951 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
953 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
956 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
957 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
961 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
962 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
966 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
967 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
971 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
972 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
976 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
977 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
981 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
982 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
986 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
987 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
991 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
992 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
996 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
997 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1001 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1002 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1006 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1007 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1011 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1012 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1016 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1017 LLVMTypeRef ToType) {
1018 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1022 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1023 LLVMTypeRef ToType) {
1024 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1028 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1029 LLVMTypeRef ToType) {
1030 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1034 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1035 LLVMTypeRef ToType) {
1036 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1040 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1041 LLVMBool isSigned) {
1042 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1043 unwrap(ToType), isSigned));
1046 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1047 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1051 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1052 LLVMValueRef ConstantIfTrue,
1053 LLVMValueRef ConstantIfFalse) {
1054 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1055 unwrap<Constant>(ConstantIfTrue),
1056 unwrap<Constant>(ConstantIfFalse)));
1059 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1060 LLVMValueRef IndexConstant) {
1061 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1062 unwrap<Constant>(IndexConstant)));
1065 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1066 LLVMValueRef ElementValueConstant,
1067 LLVMValueRef IndexConstant) {
1068 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1069 unwrap<Constant>(ElementValueConstant),
1070 unwrap<Constant>(IndexConstant)));
1073 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1074 LLVMValueRef VectorBConstant,
1075 LLVMValueRef MaskConstant) {
1076 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1077 unwrap<Constant>(VectorBConstant),
1078 unwrap<Constant>(MaskConstant)));
1081 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1083 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1084 makeArrayRef(IdxList, NumIdx)));
1087 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1088 LLVMValueRef ElementValueConstant,
1089 unsigned *IdxList, unsigned NumIdx) {
1090 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1091 unwrap<Constant>(ElementValueConstant),
1092 makeArrayRef(IdxList, NumIdx)));
1095 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1096 const char *Constraints,
1097 LLVMBool HasSideEffects,
1098 LLVMBool IsAlignStack) {
1099 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1100 Constraints, HasSideEffects, IsAlignStack));
1103 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1104 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1107 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1109 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1110 return wrap(unwrap<GlobalValue>(Global)->getParent());
1113 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1114 return unwrap<GlobalValue>(Global)->isDeclaration();
1117 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1118 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1119 case GlobalValue::ExternalLinkage:
1120 return LLVMExternalLinkage;
1121 case GlobalValue::AvailableExternallyLinkage:
1122 return LLVMAvailableExternallyLinkage;
1123 case GlobalValue::LinkOnceAnyLinkage:
1124 return LLVMLinkOnceAnyLinkage;
1125 case GlobalValue::LinkOnceODRLinkage:
1126 return LLVMLinkOnceODRLinkage;
1127 case GlobalValue::LinkOnceODRAutoHideLinkage:
1128 return LLVMLinkOnceODRAutoHideLinkage;
1129 case GlobalValue::WeakAnyLinkage:
1130 return LLVMWeakAnyLinkage;
1131 case GlobalValue::WeakODRLinkage:
1132 return LLVMWeakODRLinkage;
1133 case GlobalValue::AppendingLinkage:
1134 return LLVMAppendingLinkage;
1135 case GlobalValue::InternalLinkage:
1136 return LLVMInternalLinkage;
1137 case GlobalValue::PrivateLinkage:
1138 return LLVMPrivateLinkage;
1139 case GlobalValue::LinkerPrivateLinkage:
1140 return LLVMLinkerPrivateLinkage;
1141 case GlobalValue::LinkerPrivateWeakLinkage:
1142 return LLVMLinkerPrivateWeakLinkage;
1143 case GlobalValue::DLLImportLinkage:
1144 return LLVMDLLImportLinkage;
1145 case GlobalValue::DLLExportLinkage:
1146 return LLVMDLLExportLinkage;
1147 case GlobalValue::ExternalWeakLinkage:
1148 return LLVMExternalWeakLinkage;
1149 case GlobalValue::CommonLinkage:
1150 return LLVMCommonLinkage;
1153 llvm_unreachable("Invalid GlobalValue linkage!");
1156 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1157 GlobalValue *GV = unwrap<GlobalValue>(Global);
1160 case LLVMExternalLinkage:
1161 GV->setLinkage(GlobalValue::ExternalLinkage);
1163 case LLVMAvailableExternallyLinkage:
1164 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1166 case LLVMLinkOnceAnyLinkage:
1167 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1169 case LLVMLinkOnceODRLinkage:
1170 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1172 case LLVMLinkOnceODRAutoHideLinkage:
1173 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1175 case LLVMWeakAnyLinkage:
1176 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1178 case LLVMWeakODRLinkage:
1179 GV->setLinkage(GlobalValue::WeakODRLinkage);
1181 case LLVMAppendingLinkage:
1182 GV->setLinkage(GlobalValue::AppendingLinkage);
1184 case LLVMInternalLinkage:
1185 GV->setLinkage(GlobalValue::InternalLinkage);
1187 case LLVMPrivateLinkage:
1188 GV->setLinkage(GlobalValue::PrivateLinkage);
1190 case LLVMLinkerPrivateLinkage:
1191 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1193 case LLVMLinkerPrivateWeakLinkage:
1194 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1196 case LLVMDLLImportLinkage:
1197 GV->setLinkage(GlobalValue::DLLImportLinkage);
1199 case LLVMDLLExportLinkage:
1200 GV->setLinkage(GlobalValue::DLLExportLinkage);
1202 case LLVMExternalWeakLinkage:
1203 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1205 case LLVMGhostLinkage:
1207 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1209 case LLVMCommonLinkage:
1210 GV->setLinkage(GlobalValue::CommonLinkage);
1215 const char *LLVMGetSection(LLVMValueRef Global) {
1216 return unwrap<GlobalValue>(Global)->getSection().c_str();
1219 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1220 unwrap<GlobalValue>(Global)->setSection(Section);
1223 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1224 return static_cast<LLVMVisibility>(
1225 unwrap<GlobalValue>(Global)->getVisibility());
1228 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1229 unwrap<GlobalValue>(Global)
1230 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1233 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1234 return unwrap<GlobalValue>(Global)->getAlignment();
1237 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1238 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1241 /*--.. Operations on global variables ......................................--*/
1243 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1244 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1245 GlobalValue::ExternalLinkage, 0, Name));
1248 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1250 unsigned AddressSpace) {
1251 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1252 GlobalValue::ExternalLinkage, 0, Name, 0,
1253 GlobalVariable::NotThreadLocal, AddressSpace));
1256 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1257 return wrap(unwrap(M)->getNamedGlobal(Name));
1260 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1261 Module *Mod = unwrap(M);
1262 Module::global_iterator I = Mod->global_begin();
1263 if (I == Mod->global_end())
1268 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1269 Module *Mod = unwrap(M);
1270 Module::global_iterator I = Mod->global_end();
1271 if (I == Mod->global_begin())
1276 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1277 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1278 Module::global_iterator I = GV;
1279 if (++I == GV->getParent()->global_end())
1284 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1285 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1286 Module::global_iterator I = GV;
1287 if (I == GV->getParent()->global_begin())
1292 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1293 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1296 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1297 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1298 if ( !GV->hasInitializer() )
1300 return wrap(GV->getInitializer());
1303 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1304 unwrap<GlobalVariable>(GlobalVar)
1305 ->setInitializer(unwrap<Constant>(ConstantVal));
1308 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1309 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1312 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1313 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1316 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1317 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1320 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1321 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1324 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1325 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1326 case GlobalVariable::NotThreadLocal:
1327 return LLVMNotThreadLocal;
1328 case GlobalVariable::GeneralDynamicTLSModel:
1329 return LLVMGeneralDynamicTLSModel;
1330 case GlobalVariable::LocalDynamicTLSModel:
1331 return LLVMLocalDynamicTLSModel;
1332 case GlobalVariable::InitialExecTLSModel:
1333 return LLVMInitialExecTLSModel;
1334 case GlobalVariable::LocalExecTLSModel:
1335 return LLVMLocalExecTLSModel;
1338 llvm_unreachable("Invalid GlobalVariable thread local mode");
1341 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1342 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1345 case LLVMNotThreadLocal:
1346 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1348 case LLVMGeneralDynamicTLSModel:
1349 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1351 case LLVMLocalDynamicTLSModel:
1352 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1354 case LLVMInitialExecTLSModel:
1355 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1357 case LLVMLocalExecTLSModel:
1358 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1363 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1364 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1367 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1368 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1371 /*--.. Operations on aliases ......................................--*/
1373 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1375 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1376 unwrap<Constant>(Aliasee), unwrap (M)));
1379 /*--.. Operations on functions .............................................--*/
1381 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1382 LLVMTypeRef FunctionTy) {
1383 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1384 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1387 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1388 return wrap(unwrap(M)->getFunction(Name));
1391 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1392 Module *Mod = unwrap(M);
1393 Module::iterator I = Mod->begin();
1394 if (I == Mod->end())
1399 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1400 Module *Mod = unwrap(M);
1401 Module::iterator I = Mod->end();
1402 if (I == Mod->begin())
1407 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1408 Function *Func = unwrap<Function>(Fn);
1409 Module::iterator I = Func;
1410 if (++I == Func->getParent()->end())
1415 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1416 Function *Func = unwrap<Function>(Fn);
1417 Module::iterator I = Func;
1418 if (I == Func->getParent()->begin())
1423 void LLVMDeleteFunction(LLVMValueRef Fn) {
1424 unwrap<Function>(Fn)->eraseFromParent();
1427 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1428 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1429 return F->getIntrinsicID();
1433 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1434 return unwrap<Function>(Fn)->getCallingConv();
1437 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1438 return unwrap<Function>(Fn)->setCallingConv(
1439 static_cast<CallingConv::ID>(CC));
1442 const char *LLVMGetGC(LLVMValueRef Fn) {
1443 Function *F = unwrap<Function>(Fn);
1444 return F->hasGC()? F->getGC() : 0;
1447 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1448 Function *F = unwrap<Function>(Fn);
1455 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1456 Function *Func = unwrap<Function>(Fn);
1457 const AttributeSet PAL = Func->getAttributes();
1459 const AttributeSet PALnew =
1460 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1461 AttributeSet::get(Func->getContext(),
1462 AttributeSet::FunctionIndex, B));
1463 Func->setAttributes(PALnew);
1466 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1468 Function *Func = unwrap<Function>(Fn);
1469 AttributeSet::AttrIndex Idx =
1470 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1473 B.addAttribute(A, V);
1474 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1475 Func->addAttributes(Idx, Set);
1478 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1479 Function *Func = unwrap<Function>(Fn);
1480 const AttributeSet PAL = Func->getAttributes();
1482 const AttributeSet PALnew =
1483 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1484 AttributeSet::get(Func->getContext(),
1485 AttributeSet::FunctionIndex, B));
1486 Func->setAttributes(PALnew);
1489 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1490 Function *Func = unwrap<Function>(Fn);
1491 const AttributeSet PAL = Func->getAttributes();
1492 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1495 /*--.. Operations on parameters ............................................--*/
1497 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1498 // This function is strictly redundant to
1499 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1500 return unwrap<Function>(FnRef)->arg_size();
1503 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1504 Function *Fn = unwrap<Function>(FnRef);
1505 for (Function::arg_iterator I = Fn->arg_begin(),
1506 E = Fn->arg_end(); I != E; I++)
1507 *ParamRefs++ = wrap(I);
1510 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1511 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1517 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1518 return wrap(unwrap<Argument>(V)->getParent());
1521 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1522 Function *Func = unwrap<Function>(Fn);
1523 Function::arg_iterator I = Func->arg_begin();
1524 if (I == Func->arg_end())
1529 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1530 Function *Func = unwrap<Function>(Fn);
1531 Function::arg_iterator I = Func->arg_end();
1532 if (I == Func->arg_begin())
1537 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1538 Argument *A = unwrap<Argument>(Arg);
1539 Function::arg_iterator I = A;
1540 if (++I == A->getParent()->arg_end())
1545 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1546 Argument *A = unwrap<Argument>(Arg);
1547 Function::arg_iterator I = A;
1548 if (I == A->getParent()->arg_begin())
1553 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1554 Argument *A = unwrap<Argument>(Arg);
1556 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1559 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1560 Argument *A = unwrap<Argument>(Arg);
1562 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1565 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1566 Argument *A = unwrap<Argument>(Arg);
1567 return (LLVMAttribute)A->getParent()->getAttributes().
1568 Raw(A->getArgNo()+1);
1572 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1573 Argument *A = unwrap<Argument>(Arg);
1575 B.addAlignmentAttr(align);
1576 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1579 /*--.. Operations on basic blocks ..........................................--*/
1581 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1582 return wrap(static_cast<Value*>(unwrap(BB)));
1585 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1586 return isa<BasicBlock>(unwrap(Val));
1589 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1590 return wrap(unwrap<BasicBlock>(Val));
1593 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1594 return wrap(unwrap(BB)->getParent());
1597 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1598 return wrap(unwrap(BB)->getTerminator());
1601 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1602 return unwrap<Function>(FnRef)->size();
1605 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1606 Function *Fn = unwrap<Function>(FnRef);
1607 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1608 *BasicBlocksRefs++ = wrap(I);
1611 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1612 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1615 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1616 Function *Func = unwrap<Function>(Fn);
1617 Function::iterator I = Func->begin();
1618 if (I == Func->end())
1623 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1624 Function *Func = unwrap<Function>(Fn);
1625 Function::iterator I = Func->end();
1626 if (I == Func->begin())
1631 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1632 BasicBlock *Block = unwrap(BB);
1633 Function::iterator I = Block;
1634 if (++I == Block->getParent()->end())
1639 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1640 BasicBlock *Block = unwrap(BB);
1641 Function::iterator I = Block;
1642 if (I == Block->getParent()->begin())
1647 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1650 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1653 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1654 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1657 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1658 LLVMBasicBlockRef BBRef,
1660 BasicBlock *BB = unwrap(BBRef);
1661 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1664 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1666 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1669 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1670 unwrap(BBRef)->eraseFromParent();
1673 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1674 unwrap(BBRef)->removeFromParent();
1677 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1678 unwrap(BB)->moveBefore(unwrap(MovePos));
1681 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1682 unwrap(BB)->moveAfter(unwrap(MovePos));
1685 /*--.. Operations on instructions ..........................................--*/
1687 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1688 return wrap(unwrap<Instruction>(Inst)->getParent());
1691 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1692 BasicBlock *Block = unwrap(BB);
1693 BasicBlock::iterator I = Block->begin();
1694 if (I == Block->end())
1699 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1700 BasicBlock *Block = unwrap(BB);
1701 BasicBlock::iterator I = Block->end();
1702 if (I == Block->begin())
1707 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1708 Instruction *Instr = unwrap<Instruction>(Inst);
1709 BasicBlock::iterator I = Instr;
1710 if (++I == Instr->getParent()->end())
1715 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1716 Instruction *Instr = unwrap<Instruction>(Inst);
1717 BasicBlock::iterator I = Instr;
1718 if (I == Instr->getParent()->begin())
1723 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1724 unwrap<Instruction>(Inst)->eraseFromParent();
1727 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1728 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1729 return (LLVMIntPredicate)I->getPredicate();
1730 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1731 if (CE->getOpcode() == Instruction::ICmp)
1732 return (LLVMIntPredicate)CE->getPredicate();
1733 return (LLVMIntPredicate)0;
1736 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1737 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1738 return map_to_llvmopcode(C->getOpcode());
1739 return (LLVMOpcode)0;
1742 /*--.. Call and invoke instructions ........................................--*/
1744 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1745 Value *V = unwrap(Instr);
1746 if (CallInst *CI = dyn_cast<CallInst>(V))
1747 return CI->getCallingConv();
1748 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1749 return II->getCallingConv();
1750 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1753 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1754 Value *V = unwrap(Instr);
1755 if (CallInst *CI = dyn_cast<CallInst>(V))
1756 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1757 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1758 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1759 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1762 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1764 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1767 Call.getAttributes().addAttributes(Call->getContext(), index,
1768 AttributeSet::get(Call->getContext(),
1772 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1774 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1776 Call.setAttributes(Call.getAttributes()
1777 .removeAttributes(Call->getContext(), index,
1778 AttributeSet::get(Call->getContext(),
1782 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1784 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1786 B.addAlignmentAttr(align);
1787 Call.setAttributes(Call.getAttributes()
1788 .addAttributes(Call->getContext(), index,
1789 AttributeSet::get(Call->getContext(),
1793 /*--.. Operations on call instructions (only) ..............................--*/
1795 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1796 return unwrap<CallInst>(Call)->isTailCall();
1799 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1800 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1803 /*--.. Operations on switch instructions (only) ............................--*/
1805 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1806 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1809 /*--.. Operations on phi nodes .............................................--*/
1811 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1812 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1813 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1814 for (unsigned I = 0; I != Count; ++I)
1815 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1818 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1819 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1822 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1823 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1826 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1827 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1831 /*===-- Instruction builders ----------------------------------------------===*/
1833 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1834 return wrap(new IRBuilder<>(*unwrap(C)));
1837 LLVMBuilderRef LLVMCreateBuilder(void) {
1838 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1841 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1842 LLVMValueRef Instr) {
1843 BasicBlock *BB = unwrap(Block);
1844 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1845 unwrap(Builder)->SetInsertPoint(BB, I);
1848 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1849 Instruction *I = unwrap<Instruction>(Instr);
1850 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1853 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1854 BasicBlock *BB = unwrap(Block);
1855 unwrap(Builder)->SetInsertPoint(BB);
1858 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1859 return wrap(unwrap(Builder)->GetInsertBlock());
1862 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1863 unwrap(Builder)->ClearInsertionPoint();
1866 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1867 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1870 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1872 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1875 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1876 delete unwrap(Builder);
1879 /*--.. Metadata builders ...................................................--*/
1881 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1882 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1883 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1886 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1887 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1888 .getAsMDNode(unwrap(Builder)->getContext()));
1891 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1892 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1896 /*--.. Instruction builders ................................................--*/
1898 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1899 return wrap(unwrap(B)->CreateRetVoid());
1902 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1903 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1906 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1908 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1911 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1912 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1915 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1916 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1917 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1920 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1921 LLVMBasicBlockRef Else, unsigned NumCases) {
1922 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1925 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1926 unsigned NumDests) {
1927 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1930 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1931 LLVMValueRef *Args, unsigned NumArgs,
1932 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1934 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1935 makeArrayRef(unwrap(Args), NumArgs),
1939 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1940 LLVMValueRef PersFn, unsigned NumClauses,
1942 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1943 cast<Function>(unwrap(PersFn)),
1947 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1948 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1951 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1952 return wrap(unwrap(B)->CreateUnreachable());
1955 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1956 LLVMBasicBlockRef Dest) {
1957 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1960 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1961 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1964 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1965 unwrap<LandingPadInst>(LandingPad)->
1966 addClause(cast<Constant>(unwrap(ClauseVal)));
1969 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1970 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1973 /*--.. Arithmetic ..........................................................--*/
1975 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1977 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1980 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1982 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1985 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1987 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1990 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1992 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1995 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1997 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2000 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2002 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2005 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2007 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2010 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2012 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2015 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2017 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2020 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2022 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2025 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2027 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2030 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2032 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2035 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2037 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2040 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2042 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2045 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2046 LLVMValueRef RHS, const char *Name) {
2047 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2050 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2052 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2055 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2057 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2060 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2062 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2065 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2067 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2070 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2072 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2075 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2077 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2080 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2082 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2085 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2087 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2090 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2092 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2095 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2097 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2100 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2101 LLVMValueRef LHS, LLVMValueRef RHS,
2103 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2104 unwrap(RHS), Name));
2107 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2108 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2111 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2113 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2116 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2118 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2121 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2122 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2125 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2126 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2129 /*--.. Memory ..............................................................--*/
2131 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2133 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2134 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2135 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2136 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2137 ITy, unwrap(Ty), AllocSize,
2139 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2142 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2143 LLVMValueRef Val, const char *Name) {
2144 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2145 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2146 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2147 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2148 ITy, unwrap(Ty), AllocSize,
2149 unwrap(Val), 0, "");
2150 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2153 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2155 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2158 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2159 LLVMValueRef Val, const char *Name) {
2160 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2163 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2164 return wrap(unwrap(B)->Insert(
2165 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2169 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2171 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2174 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2175 LLVMValueRef PointerVal) {
2176 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2179 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2180 LLVMValueRef *Indices, unsigned NumIndices,
2182 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2183 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2186 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2187 LLVMValueRef *Indices, unsigned NumIndices,
2189 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2190 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2193 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2194 unsigned Idx, const char *Name) {
2195 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2198 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2200 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2203 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2205 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2208 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2209 Value *P = unwrap<Value>(MemAccessInst);
2210 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2211 return LI->isVolatile();
2212 return cast<StoreInst>(P)->isVolatile();
2215 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2216 Value *P = unwrap<Value>(MemAccessInst);
2217 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2218 return LI->setVolatile(isVolatile);
2219 return cast<StoreInst>(P)->setVolatile(isVolatile);
2222 /*--.. Casts ...............................................................--*/
2224 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2225 LLVMTypeRef DestTy, const char *Name) {
2226 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2229 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2230 LLVMTypeRef DestTy, const char *Name) {
2231 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2234 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2235 LLVMTypeRef DestTy, const char *Name) {
2236 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2239 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2240 LLVMTypeRef DestTy, const char *Name) {
2241 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2244 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2245 LLVMTypeRef DestTy, const char *Name) {
2246 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2249 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2250 LLVMTypeRef DestTy, const char *Name) {
2251 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2254 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2255 LLVMTypeRef DestTy, const char *Name) {
2256 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2259 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2260 LLVMTypeRef DestTy, const char *Name) {
2261 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2264 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2265 LLVMTypeRef DestTy, const char *Name) {
2266 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2269 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2270 LLVMTypeRef DestTy, const char *Name) {
2271 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2274 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2275 LLVMTypeRef DestTy, const char *Name) {
2276 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2279 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2280 LLVMTypeRef DestTy, const char *Name) {
2281 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2284 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2285 LLVMTypeRef DestTy, const char *Name) {
2286 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2290 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2291 LLVMTypeRef DestTy, const char *Name) {
2292 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2296 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2297 LLVMTypeRef DestTy, const char *Name) {
2298 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2302 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2303 LLVMTypeRef DestTy, const char *Name) {
2304 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2305 unwrap(DestTy), Name));
2308 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2309 LLVMTypeRef DestTy, const char *Name) {
2310 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2313 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2314 LLVMTypeRef DestTy, const char *Name) {
2315 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2316 /*isSigned*/true, Name));
2319 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2320 LLVMTypeRef DestTy, const char *Name) {
2321 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2324 /*--.. Comparisons .........................................................--*/
2326 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2327 LLVMValueRef LHS, LLVMValueRef RHS,
2329 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2330 unwrap(LHS), unwrap(RHS), Name));
2333 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2334 LLVMValueRef LHS, LLVMValueRef RHS,
2336 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2337 unwrap(LHS), unwrap(RHS), Name));
2340 /*--.. Miscellaneous instructions ..........................................--*/
2342 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2343 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2346 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2347 LLVMValueRef *Args, unsigned NumArgs,
2349 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2350 makeArrayRef(unwrap(Args), NumArgs),
2354 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2355 LLVMValueRef Then, LLVMValueRef Else,
2357 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2361 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2362 LLVMTypeRef Ty, const char *Name) {
2363 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2366 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2367 LLVMValueRef Index, const char *Name) {
2368 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2372 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2373 LLVMValueRef EltVal, LLVMValueRef Index,
2375 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2376 unwrap(Index), Name));
2379 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2380 LLVMValueRef V2, LLVMValueRef Mask,
2382 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2383 unwrap(Mask), Name));
2386 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2387 unsigned Index, const char *Name) {
2388 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2391 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2392 LLVMValueRef EltVal, unsigned Index,
2394 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2398 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2400 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2403 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2405 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2408 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2409 LLVMValueRef RHS, const char *Name) {
2410 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2413 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2414 LLVMValueRef PTR, LLVMValueRef Val,
2415 LLVMAtomicOrdering ordering,
2416 LLVMBool singleThread) {
2417 AtomicRMWInst::BinOp intop;
2419 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2420 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2421 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2422 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2423 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2424 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2425 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2426 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2427 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2428 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2429 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2431 AtomicOrdering intordering;
2433 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2434 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2435 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2436 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2437 case LLVMAtomicOrderingRelease: intordering = Release; break;
2438 case LLVMAtomicOrderingAcquireRelease:
2439 intordering = AcquireRelease;
2441 case LLVMAtomicOrderingSequentiallyConsistent:
2442 intordering = SequentiallyConsistent;
2445 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2446 intordering, singleThread ? SingleThread : CrossThread));
2450 /*===-- Module providers --------------------------------------------------===*/
2452 LLVMModuleProviderRef
2453 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2454 return reinterpret_cast<LLVMModuleProviderRef>(M);
2457 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2462 /*===-- Memory buffers ----------------------------------------------------===*/
2464 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2466 LLVMMemoryBufferRef *OutMemBuf,
2467 char **OutMessage) {
2469 OwningPtr<MemoryBuffer> MB;
2471 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2472 *OutMemBuf = wrap(MB.take());
2476 *OutMessage = strdup(ec.message().c_str());
2480 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2481 char **OutMessage) {
2482 OwningPtr<MemoryBuffer> MB;
2484 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2485 *OutMemBuf = wrap(MB.take());
2489 *OutMessage = strdup(ec.message().c_str());
2493 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2494 const char *InputData,
2495 size_t InputDataLength,
2496 const char *BufferName,
2497 LLVMBool RequiresNullTerminator) {
2499 return wrap(MemoryBuffer::getMemBuffer(
2500 StringRef(InputData, InputDataLength),
2501 StringRef(BufferName),
2502 RequiresNullTerminator));
2505 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2506 const char *InputData,
2507 size_t InputDataLength,
2508 const char *BufferName) {
2510 return wrap(MemoryBuffer::getMemBufferCopy(
2511 StringRef(InputData, InputDataLength),
2512 StringRef(BufferName)));
2515 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2516 return unwrap(MemBuf)->getBufferStart();
2519 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2520 return unwrap(MemBuf)->getBufferSize();
2523 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2524 delete unwrap(MemBuf);
2527 /*===-- Pass Registry -----------------------------------------------------===*/
2529 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2530 return wrap(PassRegistry::getPassRegistry());
2533 /*===-- Pass Manager ------------------------------------------------------===*/
2535 LLVMPassManagerRef LLVMCreatePassManager() {
2536 return wrap(new PassManager());
2539 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2540 return wrap(new FunctionPassManager(unwrap(M)));
2543 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2544 return LLVMCreateFunctionPassManagerForModule(
2545 reinterpret_cast<LLVMModuleRef>(P));
2548 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2549 return unwrap<PassManager>(PM)->run(*unwrap(M));
2552 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2553 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2556 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2557 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2560 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2561 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2564 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2568 /*===-- Threading ------------------------------------------------------===*/
2570 LLVMBool LLVMStartMultithreaded() {
2571 return llvm_start_multithreaded();
2574 void LLVMStopMultithreaded() {
2575 llvm_stop_multithreaded();
2578 LLVMBool LLVMIsMultithreaded() {
2579 return llvm_is_multithreaded();