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 /*--.. Operations on integer types .........................................--*/
225 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
226 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
228 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
229 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
231 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
232 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
234 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
235 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
237 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
238 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
240 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
241 return wrap(IntegerType::get(*unwrap(C), NumBits));
244 LLVMTypeRef LLVMInt1Type(void) {
245 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
247 LLVMTypeRef LLVMInt8Type(void) {
248 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
250 LLVMTypeRef LLVMInt16Type(void) {
251 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
253 LLVMTypeRef LLVMInt32Type(void) {
254 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
256 LLVMTypeRef LLVMInt64Type(void) {
257 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
259 LLVMTypeRef LLVMIntType(unsigned NumBits) {
260 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
263 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
264 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
267 /*--.. Operations on real types ............................................--*/
269 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
270 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
272 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
273 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
275 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
276 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
278 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
279 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
281 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
282 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
284 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
285 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
287 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
288 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
291 LLVMTypeRef LLVMHalfType(void) {
292 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
294 LLVMTypeRef LLVMFloatType(void) {
295 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
297 LLVMTypeRef LLVMDoubleType(void) {
298 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
300 LLVMTypeRef LLVMX86FP80Type(void) {
301 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
303 LLVMTypeRef LLVMFP128Type(void) {
304 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
306 LLVMTypeRef LLVMPPCFP128Type(void) {
307 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
309 LLVMTypeRef LLVMX86MMXType(void) {
310 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
313 /*--.. Operations on function types ........................................--*/
315 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
316 LLVMTypeRef *ParamTypes, unsigned ParamCount,
318 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
319 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
322 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
323 return unwrap<FunctionType>(FunctionTy)->isVarArg();
326 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
327 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
330 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
331 return unwrap<FunctionType>(FunctionTy)->getNumParams();
334 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
335 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
336 for (FunctionType::param_iterator I = Ty->param_begin(),
337 E = Ty->param_end(); I != E; ++I)
341 /*--.. Operations on struct types ..........................................--*/
343 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
344 unsigned ElementCount, LLVMBool Packed) {
345 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
346 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
349 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
350 unsigned ElementCount, LLVMBool Packed) {
351 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
352 ElementCount, Packed);
355 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
357 return wrap(StructType::create(*unwrap(C), Name));
360 const char *LLVMGetStructName(LLVMTypeRef Ty)
362 StructType *Type = unwrap<StructType>(Ty);
363 if (!Type->hasName())
365 return Type->getName().data();
368 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
369 unsigned ElementCount, LLVMBool Packed) {
370 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
371 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
374 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
375 return unwrap<StructType>(StructTy)->getNumElements();
378 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
379 StructType *Ty = unwrap<StructType>(StructTy);
380 for (StructType::element_iterator I = Ty->element_begin(),
381 E = Ty->element_end(); I != E; ++I)
385 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
386 return unwrap<StructType>(StructTy)->isPacked();
389 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
390 return unwrap<StructType>(StructTy)->isOpaque();
393 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
394 return wrap(unwrap(M)->getTypeByName(Name));
397 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
399 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
400 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
403 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
404 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
407 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
408 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
411 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
412 return wrap(unwrap<SequentialType>(Ty)->getElementType());
415 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
416 return unwrap<ArrayType>(ArrayTy)->getNumElements();
419 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
420 return unwrap<PointerType>(PointerTy)->getAddressSpace();
423 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
424 return unwrap<VectorType>(VectorTy)->getNumElements();
427 /*--.. Operations on other types ...........................................--*/
429 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
430 return wrap(Type::getVoidTy(*unwrap(C)));
432 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
433 return wrap(Type::getLabelTy(*unwrap(C)));
436 LLVMTypeRef LLVMVoidType(void) {
437 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
439 LLVMTypeRef LLVMLabelType(void) {
440 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
443 /*===-- Operations on values ----------------------------------------------===*/
445 /*--.. Operations on all values ............................................--*/
447 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
448 return wrap(unwrap(Val)->getType());
451 const char *LLVMGetValueName(LLVMValueRef Val) {
452 return unwrap(Val)->getName().data();
455 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
456 unwrap(Val)->setName(Name);
459 void LLVMDumpValue(LLVMValueRef Val) {
463 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
464 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
467 int LLVMHasMetadata(LLVMValueRef Inst) {
468 return unwrap<Instruction>(Inst)->hasMetadata();
471 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
472 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
475 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
476 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
479 /*--.. Conversion functions ................................................--*/
481 #define LLVM_DEFINE_VALUE_CAST(name) \
482 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
483 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
486 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
488 /*--.. Operations on Uses ..................................................--*/
489 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
490 Value *V = unwrap(Val);
491 Value::use_iterator I = V->use_begin();
492 if (I == V->use_end())
494 return wrap(&(I.getUse()));
497 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
498 Use *Next = unwrap(U)->getNext();
504 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
505 return wrap(unwrap(U)->getUser());
508 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
509 return wrap(unwrap(U)->get());
512 /*--.. Operations on Users .................................................--*/
513 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
514 Value *V = unwrap(Val);
515 if (MDNode *MD = dyn_cast<MDNode>(V))
516 return wrap(MD->getOperand(Index));
517 return wrap(cast<User>(V)->getOperand(Index));
520 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
521 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
524 int LLVMGetNumOperands(LLVMValueRef Val) {
525 Value *V = unwrap(Val);
526 if (MDNode *MD = dyn_cast<MDNode>(V))
527 return MD->getNumOperands();
528 return cast<User>(V)->getNumOperands();
531 /*--.. Operations on constants of any type .................................--*/
533 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
534 return wrap(Constant::getNullValue(unwrap(Ty)));
537 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
538 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
541 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
542 return wrap(UndefValue::get(unwrap(Ty)));
545 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
546 return isa<Constant>(unwrap(Ty));
549 LLVMBool LLVMIsNull(LLVMValueRef Val) {
550 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
551 return C->isNullValue();
555 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
556 return isa<UndefValue>(unwrap(Val));
559 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
561 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
564 /*--.. Operations on metadata nodes ........................................--*/
566 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
568 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
571 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
572 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
575 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
577 return wrap(MDNode::get(*unwrap(C),
578 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
581 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
582 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
585 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
586 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
587 *Len = S->getString().size();
588 return S->getString().data();
594 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
596 return cast<MDNode>(unwrap(V))->getNumOperands();
599 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
601 const MDNode *N = cast<MDNode>(unwrap(V));
602 const unsigned numOperands = N->getNumOperands();
603 for (unsigned i = 0; i < numOperands; i++)
604 Dest[i] = wrap(N->getOperand(i));
607 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
609 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
610 return N->getNumOperands();
615 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
617 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
620 for (unsigned i=0;i<N->getNumOperands();i++)
621 Dest[i] = wrap(N->getOperand(i));
624 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
627 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
630 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
635 /*--.. Operations on scalar constants ......................................--*/
637 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
638 LLVMBool SignExtend) {
639 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
642 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
644 const uint64_t Words[]) {
645 IntegerType *Ty = unwrap<IntegerType>(IntTy);
646 return wrap(ConstantInt::get(Ty->getContext(),
647 APInt(Ty->getBitWidth(),
648 makeArrayRef(Words, NumWords))));
651 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
653 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
657 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
658 unsigned SLen, uint8_t Radix) {
659 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
663 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
664 return wrap(ConstantFP::get(unwrap(RealTy), N));
667 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
668 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
671 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
673 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
676 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
677 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
680 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
681 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
684 /*--.. Operations on composite constants ...................................--*/
686 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
688 LLVMBool DontNullTerminate) {
689 /* Inverted the sense of AddNull because ', 0)' is a
690 better mnemonic for null termination than ', 1)'. */
691 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
692 DontNullTerminate == 0));
694 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
695 LLVMValueRef *ConstantVals,
696 unsigned Count, LLVMBool Packed) {
697 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
698 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
702 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
703 LLVMBool DontNullTerminate) {
704 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
707 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
708 LLVMValueRef *ConstantVals, unsigned Length) {
709 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
710 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
712 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
714 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
718 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
719 LLVMValueRef *ConstantVals,
721 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
722 StructType *Ty = cast<StructType>(unwrap(StructTy));
724 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
727 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
728 return wrap(ConstantVector::get(makeArrayRef(
729 unwrap<Constant>(ScalarConstantVals, Size), Size)));
732 /*-- Opcode mapping */
734 static LLVMOpcode map_to_llvmopcode(int opcode)
737 default: llvm_unreachable("Unhandled Opcode.");
738 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
739 #include "llvm/IR/Instruction.def"
744 static int map_from_llvmopcode(LLVMOpcode code)
747 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
748 #include "llvm/IR/Instruction.def"
751 llvm_unreachable("Unhandled Opcode.");
754 /*--.. Constant expressions ................................................--*/
756 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
757 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
760 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
761 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
764 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
765 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
768 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
769 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
772 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
773 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
776 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
777 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
781 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
782 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
785 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
786 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
789 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
790 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
791 unwrap<Constant>(RHSConstant)));
794 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
795 LLVMValueRef RHSConstant) {
796 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
797 unwrap<Constant>(RHSConstant)));
800 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
801 LLVMValueRef RHSConstant) {
802 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
803 unwrap<Constant>(RHSConstant)));
806 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
807 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
808 unwrap<Constant>(RHSConstant)));
811 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
812 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
813 unwrap<Constant>(RHSConstant)));
816 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
817 LLVMValueRef RHSConstant) {
818 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
819 unwrap<Constant>(RHSConstant)));
822 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
823 LLVMValueRef RHSConstant) {
824 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
825 unwrap<Constant>(RHSConstant)));
828 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
829 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
830 unwrap<Constant>(RHSConstant)));
833 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
834 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
835 unwrap<Constant>(RHSConstant)));
838 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
839 LLVMValueRef RHSConstant) {
840 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
841 unwrap<Constant>(RHSConstant)));
844 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
845 LLVMValueRef RHSConstant) {
846 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
847 unwrap<Constant>(RHSConstant)));
850 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
851 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
852 unwrap<Constant>(RHSConstant)));
855 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
856 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
857 unwrap<Constant>(RHSConstant)));
860 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
861 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
862 unwrap<Constant>(RHSConstant)));
865 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
866 LLVMValueRef RHSConstant) {
867 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
868 unwrap<Constant>(RHSConstant)));
871 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
872 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
873 unwrap<Constant>(RHSConstant)));
876 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
877 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
878 unwrap<Constant>(RHSConstant)));
881 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
882 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
883 unwrap<Constant>(RHSConstant)));
886 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
887 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
888 unwrap<Constant>(RHSConstant)));
891 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
892 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
893 unwrap<Constant>(RHSConstant)));
896 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
897 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
898 unwrap<Constant>(RHSConstant)));
901 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
902 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
903 unwrap<Constant>(RHSConstant)));
906 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
907 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
908 return wrap(ConstantExpr::getICmp(Predicate,
909 unwrap<Constant>(LHSConstant),
910 unwrap<Constant>(RHSConstant)));
913 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
914 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
915 return wrap(ConstantExpr::getFCmp(Predicate,
916 unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
921 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
922 unwrap<Constant>(RHSConstant)));
925 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
926 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
927 unwrap<Constant>(RHSConstant)));
930 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
931 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
932 unwrap<Constant>(RHSConstant)));
935 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
936 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
937 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
939 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
943 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
944 LLVMValueRef *ConstantIndices,
945 unsigned NumIndices) {
946 Constant* Val = unwrap<Constant>(ConstantVal);
947 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
949 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
952 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
953 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
957 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
958 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
962 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
963 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
967 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
968 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
972 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
973 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
977 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
978 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
982 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
983 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
987 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
988 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
992 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
993 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
997 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
998 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1002 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1003 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1007 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1008 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1012 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1013 LLVMTypeRef ToType) {
1014 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1018 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1019 LLVMTypeRef ToType) {
1020 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1024 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1025 LLVMTypeRef ToType) {
1026 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1030 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1031 LLVMTypeRef ToType) {
1032 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1036 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1037 LLVMBool isSigned) {
1038 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1039 unwrap(ToType), isSigned));
1042 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1043 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1047 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1048 LLVMValueRef ConstantIfTrue,
1049 LLVMValueRef ConstantIfFalse) {
1050 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1051 unwrap<Constant>(ConstantIfTrue),
1052 unwrap<Constant>(ConstantIfFalse)));
1055 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1056 LLVMValueRef IndexConstant) {
1057 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1058 unwrap<Constant>(IndexConstant)));
1061 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1062 LLVMValueRef ElementValueConstant,
1063 LLVMValueRef IndexConstant) {
1064 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1065 unwrap<Constant>(ElementValueConstant),
1066 unwrap<Constant>(IndexConstant)));
1069 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1070 LLVMValueRef VectorBConstant,
1071 LLVMValueRef MaskConstant) {
1072 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1073 unwrap<Constant>(VectorBConstant),
1074 unwrap<Constant>(MaskConstant)));
1077 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1079 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1080 makeArrayRef(IdxList, NumIdx)));
1083 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1084 LLVMValueRef ElementValueConstant,
1085 unsigned *IdxList, unsigned NumIdx) {
1086 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1087 unwrap<Constant>(ElementValueConstant),
1088 makeArrayRef(IdxList, NumIdx)));
1091 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1092 const char *Constraints,
1093 LLVMBool HasSideEffects,
1094 LLVMBool IsAlignStack) {
1095 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1096 Constraints, HasSideEffects, IsAlignStack));
1099 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1100 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1103 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1105 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1106 return wrap(unwrap<GlobalValue>(Global)->getParent());
1109 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1110 return unwrap<GlobalValue>(Global)->isDeclaration();
1113 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1114 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1115 case GlobalValue::ExternalLinkage:
1116 return LLVMExternalLinkage;
1117 case GlobalValue::AvailableExternallyLinkage:
1118 return LLVMAvailableExternallyLinkage;
1119 case GlobalValue::LinkOnceAnyLinkage:
1120 return LLVMLinkOnceAnyLinkage;
1121 case GlobalValue::LinkOnceODRLinkage:
1122 return LLVMLinkOnceODRLinkage;
1123 case GlobalValue::LinkOnceODRAutoHideLinkage:
1124 return LLVMLinkOnceODRAutoHideLinkage;
1125 case GlobalValue::WeakAnyLinkage:
1126 return LLVMWeakAnyLinkage;
1127 case GlobalValue::WeakODRLinkage:
1128 return LLVMWeakODRLinkage;
1129 case GlobalValue::AppendingLinkage:
1130 return LLVMAppendingLinkage;
1131 case GlobalValue::InternalLinkage:
1132 return LLVMInternalLinkage;
1133 case GlobalValue::PrivateLinkage:
1134 return LLVMPrivateLinkage;
1135 case GlobalValue::LinkerPrivateLinkage:
1136 return LLVMLinkerPrivateLinkage;
1137 case GlobalValue::LinkerPrivateWeakLinkage:
1138 return LLVMLinkerPrivateWeakLinkage;
1139 case GlobalValue::DLLImportLinkage:
1140 return LLVMDLLImportLinkage;
1141 case GlobalValue::DLLExportLinkage:
1142 return LLVMDLLExportLinkage;
1143 case GlobalValue::ExternalWeakLinkage:
1144 return LLVMExternalWeakLinkage;
1145 case GlobalValue::CommonLinkage:
1146 return LLVMCommonLinkage;
1149 llvm_unreachable("Invalid GlobalValue linkage!");
1152 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1153 GlobalValue *GV = unwrap<GlobalValue>(Global);
1156 case LLVMExternalLinkage:
1157 GV->setLinkage(GlobalValue::ExternalLinkage);
1159 case LLVMAvailableExternallyLinkage:
1160 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1162 case LLVMLinkOnceAnyLinkage:
1163 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1165 case LLVMLinkOnceODRLinkage:
1166 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1168 case LLVMLinkOnceODRAutoHideLinkage:
1169 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1171 case LLVMWeakAnyLinkage:
1172 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1174 case LLVMWeakODRLinkage:
1175 GV->setLinkage(GlobalValue::WeakODRLinkage);
1177 case LLVMAppendingLinkage:
1178 GV->setLinkage(GlobalValue::AppendingLinkage);
1180 case LLVMInternalLinkage:
1181 GV->setLinkage(GlobalValue::InternalLinkage);
1183 case LLVMPrivateLinkage:
1184 GV->setLinkage(GlobalValue::PrivateLinkage);
1186 case LLVMLinkerPrivateLinkage:
1187 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1189 case LLVMLinkerPrivateWeakLinkage:
1190 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1192 case LLVMDLLImportLinkage:
1193 GV->setLinkage(GlobalValue::DLLImportLinkage);
1195 case LLVMDLLExportLinkage:
1196 GV->setLinkage(GlobalValue::DLLExportLinkage);
1198 case LLVMExternalWeakLinkage:
1199 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1201 case LLVMGhostLinkage:
1203 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1205 case LLVMCommonLinkage:
1206 GV->setLinkage(GlobalValue::CommonLinkage);
1211 const char *LLVMGetSection(LLVMValueRef Global) {
1212 return unwrap<GlobalValue>(Global)->getSection().c_str();
1215 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1216 unwrap<GlobalValue>(Global)->setSection(Section);
1219 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1220 return static_cast<LLVMVisibility>(
1221 unwrap<GlobalValue>(Global)->getVisibility());
1224 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1225 unwrap<GlobalValue>(Global)
1226 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1229 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1230 return unwrap<GlobalValue>(Global)->getAlignment();
1233 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1234 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1237 /*--.. Operations on global variables ......................................--*/
1239 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1240 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1241 GlobalValue::ExternalLinkage, 0, Name));
1244 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1246 unsigned AddressSpace) {
1247 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1248 GlobalValue::ExternalLinkage, 0, Name, 0,
1249 GlobalVariable::NotThreadLocal, AddressSpace));
1252 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1253 return wrap(unwrap(M)->getNamedGlobal(Name));
1256 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1257 Module *Mod = unwrap(M);
1258 Module::global_iterator I = Mod->global_begin();
1259 if (I == Mod->global_end())
1264 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1265 Module *Mod = unwrap(M);
1266 Module::global_iterator I = Mod->global_end();
1267 if (I == Mod->global_begin())
1272 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1273 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1274 Module::global_iterator I = GV;
1275 if (++I == GV->getParent()->global_end())
1280 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1281 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1282 Module::global_iterator I = GV;
1283 if (I == GV->getParent()->global_begin())
1288 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1289 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1292 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1293 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1294 if ( !GV->hasInitializer() )
1296 return wrap(GV->getInitializer());
1299 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1300 unwrap<GlobalVariable>(GlobalVar)
1301 ->setInitializer(unwrap<Constant>(ConstantVal));
1304 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1305 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1308 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1309 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1312 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1313 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1316 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1317 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1320 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1321 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1322 case GlobalVariable::NotThreadLocal:
1323 return LLVMNotThreadLocal;
1324 case GlobalVariable::GeneralDynamicTLSModel:
1325 return LLVMGeneralDynamicTLSModel;
1326 case GlobalVariable::LocalDynamicTLSModel:
1327 return LLVMLocalDynamicTLSModel;
1328 case GlobalVariable::InitialExecTLSModel:
1329 return LLVMInitialExecTLSModel;
1330 case GlobalVariable::LocalExecTLSModel:
1331 return LLVMLocalExecTLSModel;
1334 llvm_unreachable("Invalid GlobalVariable thread local mode");
1337 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1338 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1341 case LLVMNotThreadLocal:
1342 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1344 case LLVMGeneralDynamicTLSModel:
1345 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1347 case LLVMLocalDynamicTLSModel:
1348 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1350 case LLVMInitialExecTLSModel:
1351 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1353 case LLVMLocalExecTLSModel:
1354 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1359 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1360 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1363 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1364 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1367 /*--.. Operations on aliases ......................................--*/
1369 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1371 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1372 unwrap<Constant>(Aliasee), unwrap (M)));
1375 /*--.. Operations on functions .............................................--*/
1377 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1378 LLVMTypeRef FunctionTy) {
1379 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1380 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1383 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1384 return wrap(unwrap(M)->getFunction(Name));
1387 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1388 Module *Mod = unwrap(M);
1389 Module::iterator I = Mod->begin();
1390 if (I == Mod->end())
1395 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1396 Module *Mod = unwrap(M);
1397 Module::iterator I = Mod->end();
1398 if (I == Mod->begin())
1403 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1404 Function *Func = unwrap<Function>(Fn);
1405 Module::iterator I = Func;
1406 if (++I == Func->getParent()->end())
1411 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1412 Function *Func = unwrap<Function>(Fn);
1413 Module::iterator I = Func;
1414 if (I == Func->getParent()->begin())
1419 void LLVMDeleteFunction(LLVMValueRef Fn) {
1420 unwrap<Function>(Fn)->eraseFromParent();
1423 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1424 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1425 return F->getIntrinsicID();
1429 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1430 return unwrap<Function>(Fn)->getCallingConv();
1433 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1434 return unwrap<Function>(Fn)->setCallingConv(
1435 static_cast<CallingConv::ID>(CC));
1438 const char *LLVMGetGC(LLVMValueRef Fn) {
1439 Function *F = unwrap<Function>(Fn);
1440 return F->hasGC()? F->getGC() : 0;
1443 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1444 Function *F = unwrap<Function>(Fn);
1451 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1452 Function *Func = unwrap<Function>(Fn);
1453 const AttributeSet PAL = Func->getAttributes();
1455 const AttributeSet PALnew =
1456 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1457 AttributeSet::get(Func->getContext(),
1458 AttributeSet::FunctionIndex, B));
1459 Func->setAttributes(PALnew);
1462 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1464 Function *Func = unwrap<Function>(Fn);
1465 AttributeSet::AttrIndex Idx =
1466 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1469 B.addAttribute(A, V);
1470 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1471 Func->addAttributes(Idx, Set);
1474 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1475 Function *Func = unwrap<Function>(Fn);
1476 const AttributeSet PAL = Func->getAttributes();
1478 const AttributeSet PALnew =
1479 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1480 AttributeSet::get(Func->getContext(),
1481 AttributeSet::FunctionIndex, B));
1482 Func->setAttributes(PALnew);
1485 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1486 Function *Func = unwrap<Function>(Fn);
1487 const AttributeSet PAL = Func->getAttributes();
1488 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1491 /*--.. Operations on parameters ............................................--*/
1493 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1494 // This function is strictly redundant to
1495 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1496 return unwrap<Function>(FnRef)->arg_size();
1499 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1500 Function *Fn = unwrap<Function>(FnRef);
1501 for (Function::arg_iterator I = Fn->arg_begin(),
1502 E = Fn->arg_end(); I != E; I++)
1503 *ParamRefs++ = wrap(I);
1506 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1507 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1513 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1514 return wrap(unwrap<Argument>(V)->getParent());
1517 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1518 Function *Func = unwrap<Function>(Fn);
1519 Function::arg_iterator I = Func->arg_begin();
1520 if (I == Func->arg_end())
1525 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1526 Function *Func = unwrap<Function>(Fn);
1527 Function::arg_iterator I = Func->arg_end();
1528 if (I == Func->arg_begin())
1533 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1534 Argument *A = unwrap<Argument>(Arg);
1535 Function::arg_iterator I = A;
1536 if (++I == A->getParent()->arg_end())
1541 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1542 Argument *A = unwrap<Argument>(Arg);
1543 Function::arg_iterator I = A;
1544 if (I == A->getParent()->arg_begin())
1549 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1550 Argument *A = unwrap<Argument>(Arg);
1552 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1555 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1556 Argument *A = unwrap<Argument>(Arg);
1558 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1561 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1562 Argument *A = unwrap<Argument>(Arg);
1563 return (LLVMAttribute)A->getParent()->getAttributes().
1564 Raw(A->getArgNo()+1);
1568 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1569 Argument *A = unwrap<Argument>(Arg);
1571 B.addAlignmentAttr(align);
1572 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1575 /*--.. Operations on basic blocks ..........................................--*/
1577 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1578 return wrap(static_cast<Value*>(unwrap(BB)));
1581 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1582 return isa<BasicBlock>(unwrap(Val));
1585 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1586 return wrap(unwrap<BasicBlock>(Val));
1589 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1590 return wrap(unwrap(BB)->getParent());
1593 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1594 return wrap(unwrap(BB)->getTerminator());
1597 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1598 return unwrap<Function>(FnRef)->size();
1601 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1602 Function *Fn = unwrap<Function>(FnRef);
1603 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1604 *BasicBlocksRefs++ = wrap(I);
1607 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1608 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1611 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1612 Function *Func = unwrap<Function>(Fn);
1613 Function::iterator I = Func->begin();
1614 if (I == Func->end())
1619 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1620 Function *Func = unwrap<Function>(Fn);
1621 Function::iterator I = Func->end();
1622 if (I == Func->begin())
1627 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1628 BasicBlock *Block = unwrap(BB);
1629 Function::iterator I = Block;
1630 if (++I == Block->getParent()->end())
1635 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1636 BasicBlock *Block = unwrap(BB);
1637 Function::iterator I = Block;
1638 if (I == Block->getParent()->begin())
1643 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1646 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1649 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1650 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1653 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1654 LLVMBasicBlockRef BBRef,
1656 BasicBlock *BB = unwrap(BBRef);
1657 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1660 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1662 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1665 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1666 unwrap(BBRef)->eraseFromParent();
1669 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1670 unwrap(BBRef)->removeFromParent();
1673 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1674 unwrap(BB)->moveBefore(unwrap(MovePos));
1677 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1678 unwrap(BB)->moveAfter(unwrap(MovePos));
1681 /*--.. Operations on instructions ..........................................--*/
1683 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1684 return wrap(unwrap<Instruction>(Inst)->getParent());
1687 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1688 BasicBlock *Block = unwrap(BB);
1689 BasicBlock::iterator I = Block->begin();
1690 if (I == Block->end())
1695 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1696 BasicBlock *Block = unwrap(BB);
1697 BasicBlock::iterator I = Block->end();
1698 if (I == Block->begin())
1703 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1704 Instruction *Instr = unwrap<Instruction>(Inst);
1705 BasicBlock::iterator I = Instr;
1706 if (++I == Instr->getParent()->end())
1711 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1712 Instruction *Instr = unwrap<Instruction>(Inst);
1713 BasicBlock::iterator I = Instr;
1714 if (I == Instr->getParent()->begin())
1719 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1720 unwrap<Instruction>(Inst)->eraseFromParent();
1723 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1724 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1725 return (LLVMIntPredicate)I->getPredicate();
1726 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1727 if (CE->getOpcode() == Instruction::ICmp)
1728 return (LLVMIntPredicate)CE->getPredicate();
1729 return (LLVMIntPredicate)0;
1732 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1733 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1734 return map_to_llvmopcode(C->getOpcode());
1735 return (LLVMOpcode)0;
1738 /*--.. Call and invoke instructions ........................................--*/
1740 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1741 Value *V = unwrap(Instr);
1742 if (CallInst *CI = dyn_cast<CallInst>(V))
1743 return CI->getCallingConv();
1744 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1745 return II->getCallingConv();
1746 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1749 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1750 Value *V = unwrap(Instr);
1751 if (CallInst *CI = dyn_cast<CallInst>(V))
1752 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1753 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1754 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1755 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1758 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1760 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1763 Call.getAttributes().addAttributes(Call->getContext(), index,
1764 AttributeSet::get(Call->getContext(),
1768 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1770 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1772 Call.setAttributes(Call.getAttributes()
1773 .removeAttributes(Call->getContext(), index,
1774 AttributeSet::get(Call->getContext(),
1778 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1780 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1782 B.addAlignmentAttr(align);
1783 Call.setAttributes(Call.getAttributes()
1784 .addAttributes(Call->getContext(), index,
1785 AttributeSet::get(Call->getContext(),
1789 /*--.. Operations on call instructions (only) ..............................--*/
1791 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1792 return unwrap<CallInst>(Call)->isTailCall();
1795 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1796 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1799 /*--.. Operations on switch instructions (only) ............................--*/
1801 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1802 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1805 /*--.. Operations on phi nodes .............................................--*/
1807 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1808 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1809 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1810 for (unsigned I = 0; I != Count; ++I)
1811 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1814 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1815 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1818 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1819 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1822 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1823 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1827 /*===-- Instruction builders ----------------------------------------------===*/
1829 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1830 return wrap(new IRBuilder<>(*unwrap(C)));
1833 LLVMBuilderRef LLVMCreateBuilder(void) {
1834 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1837 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1838 LLVMValueRef Instr) {
1839 BasicBlock *BB = unwrap(Block);
1840 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1841 unwrap(Builder)->SetInsertPoint(BB, I);
1844 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1845 Instruction *I = unwrap<Instruction>(Instr);
1846 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1849 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1850 BasicBlock *BB = unwrap(Block);
1851 unwrap(Builder)->SetInsertPoint(BB);
1854 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1855 return wrap(unwrap(Builder)->GetInsertBlock());
1858 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1859 unwrap(Builder)->ClearInsertionPoint();
1862 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1863 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1866 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1868 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1871 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1872 delete unwrap(Builder);
1875 /*--.. Metadata builders ...................................................--*/
1877 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1878 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1879 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1882 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1883 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1884 .getAsMDNode(unwrap(Builder)->getContext()));
1887 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1888 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1892 /*--.. Instruction builders ................................................--*/
1894 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1895 return wrap(unwrap(B)->CreateRetVoid());
1898 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1899 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1902 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1904 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1907 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1908 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1911 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1912 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1913 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1916 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1917 LLVMBasicBlockRef Else, unsigned NumCases) {
1918 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1921 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1922 unsigned NumDests) {
1923 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1926 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1927 LLVMValueRef *Args, unsigned NumArgs,
1928 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1930 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1931 makeArrayRef(unwrap(Args), NumArgs),
1935 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1936 LLVMValueRef PersFn, unsigned NumClauses,
1938 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1939 cast<Function>(unwrap(PersFn)),
1943 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1944 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1947 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1948 return wrap(unwrap(B)->CreateUnreachable());
1951 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1952 LLVMBasicBlockRef Dest) {
1953 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1956 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1957 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1960 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1961 unwrap<LandingPadInst>(LandingPad)->
1962 addClause(cast<Constant>(unwrap(ClauseVal)));
1965 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1966 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1969 /*--.. Arithmetic ..........................................................--*/
1971 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1973 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1976 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1978 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1981 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1983 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1986 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1988 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1991 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1993 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1996 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1998 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2001 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2003 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2006 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2008 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2011 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2013 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2016 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2018 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2021 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2023 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2026 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2028 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2031 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2033 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2036 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2038 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2041 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2042 LLVMValueRef RHS, const char *Name) {
2043 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2046 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2048 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2051 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2053 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2056 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2058 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2061 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2063 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2066 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2068 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2071 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2073 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2076 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2078 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2081 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2083 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2086 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2088 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2091 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2093 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2096 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2097 LLVMValueRef LHS, LLVMValueRef RHS,
2099 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2100 unwrap(RHS), Name));
2103 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2104 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2107 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2109 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2112 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2114 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2117 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2118 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2121 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2122 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2125 /*--.. Memory ..............................................................--*/
2127 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2129 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2130 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2131 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2132 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2133 ITy, unwrap(Ty), AllocSize,
2135 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2138 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2139 LLVMValueRef Val, const char *Name) {
2140 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2141 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2142 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2143 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2144 ITy, unwrap(Ty), AllocSize,
2145 unwrap(Val), 0, "");
2146 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2149 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2151 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2154 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2155 LLVMValueRef Val, const char *Name) {
2156 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2159 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2160 return wrap(unwrap(B)->Insert(
2161 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2165 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2167 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2170 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2171 LLVMValueRef PointerVal) {
2172 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2175 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2176 LLVMValueRef *Indices, unsigned NumIndices,
2178 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2179 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2182 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2183 LLVMValueRef *Indices, unsigned NumIndices,
2185 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2186 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2189 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2190 unsigned Idx, const char *Name) {
2191 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2194 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2196 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2199 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2201 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2204 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2205 Value *P = unwrap<Value>(MemAccessInst);
2206 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2207 return LI->isVolatile();
2208 return cast<StoreInst>(P)->isVolatile();
2211 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2212 Value *P = unwrap<Value>(MemAccessInst);
2213 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2214 return LI->setVolatile(isVolatile);
2215 return cast<StoreInst>(P)->setVolatile(isVolatile);
2218 /*--.. Casts ...............................................................--*/
2220 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2221 LLVMTypeRef DestTy, const char *Name) {
2222 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2225 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2226 LLVMTypeRef DestTy, const char *Name) {
2227 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2230 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2231 LLVMTypeRef DestTy, const char *Name) {
2232 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2235 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2236 LLVMTypeRef DestTy, const char *Name) {
2237 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2240 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2241 LLVMTypeRef DestTy, const char *Name) {
2242 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2245 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2246 LLVMTypeRef DestTy, const char *Name) {
2247 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2250 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2251 LLVMTypeRef DestTy, const char *Name) {
2252 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2255 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2256 LLVMTypeRef DestTy, const char *Name) {
2257 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2260 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2261 LLVMTypeRef DestTy, const char *Name) {
2262 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2265 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2266 LLVMTypeRef DestTy, const char *Name) {
2267 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2270 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2271 LLVMTypeRef DestTy, const char *Name) {
2272 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2275 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2276 LLVMTypeRef DestTy, const char *Name) {
2277 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2280 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2281 LLVMTypeRef DestTy, const char *Name) {
2282 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2286 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2287 LLVMTypeRef DestTy, const char *Name) {
2288 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2292 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2293 LLVMTypeRef DestTy, const char *Name) {
2294 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2298 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2299 LLVMTypeRef DestTy, const char *Name) {
2300 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2301 unwrap(DestTy), Name));
2304 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2305 LLVMTypeRef DestTy, const char *Name) {
2306 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2309 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2310 LLVMTypeRef DestTy, const char *Name) {
2311 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2312 /*isSigned*/true, Name));
2315 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2316 LLVMTypeRef DestTy, const char *Name) {
2317 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2320 /*--.. Comparisons .........................................................--*/
2322 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2323 LLVMValueRef LHS, LLVMValueRef RHS,
2325 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2326 unwrap(LHS), unwrap(RHS), Name));
2329 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2330 LLVMValueRef LHS, LLVMValueRef RHS,
2332 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2333 unwrap(LHS), unwrap(RHS), Name));
2336 /*--.. Miscellaneous instructions ..........................................--*/
2338 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2339 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2342 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2343 LLVMValueRef *Args, unsigned NumArgs,
2345 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2346 makeArrayRef(unwrap(Args), NumArgs),
2350 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2351 LLVMValueRef Then, LLVMValueRef Else,
2353 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2357 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2358 LLVMTypeRef Ty, const char *Name) {
2359 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2362 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2363 LLVMValueRef Index, const char *Name) {
2364 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2368 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2369 LLVMValueRef EltVal, LLVMValueRef Index,
2371 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2372 unwrap(Index), Name));
2375 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2376 LLVMValueRef V2, LLVMValueRef Mask,
2378 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2379 unwrap(Mask), Name));
2382 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2383 unsigned Index, const char *Name) {
2384 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2387 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2388 LLVMValueRef EltVal, unsigned Index,
2390 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2394 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2396 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2399 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2401 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2404 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2405 LLVMValueRef RHS, const char *Name) {
2406 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2409 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2410 LLVMValueRef PTR, LLVMValueRef Val,
2411 LLVMAtomicOrdering ordering,
2412 LLVMBool singleThread) {
2413 AtomicRMWInst::BinOp intop;
2415 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2416 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2417 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2418 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2419 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2420 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2421 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2422 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2423 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2424 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2425 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2427 AtomicOrdering intordering;
2429 case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
2430 case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
2431 case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
2432 case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
2433 case LLVMAtomicOrderingRelease: intordering = Release; break;
2434 case LLVMAtomicOrderingAcquireRelease:
2435 intordering = AcquireRelease;
2437 case LLVMAtomicOrderingSequentiallyConsistent:
2438 intordering = SequentiallyConsistent;
2441 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2442 intordering, singleThread ? SingleThread : CrossThread));
2446 /*===-- Module providers --------------------------------------------------===*/
2448 LLVMModuleProviderRef
2449 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2450 return reinterpret_cast<LLVMModuleProviderRef>(M);
2453 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2458 /*===-- Memory buffers ----------------------------------------------------===*/
2460 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2462 LLVMMemoryBufferRef *OutMemBuf,
2463 char **OutMessage) {
2465 OwningPtr<MemoryBuffer> MB;
2467 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2468 *OutMemBuf = wrap(MB.take());
2472 *OutMessage = strdup(ec.message().c_str());
2476 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2477 char **OutMessage) {
2478 OwningPtr<MemoryBuffer> MB;
2480 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2481 *OutMemBuf = wrap(MB.take());
2485 *OutMessage = strdup(ec.message().c_str());
2489 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2490 const char *InputData,
2491 size_t InputDataLength,
2492 const char *BufferName,
2493 LLVMBool RequiresNullTerminator) {
2495 return wrap(MemoryBuffer::getMemBuffer(
2496 StringRef(InputData, InputDataLength),
2497 StringRef(BufferName),
2498 RequiresNullTerminator));
2501 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2502 const char *InputData,
2503 size_t InputDataLength,
2504 const char *BufferName) {
2506 return wrap(MemoryBuffer::getMemBufferCopy(
2507 StringRef(InputData, InputDataLength),
2508 StringRef(BufferName)));
2511 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2512 return unwrap(MemBuf)->getBufferStart();
2515 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2516 return unwrap(MemBuf)->getBufferSize();
2519 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2520 delete unwrap(MemBuf);
2523 /*===-- Pass Registry -----------------------------------------------------===*/
2525 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2526 return wrap(PassRegistry::getPassRegistry());
2529 /*===-- Pass Manager ------------------------------------------------------===*/
2531 LLVMPassManagerRef LLVMCreatePassManager() {
2532 return wrap(new PassManager());
2535 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2536 return wrap(new FunctionPassManager(unwrap(M)));
2539 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2540 return LLVMCreateFunctionPassManagerForModule(
2541 reinterpret_cast<LLVMModuleRef>(P));
2544 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2545 return unwrap<PassManager>(PM)->run(*unwrap(M));
2548 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2549 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2552 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2553 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2556 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2557 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2560 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2564 /*===-- Threading ------------------------------------------------------===*/
2566 LLVMBool LLVMStartMultithreaded() {
2567 return llvm_start_multithreaded();
2570 void LLVMStopMultithreaded() {
2571 llvm_stop_multithreaded();
2574 LLVMBool LLVMIsMultithreaded() {
2575 return llvm_is_multithreaded();