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/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/InlineAsm.h"
23 #include "llvm/IntrinsicInst.h"
24 #include "llvm/PassManager.h"
25 #include "llvm/Support/CallSite.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/system_error.h"
37 void llvm::initializeCore(PassRegistry &Registry) {
38 initializeDominatorTreePass(Registry);
39 initializePrintModulePassPass(Registry);
40 initializePrintFunctionPassPass(Registry);
41 initializeVerifierPass(Registry);
42 initializePreVerifierPass(Registry);
45 void LLVMInitializeCore(LLVMPassRegistryRef R) {
46 initializeCore(*unwrap(R));
49 /*===-- Error handling ----------------------------------------------------===*/
51 void LLVMDisposeMessage(char *Message) {
56 /*===-- Operations on contexts --------------------------------------------===*/
58 LLVMContextRef LLVMContextCreate() {
59 return wrap(new LLVMContext());
62 LLVMContextRef LLVMGetGlobalContext() {
63 return wrap(&getGlobalContext());
66 void LLVMContextDispose(LLVMContextRef C) {
70 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
72 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
75 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
76 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
80 /*===-- Operations on modules ---------------------------------------------===*/
82 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
83 return wrap(new Module(ModuleID, getGlobalContext()));
86 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
88 return wrap(new Module(ModuleID, *unwrap(C)));
91 void LLVMDisposeModule(LLVMModuleRef M) {
95 /*--.. Data layout .........................................................--*/
96 const char * LLVMGetDataLayout(LLVMModuleRef M) {
97 return unwrap(M)->getDataLayout().c_str();
100 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
101 unwrap(M)->setDataLayout(Triple);
104 /*--.. Target triple .......................................................--*/
105 const char * LLVMGetTarget(LLVMModuleRef M) {
106 return unwrap(M)->getTargetTriple().c_str();
109 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
110 unwrap(M)->setTargetTriple(Triple);
113 void LLVMDumpModule(LLVMModuleRef M) {
117 /*--.. Operations on inline assembler ......................................--*/
118 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
119 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
123 /*--.. Operations on module contexts ......................................--*/
124 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
125 return wrap(&unwrap(M)->getContext());
129 /*===-- Operations on types -----------------------------------------------===*/
131 /*--.. Operations on all types (mostly) ....................................--*/
133 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
134 switch (unwrap(Ty)->getTypeID()) {
136 assert(false && "Unhandled TypeID.");
138 return LLVMVoidTypeKind;
139 case Type::FloatTyID:
140 return LLVMFloatTypeKind;
141 case Type::DoubleTyID:
142 return LLVMDoubleTypeKind;
143 case Type::X86_FP80TyID:
144 return LLVMX86_FP80TypeKind;
145 case Type::FP128TyID:
146 return LLVMFP128TypeKind;
147 case Type::PPC_FP128TyID:
148 return LLVMPPC_FP128TypeKind;
149 case Type::LabelTyID:
150 return LLVMLabelTypeKind;
151 case Type::MetadataTyID:
152 return LLVMMetadataTypeKind;
153 case Type::IntegerTyID:
154 return LLVMIntegerTypeKind;
155 case Type::FunctionTyID:
156 return LLVMFunctionTypeKind;
157 case Type::StructTyID:
158 return LLVMStructTypeKind;
159 case Type::ArrayTyID:
160 return LLVMArrayTypeKind;
161 case Type::PointerTyID:
162 return LLVMPointerTypeKind;
163 case Type::VectorTyID:
164 return LLVMVectorTypeKind;
165 case Type::X86_MMXTyID:
166 return LLVMX86_MMXTypeKind;
170 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
171 return wrap(&unwrap(Ty)->getContext());
174 /*--.. Operations on integer types .........................................--*/
176 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
177 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
179 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
180 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
182 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
183 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
185 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
186 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
188 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
189 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
191 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
192 return wrap(IntegerType::get(*unwrap(C), NumBits));
195 LLVMTypeRef LLVMInt1Type(void) {
196 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
198 LLVMTypeRef LLVMInt8Type(void) {
199 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
201 LLVMTypeRef LLVMInt16Type(void) {
202 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
204 LLVMTypeRef LLVMInt32Type(void) {
205 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
207 LLVMTypeRef LLVMInt64Type(void) {
208 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
210 LLVMTypeRef LLVMIntType(unsigned NumBits) {
211 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
214 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
215 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
218 /*--.. Operations on real types ............................................--*/
220 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
221 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
223 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
224 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
226 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
227 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
229 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
230 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
232 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
233 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
235 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
236 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
239 LLVMTypeRef LLVMFloatType(void) {
240 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
242 LLVMTypeRef LLVMDoubleType(void) {
243 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
245 LLVMTypeRef LLVMX86FP80Type(void) {
246 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
248 LLVMTypeRef LLVMFP128Type(void) {
249 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
251 LLVMTypeRef LLVMPPCFP128Type(void) {
252 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
254 LLVMTypeRef LLVMX86MMXType(void) {
255 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
258 /*--.. Operations on function types ........................................--*/
260 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
261 LLVMTypeRef *ParamTypes, unsigned ParamCount,
263 std::vector<Type*> Tys;
264 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
265 Tys.push_back(unwrap(*I));
267 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
270 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
271 return unwrap<FunctionType>(FunctionTy)->isVarArg();
274 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
275 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
278 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
279 return unwrap<FunctionType>(FunctionTy)->getNumParams();
282 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
283 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
284 for (FunctionType::param_iterator I = Ty->param_begin(),
285 E = Ty->param_end(); I != E; ++I)
289 /*--.. Operations on struct types ..........................................--*/
291 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
292 unsigned ElementCount, LLVMBool Packed) {
293 std::vector<Type*> Tys;
294 for (LLVMTypeRef *I = ElementTypes,
295 *E = ElementTypes + ElementCount; I != E; ++I)
296 Tys.push_back(unwrap(*I));
298 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
301 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
302 unsigned ElementCount, LLVMBool Packed) {
303 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
304 ElementCount, Packed);
308 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
309 return unwrap<StructType>(StructTy)->getNumElements();
312 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
313 StructType *Ty = unwrap<StructType>(StructTy);
314 for (StructType::element_iterator I = Ty->element_begin(),
315 E = Ty->element_end(); I != E; ++I)
319 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
320 return unwrap<StructType>(StructTy)->isPacked();
323 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
325 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
326 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
329 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
330 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
333 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
334 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
337 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
338 return wrap(unwrap<SequentialType>(Ty)->getElementType());
341 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
342 return unwrap<ArrayType>(ArrayTy)->getNumElements();
345 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
346 return unwrap<PointerType>(PointerTy)->getAddressSpace();
349 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
350 return unwrap<VectorType>(VectorTy)->getNumElements();
353 /*--.. Operations on other types ...........................................--*/
355 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
356 return wrap(Type::getVoidTy(*unwrap(C)));
358 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
359 return wrap(Type::getLabelTy(*unwrap(C)));
362 LLVMTypeRef LLVMVoidType(void) {
363 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
365 LLVMTypeRef LLVMLabelType(void) {
366 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
369 /*===-- Operations on values ----------------------------------------------===*/
371 /*--.. Operations on all values ............................................--*/
373 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
374 return wrap(unwrap(Val)->getType());
377 const char *LLVMGetValueName(LLVMValueRef Val) {
378 return unwrap(Val)->getName().data();
381 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
382 unwrap(Val)->setName(Name);
385 void LLVMDumpValue(LLVMValueRef Val) {
389 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
390 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
393 int LLVMHasMetadata(LLVMValueRef Inst) {
394 return unwrap<Instruction>(Inst)->hasMetadata();
397 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
398 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
401 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
402 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
405 /*--.. Conversion functions ................................................--*/
407 #define LLVM_DEFINE_VALUE_CAST(name) \
408 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
409 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
412 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
414 /*--.. Operations on Uses ..................................................--*/
415 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
416 Value *V = unwrap(Val);
417 Value::use_iterator I = V->use_begin();
418 if (I == V->use_end())
420 return wrap(&(I.getUse()));
423 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
424 Use *Next = unwrap(U)->getNext();
430 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
431 return wrap(unwrap(U)->getUser());
434 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
435 return wrap(unwrap(U)->get());
438 /*--.. Operations on Users .................................................--*/
439 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
440 return wrap(unwrap<User>(Val)->getOperand(Index));
443 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
444 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
447 int LLVMGetNumOperands(LLVMValueRef Val) {
448 return unwrap<User>(Val)->getNumOperands();
451 /*--.. Operations on constants of any type .................................--*/
453 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
454 return wrap(Constant::getNullValue(unwrap(Ty)));
457 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
458 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
461 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
462 return wrap(UndefValue::get(unwrap(Ty)));
465 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
466 return isa<Constant>(unwrap(Ty));
469 LLVMBool LLVMIsNull(LLVMValueRef Val) {
470 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
471 return C->isNullValue();
475 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
476 return isa<UndefValue>(unwrap(Val));
479 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
481 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
484 /*--.. Operations on metadata nodes ........................................--*/
486 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
488 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
491 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
492 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
495 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
497 return wrap(MDNode::get(*unwrap(C),
498 ArrayRef<Value*>(unwrap<Value>(Vals, Count), Count)));
501 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
502 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
505 /*--.. Operations on scalar constants ......................................--*/
507 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
508 LLVMBool SignExtend) {
509 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
512 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
514 const uint64_t Words[]) {
515 IntegerType *Ty = unwrap<IntegerType>(IntTy);
516 return wrap(ConstantInt::get(Ty->getContext(),
517 APInt(Ty->getBitWidth(), NumWords, Words)));
520 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
522 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
526 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
527 unsigned SLen, uint8_t Radix) {
528 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
532 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
533 return wrap(ConstantFP::get(unwrap(RealTy), N));
536 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
537 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
540 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
542 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
545 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
546 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
549 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
550 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
553 /*--.. Operations on composite constants ...................................--*/
555 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
557 LLVMBool DontNullTerminate) {
558 /* Inverted the sense of AddNull because ', 0)' is a
559 better mnemonic for null termination than ', 1)'. */
560 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
561 DontNullTerminate == 0));
563 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
564 LLVMValueRef *ConstantVals,
565 unsigned Count, LLVMBool Packed) {
566 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
567 return wrap(ConstantStruct::getAnon(*unwrap(C),
568 ArrayRef<Constant*>(Elements, Count),
572 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
573 LLVMBool DontNullTerminate) {
574 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
577 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
578 LLVMValueRef *ConstantVals, unsigned Length) {
579 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
580 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
582 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
584 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
587 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
588 return wrap(ConstantVector::get(ArrayRef<Constant*>(
589 unwrap<Constant>(ScalarConstantVals, Size), Size)));
591 /*--.. Constant expressions ................................................--*/
593 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
594 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
597 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
598 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
601 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
602 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
605 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
606 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
609 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
610 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
613 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
614 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
618 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
619 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
622 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
623 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
626 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
627 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
628 unwrap<Constant>(RHSConstant)));
631 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
632 LLVMValueRef RHSConstant) {
633 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
634 unwrap<Constant>(RHSConstant)));
637 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
638 LLVMValueRef RHSConstant) {
639 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
640 unwrap<Constant>(RHSConstant)));
643 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
644 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
645 unwrap<Constant>(RHSConstant)));
648 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
649 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
650 unwrap<Constant>(RHSConstant)));
653 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
654 LLVMValueRef RHSConstant) {
655 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
656 unwrap<Constant>(RHSConstant)));
659 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
660 LLVMValueRef RHSConstant) {
661 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
662 unwrap<Constant>(RHSConstant)));
665 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
666 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
667 unwrap<Constant>(RHSConstant)));
670 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
671 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
672 unwrap<Constant>(RHSConstant)));
675 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
676 LLVMValueRef RHSConstant) {
677 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
678 unwrap<Constant>(RHSConstant)));
681 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
682 LLVMValueRef RHSConstant) {
683 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
684 unwrap<Constant>(RHSConstant)));
687 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
688 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
689 unwrap<Constant>(RHSConstant)));
692 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
693 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
694 unwrap<Constant>(RHSConstant)));
697 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
698 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
699 unwrap<Constant>(RHSConstant)));
702 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
703 LLVMValueRef RHSConstant) {
704 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
705 unwrap<Constant>(RHSConstant)));
708 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
709 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
710 unwrap<Constant>(RHSConstant)));
713 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
714 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
715 unwrap<Constant>(RHSConstant)));
718 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
719 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
720 unwrap<Constant>(RHSConstant)));
723 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
724 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
725 unwrap<Constant>(RHSConstant)));
728 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
729 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
730 unwrap<Constant>(RHSConstant)));
733 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
734 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
735 unwrap<Constant>(RHSConstant)));
738 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
739 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
740 unwrap<Constant>(RHSConstant)));
743 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
744 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
745 return wrap(ConstantExpr::getICmp(Predicate,
746 unwrap<Constant>(LHSConstant),
747 unwrap<Constant>(RHSConstant)));
750 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
751 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
752 return wrap(ConstantExpr::getFCmp(Predicate,
753 unwrap<Constant>(LHSConstant),
754 unwrap<Constant>(RHSConstant)));
757 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
759 unwrap<Constant>(RHSConstant)));
762 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
763 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
764 unwrap<Constant>(RHSConstant)));
767 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
768 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
769 unwrap<Constant>(RHSConstant)));
772 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
773 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
774 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
775 unwrap<Constant>(ConstantIndices,
780 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
781 LLVMValueRef *ConstantIndices,
782 unsigned NumIndices) {
783 Constant* Val = unwrap<Constant>(ConstantVal);
784 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
785 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
788 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
789 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
793 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
794 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
798 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
799 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
803 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
804 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
808 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
809 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
813 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
814 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
818 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
819 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
823 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
824 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
828 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
829 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
833 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
834 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
838 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
839 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
843 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
844 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
848 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
849 LLVMTypeRef ToType) {
850 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
854 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
855 LLVMTypeRef ToType) {
856 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
860 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
861 LLVMTypeRef ToType) {
862 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
866 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
867 LLVMTypeRef ToType) {
868 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
872 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
874 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
875 unwrap(ToType), isSigned));
878 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
879 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
883 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
884 LLVMValueRef ConstantIfTrue,
885 LLVMValueRef ConstantIfFalse) {
886 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
887 unwrap<Constant>(ConstantIfTrue),
888 unwrap<Constant>(ConstantIfFalse)));
891 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
892 LLVMValueRef IndexConstant) {
893 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
894 unwrap<Constant>(IndexConstant)));
897 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
898 LLVMValueRef ElementValueConstant,
899 LLVMValueRef IndexConstant) {
900 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
901 unwrap<Constant>(ElementValueConstant),
902 unwrap<Constant>(IndexConstant)));
905 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
906 LLVMValueRef VectorBConstant,
907 LLVMValueRef MaskConstant) {
908 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
909 unwrap<Constant>(VectorBConstant),
910 unwrap<Constant>(MaskConstant)));
913 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
915 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
916 ArrayRef<unsigned>(IdxList,
920 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
921 LLVMValueRef ElementValueConstant,
922 unsigned *IdxList, unsigned NumIdx) {
923 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
924 unwrap<Constant>(ElementValueConstant),
925 ArrayRef<unsigned>(IdxList,
929 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
930 const char *Constraints,
931 LLVMBool HasSideEffects,
932 LLVMBool IsAlignStack) {
933 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
934 Constraints, HasSideEffects, IsAlignStack));
937 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
938 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
941 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
943 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
944 return wrap(unwrap<GlobalValue>(Global)->getParent());
947 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
948 return unwrap<GlobalValue>(Global)->isDeclaration();
951 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
952 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
954 assert(false && "Unhandled Linkage Type.");
955 case GlobalValue::ExternalLinkage:
956 return LLVMExternalLinkage;
957 case GlobalValue::AvailableExternallyLinkage:
958 return LLVMAvailableExternallyLinkage;
959 case GlobalValue::LinkOnceAnyLinkage:
960 return LLVMLinkOnceAnyLinkage;
961 case GlobalValue::LinkOnceODRLinkage:
962 return LLVMLinkOnceODRLinkage;
963 case GlobalValue::WeakAnyLinkage:
964 return LLVMWeakAnyLinkage;
965 case GlobalValue::WeakODRLinkage:
966 return LLVMWeakODRLinkage;
967 case GlobalValue::AppendingLinkage:
968 return LLVMAppendingLinkage;
969 case GlobalValue::InternalLinkage:
970 return LLVMInternalLinkage;
971 case GlobalValue::PrivateLinkage:
972 return LLVMPrivateLinkage;
973 case GlobalValue::LinkerPrivateLinkage:
974 return LLVMLinkerPrivateLinkage;
975 case GlobalValue::LinkerPrivateWeakLinkage:
976 return LLVMLinkerPrivateWeakLinkage;
977 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
978 return LLVMLinkerPrivateWeakDefAutoLinkage;
979 case GlobalValue::DLLImportLinkage:
980 return LLVMDLLImportLinkage;
981 case GlobalValue::DLLExportLinkage:
982 return LLVMDLLExportLinkage;
983 case GlobalValue::ExternalWeakLinkage:
984 return LLVMExternalWeakLinkage;
985 case GlobalValue::CommonLinkage:
986 return LLVMCommonLinkage;
989 // Should never get here.
990 return static_cast<LLVMLinkage>(0);
993 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
994 GlobalValue *GV = unwrap<GlobalValue>(Global);
998 assert(false && "Unhandled Linkage Type.");
999 case LLVMExternalLinkage:
1000 GV->setLinkage(GlobalValue::ExternalLinkage);
1002 case LLVMAvailableExternallyLinkage:
1003 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1005 case LLVMLinkOnceAnyLinkage:
1006 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1008 case LLVMLinkOnceODRLinkage:
1009 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1011 case LLVMWeakAnyLinkage:
1012 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1014 case LLVMWeakODRLinkage:
1015 GV->setLinkage(GlobalValue::WeakODRLinkage);
1017 case LLVMAppendingLinkage:
1018 GV->setLinkage(GlobalValue::AppendingLinkage);
1020 case LLVMInternalLinkage:
1021 GV->setLinkage(GlobalValue::InternalLinkage);
1023 case LLVMPrivateLinkage:
1024 GV->setLinkage(GlobalValue::PrivateLinkage);
1026 case LLVMLinkerPrivateLinkage:
1027 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1029 case LLVMLinkerPrivateWeakLinkage:
1030 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1032 case LLVMLinkerPrivateWeakDefAutoLinkage:
1033 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1035 case LLVMDLLImportLinkage:
1036 GV->setLinkage(GlobalValue::DLLImportLinkage);
1038 case LLVMDLLExportLinkage:
1039 GV->setLinkage(GlobalValue::DLLExportLinkage);
1041 case LLVMExternalWeakLinkage:
1042 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1044 case LLVMGhostLinkage:
1046 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1048 case LLVMCommonLinkage:
1049 GV->setLinkage(GlobalValue::CommonLinkage);
1054 const char *LLVMGetSection(LLVMValueRef Global) {
1055 return unwrap<GlobalValue>(Global)->getSection().c_str();
1058 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1059 unwrap<GlobalValue>(Global)->setSection(Section);
1062 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1063 return static_cast<LLVMVisibility>(
1064 unwrap<GlobalValue>(Global)->getVisibility());
1067 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1068 unwrap<GlobalValue>(Global)
1069 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1072 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1073 return unwrap<GlobalValue>(Global)->getAlignment();
1076 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1077 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1080 /*--.. Operations on global variables ......................................--*/
1082 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1083 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1084 GlobalValue::ExternalLinkage, 0, Name));
1087 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1089 unsigned AddressSpace) {
1090 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1091 GlobalValue::ExternalLinkage, 0, Name, 0,
1092 false, AddressSpace));
1095 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1096 return wrap(unwrap(M)->getNamedGlobal(Name));
1099 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1100 Module *Mod = unwrap(M);
1101 Module::global_iterator I = Mod->global_begin();
1102 if (I == Mod->global_end())
1107 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1108 Module *Mod = unwrap(M);
1109 Module::global_iterator I = Mod->global_end();
1110 if (I == Mod->global_begin())
1115 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1116 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1117 Module::global_iterator I = GV;
1118 if (++I == GV->getParent()->global_end())
1123 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1124 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1125 Module::global_iterator I = GV;
1126 if (I == GV->getParent()->global_begin())
1131 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1132 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1135 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1136 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1137 if ( !GV->hasInitializer() )
1139 return wrap(GV->getInitializer());
1142 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1143 unwrap<GlobalVariable>(GlobalVar)
1144 ->setInitializer(unwrap<Constant>(ConstantVal));
1147 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1148 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1151 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1152 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1155 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1156 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1159 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1160 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1163 /*--.. Operations on aliases ......................................--*/
1165 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1167 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1168 unwrap<Constant>(Aliasee), unwrap (M)));
1171 /*--.. Operations on functions .............................................--*/
1173 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1174 LLVMTypeRef FunctionTy) {
1175 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1176 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1179 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1180 return wrap(unwrap(M)->getFunction(Name));
1183 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1184 Module *Mod = unwrap(M);
1185 Module::iterator I = Mod->begin();
1186 if (I == Mod->end())
1191 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1192 Module *Mod = unwrap(M);
1193 Module::iterator I = Mod->end();
1194 if (I == Mod->begin())
1199 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1200 Function *Func = unwrap<Function>(Fn);
1201 Module::iterator I = Func;
1202 if (++I == Func->getParent()->end())
1207 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1208 Function *Func = unwrap<Function>(Fn);
1209 Module::iterator I = Func;
1210 if (I == Func->getParent()->begin())
1215 void LLVMDeleteFunction(LLVMValueRef Fn) {
1216 unwrap<Function>(Fn)->eraseFromParent();
1219 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1220 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1221 return F->getIntrinsicID();
1225 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1226 return unwrap<Function>(Fn)->getCallingConv();
1229 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1230 return unwrap<Function>(Fn)->setCallingConv(
1231 static_cast<CallingConv::ID>(CC));
1234 const char *LLVMGetGC(LLVMValueRef Fn) {
1235 Function *F = unwrap<Function>(Fn);
1236 return F->hasGC()? F->getGC() : 0;
1239 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1240 Function *F = unwrap<Function>(Fn);
1247 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1248 Function *Func = unwrap<Function>(Fn);
1249 const AttrListPtr PAL = Func->getAttributes();
1250 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1251 Func->setAttributes(PALnew);
1254 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1255 Function *Func = unwrap<Function>(Fn);
1256 const AttrListPtr PAL = Func->getAttributes();
1257 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1258 Func->setAttributes(PALnew);
1261 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1262 Function *Func = unwrap<Function>(Fn);
1263 const AttrListPtr PAL = Func->getAttributes();
1264 Attributes attr = PAL.getFnAttributes();
1265 return (LLVMAttribute)attr;
1268 /*--.. Operations on parameters ............................................--*/
1270 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1271 // This function is strictly redundant to
1272 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1273 return unwrap<Function>(FnRef)->arg_size();
1276 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1277 Function *Fn = unwrap<Function>(FnRef);
1278 for (Function::arg_iterator I = Fn->arg_begin(),
1279 E = Fn->arg_end(); I != E; I++)
1280 *ParamRefs++ = wrap(I);
1283 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1284 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1290 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1291 return wrap(unwrap<Argument>(V)->getParent());
1294 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1295 Function *Func = unwrap<Function>(Fn);
1296 Function::arg_iterator I = Func->arg_begin();
1297 if (I == Func->arg_end())
1302 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1303 Function *Func = unwrap<Function>(Fn);
1304 Function::arg_iterator I = Func->arg_end();
1305 if (I == Func->arg_begin())
1310 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1311 Argument *A = unwrap<Argument>(Arg);
1312 Function::arg_iterator I = A;
1313 if (++I == A->getParent()->arg_end())
1318 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1319 Argument *A = unwrap<Argument>(Arg);
1320 Function::arg_iterator I = A;
1321 if (I == A->getParent()->arg_begin())
1326 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1327 unwrap<Argument>(Arg)->addAttr(PA);
1330 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1331 unwrap<Argument>(Arg)->removeAttr(PA);
1334 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1335 Argument *A = unwrap<Argument>(Arg);
1336 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1338 return (LLVMAttribute)attr;
1342 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1343 unwrap<Argument>(Arg)->addAttr(
1344 Attribute::constructAlignmentFromInt(align));
1347 /*--.. Operations on basic blocks ..........................................--*/
1349 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1350 return wrap(static_cast<Value*>(unwrap(BB)));
1353 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1354 return isa<BasicBlock>(unwrap(Val));
1357 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1358 return wrap(unwrap<BasicBlock>(Val));
1361 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1362 return wrap(unwrap(BB)->getParent());
1365 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1366 return unwrap<Function>(FnRef)->size();
1369 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1370 Function *Fn = unwrap<Function>(FnRef);
1371 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1372 *BasicBlocksRefs++ = wrap(I);
1375 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1376 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1379 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1380 Function *Func = unwrap<Function>(Fn);
1381 Function::iterator I = Func->begin();
1382 if (I == Func->end())
1387 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1388 Function *Func = unwrap<Function>(Fn);
1389 Function::iterator I = Func->end();
1390 if (I == Func->begin())
1395 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1396 BasicBlock *Block = unwrap(BB);
1397 Function::iterator I = Block;
1398 if (++I == Block->getParent()->end())
1403 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1404 BasicBlock *Block = unwrap(BB);
1405 Function::iterator I = Block;
1406 if (I == Block->getParent()->begin())
1411 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1414 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1417 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1418 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1421 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1422 LLVMBasicBlockRef BBRef,
1424 BasicBlock *BB = unwrap(BBRef);
1425 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1428 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1430 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1433 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1434 unwrap(BBRef)->eraseFromParent();
1437 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1438 unwrap(BB)->moveBefore(unwrap(MovePos));
1441 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1442 unwrap(BB)->moveAfter(unwrap(MovePos));
1445 /*--.. Operations on instructions ..........................................--*/
1447 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1448 return wrap(unwrap<Instruction>(Inst)->getParent());
1451 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1452 BasicBlock *Block = unwrap(BB);
1453 BasicBlock::iterator I = Block->begin();
1454 if (I == Block->end())
1459 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1460 BasicBlock *Block = unwrap(BB);
1461 BasicBlock::iterator I = Block->end();
1462 if (I == Block->begin())
1467 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1468 Instruction *Instr = unwrap<Instruction>(Inst);
1469 BasicBlock::iterator I = Instr;
1470 if (++I == Instr->getParent()->end())
1475 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1476 Instruction *Instr = unwrap<Instruction>(Inst);
1477 BasicBlock::iterator I = Instr;
1478 if (I == Instr->getParent()->begin())
1483 /*--.. Call and invoke instructions ........................................--*/
1485 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1486 Value *V = unwrap(Instr);
1487 if (CallInst *CI = dyn_cast<CallInst>(V))
1488 return CI->getCallingConv();
1489 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1490 return II->getCallingConv();
1491 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1495 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1496 Value *V = unwrap(Instr);
1497 if (CallInst *CI = dyn_cast<CallInst>(V))
1498 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1499 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1500 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1501 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1504 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1506 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1508 Call.getAttributes().addAttr(index, PA));
1511 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1513 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1515 Call.getAttributes().removeAttr(index, PA));
1518 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1520 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1522 Call.getAttributes().addAttr(index,
1523 Attribute::constructAlignmentFromInt(align)));
1526 /*--.. Operations on call instructions (only) ..............................--*/
1528 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1529 return unwrap<CallInst>(Call)->isTailCall();
1532 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1533 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1536 /*--.. Operations on phi nodes .............................................--*/
1538 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1539 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1540 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1541 for (unsigned I = 0; I != Count; ++I)
1542 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1545 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1546 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1549 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1550 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1553 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1554 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1558 /*===-- Instruction builders ----------------------------------------------===*/
1560 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1561 return wrap(new IRBuilder<>(*unwrap(C)));
1564 LLVMBuilderRef LLVMCreateBuilder(void) {
1565 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1568 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1569 LLVMValueRef Instr) {
1570 BasicBlock *BB = unwrap(Block);
1571 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1572 unwrap(Builder)->SetInsertPoint(BB, I);
1575 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1576 Instruction *I = unwrap<Instruction>(Instr);
1577 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1580 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1581 BasicBlock *BB = unwrap(Block);
1582 unwrap(Builder)->SetInsertPoint(BB);
1585 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1586 return wrap(unwrap(Builder)->GetInsertBlock());
1589 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1590 unwrap(Builder)->ClearInsertionPoint();
1593 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1594 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1597 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1599 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1602 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1603 delete unwrap(Builder);
1606 /*--.. Metadata builders ...................................................--*/
1608 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1609 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1610 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1613 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1614 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1615 .getAsMDNode(unwrap(Builder)->getContext()));
1618 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1619 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1623 /*--.. Instruction builders ................................................--*/
1625 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1626 return wrap(unwrap(B)->CreateRetVoid());
1629 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1630 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1633 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1635 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1638 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1639 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1642 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1643 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1644 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1647 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1648 LLVMBasicBlockRef Else, unsigned NumCases) {
1649 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1652 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1653 unsigned NumDests) {
1654 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1657 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1658 LLVMValueRef *Args, unsigned NumArgs,
1659 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1661 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1662 unwrap(Args), unwrap(Args) + NumArgs,
1666 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1667 return wrap(unwrap(B)->CreateUnwind());
1670 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1671 return wrap(unwrap(B)->CreateUnreachable());
1674 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1675 LLVMBasicBlockRef Dest) {
1676 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1679 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1680 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1683 /*--.. Arithmetic ..........................................................--*/
1685 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1687 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1690 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1692 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1695 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1697 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1700 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1702 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1705 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1707 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1710 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1712 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1715 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1717 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1720 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1722 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1725 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1727 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1730 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1732 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1735 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1737 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1740 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1742 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1745 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1747 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1750 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1752 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1755 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1756 LLVMValueRef RHS, const char *Name) {
1757 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1760 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1762 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1765 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1767 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1770 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1772 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1775 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1777 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1780 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1782 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1785 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1787 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1790 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1792 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1795 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1797 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1800 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1802 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1805 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1807 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1810 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1811 LLVMValueRef LHS, LLVMValueRef RHS,
1813 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1814 unwrap(RHS), Name));
1817 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1818 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1821 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1823 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1826 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1828 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1831 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1832 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1835 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1836 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1839 /*--.. Memory ..............................................................--*/
1841 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1843 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1844 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1845 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1846 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1847 ITy, unwrap(Ty), AllocSize,
1849 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1852 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1853 LLVMValueRef Val, const char *Name) {
1854 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1855 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1856 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1857 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1858 ITy, unwrap(Ty), AllocSize,
1859 unwrap(Val), 0, "");
1860 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1863 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1865 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1868 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1869 LLVMValueRef Val, const char *Name) {
1870 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1873 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1874 return wrap(unwrap(B)->Insert(
1875 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1879 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1881 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1884 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1885 LLVMValueRef PointerVal) {
1886 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1889 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1890 LLVMValueRef *Indices, unsigned NumIndices,
1892 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1893 unwrap(Indices) + NumIndices, Name));
1896 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1897 LLVMValueRef *Indices, unsigned NumIndices,
1899 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1900 unwrap(Indices) + NumIndices, Name));
1903 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1904 unsigned Idx, const char *Name) {
1905 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1908 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1910 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1913 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1915 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1918 /*--.. Casts ...............................................................--*/
1920 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1921 LLVMTypeRef DestTy, const char *Name) {
1922 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1925 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1926 LLVMTypeRef DestTy, const char *Name) {
1927 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1930 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1931 LLVMTypeRef DestTy, const char *Name) {
1932 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1935 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1936 LLVMTypeRef DestTy, const char *Name) {
1937 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1940 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1941 LLVMTypeRef DestTy, const char *Name) {
1942 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1945 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1946 LLVMTypeRef DestTy, const char *Name) {
1947 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1950 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1951 LLVMTypeRef DestTy, const char *Name) {
1952 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1955 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1956 LLVMTypeRef DestTy, const char *Name) {
1957 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1960 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1961 LLVMTypeRef DestTy, const char *Name) {
1962 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1965 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1966 LLVMTypeRef DestTy, const char *Name) {
1967 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1970 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1971 LLVMTypeRef DestTy, const char *Name) {
1972 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1975 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1976 LLVMTypeRef DestTy, const char *Name) {
1977 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1980 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1981 LLVMTypeRef DestTy, const char *Name) {
1982 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1986 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1987 LLVMTypeRef DestTy, const char *Name) {
1988 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1992 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1993 LLVMTypeRef DestTy, const char *Name) {
1994 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1998 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
1999 LLVMTypeRef DestTy, const char *Name) {
2000 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2001 unwrap(DestTy), Name));
2004 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2005 LLVMTypeRef DestTy, const char *Name) {
2006 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2009 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2010 LLVMTypeRef DestTy, const char *Name) {
2011 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2012 /*isSigned*/true, Name));
2015 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2016 LLVMTypeRef DestTy, const char *Name) {
2017 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2020 /*--.. Comparisons .........................................................--*/
2022 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2023 LLVMValueRef LHS, LLVMValueRef RHS,
2025 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2026 unwrap(LHS), unwrap(RHS), Name));
2029 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2030 LLVMValueRef LHS, LLVMValueRef RHS,
2032 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2033 unwrap(LHS), unwrap(RHS), Name));
2036 /*--.. Miscellaneous instructions ..........................................--*/
2038 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2039 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2042 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2043 LLVMValueRef *Args, unsigned NumArgs,
2045 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2046 unwrap(Args) + NumArgs, Name));
2049 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2050 LLVMValueRef Then, LLVMValueRef Else,
2052 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2056 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2057 LLVMTypeRef Ty, const char *Name) {
2058 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2061 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2062 LLVMValueRef Index, const char *Name) {
2063 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2067 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2068 LLVMValueRef EltVal, LLVMValueRef Index,
2070 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2071 unwrap(Index), Name));
2074 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2075 LLVMValueRef V2, LLVMValueRef Mask,
2077 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2078 unwrap(Mask), Name));
2081 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2082 unsigned Index, const char *Name) {
2083 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2086 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2087 LLVMValueRef EltVal, unsigned Index,
2089 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2093 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2095 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2098 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2100 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2103 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2104 LLVMValueRef RHS, const char *Name) {
2105 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2109 /*===-- Module providers --------------------------------------------------===*/
2111 LLVMModuleProviderRef
2112 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2113 return reinterpret_cast<LLVMModuleProviderRef>(M);
2116 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2121 /*===-- Memory buffers ----------------------------------------------------===*/
2123 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2125 LLVMMemoryBufferRef *OutMemBuf,
2126 char **OutMessage) {
2128 OwningPtr<MemoryBuffer> MB;
2130 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2131 *OutMemBuf = wrap(MB.take());
2135 *OutMessage = strdup(ec.message().c_str());
2139 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2140 char **OutMessage) {
2141 OwningPtr<MemoryBuffer> MB;
2143 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2144 *OutMemBuf = wrap(MB.take());
2148 *OutMessage = strdup(ec.message().c_str());
2152 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2153 delete unwrap(MemBuf);
2156 /*===-- Pass Registry -----------------------------------------------------===*/
2158 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2159 return wrap(PassRegistry::getPassRegistry());
2162 /*===-- Pass Manager ------------------------------------------------------===*/
2164 LLVMPassManagerRef LLVMCreatePassManager() {
2165 return wrap(new PassManager());
2168 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2169 return wrap(new FunctionPassManager(unwrap(M)));
2172 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2173 return LLVMCreateFunctionPassManagerForModule(
2174 reinterpret_cast<LLVMModuleRef>(P));
2177 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2178 return unwrap<PassManager>(PM)->run(*unwrap(M));
2181 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2182 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2185 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2186 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2189 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2190 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2193 void LLVMDisposePassManager(LLVMPassManagerRef PM) {