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 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
264 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
267 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
268 return unwrap<FunctionType>(FunctionTy)->isVarArg();
271 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
272 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
275 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
276 return unwrap<FunctionType>(FunctionTy)->getNumParams();
279 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
280 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
281 for (FunctionType::param_iterator I = Ty->param_begin(),
282 E = Ty->param_end(); I != E; ++I)
286 /*--.. Operations on struct types ..........................................--*/
288 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
289 unsigned ElementCount, LLVMBool Packed) {
290 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
291 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
294 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
295 unsigned ElementCount, LLVMBool Packed) {
296 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
297 ElementCount, Packed);
300 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
302 return wrap(StructType::createNamed(*unwrap(C), Name));
305 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
306 unsigned ElementCount, LLVMBool Packed) {
307 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
308 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
311 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
312 return unwrap<StructType>(StructTy)->getNumElements();
315 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
316 StructType *Ty = unwrap<StructType>(StructTy);
317 for (StructType::element_iterator I = Ty->element_begin(),
318 E = Ty->element_end(); I != E; ++I)
322 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
323 return unwrap<StructType>(StructTy)->isPacked();
326 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
327 return unwrap<StructType>(StructTy)->isOpaque();
330 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
331 return wrap(unwrap(M)->getTypeByName(Name));
334 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
336 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
337 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
340 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
341 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
344 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
345 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
348 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
349 return wrap(unwrap<SequentialType>(Ty)->getElementType());
352 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
353 return unwrap<ArrayType>(ArrayTy)->getNumElements();
356 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
357 return unwrap<PointerType>(PointerTy)->getAddressSpace();
360 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
361 return unwrap<VectorType>(VectorTy)->getNumElements();
364 /*--.. Operations on other types ...........................................--*/
366 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
367 return wrap(Type::getVoidTy(*unwrap(C)));
369 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
370 return wrap(Type::getLabelTy(*unwrap(C)));
373 LLVMTypeRef LLVMVoidType(void) {
374 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
376 LLVMTypeRef LLVMLabelType(void) {
377 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
380 /*===-- Operations on values ----------------------------------------------===*/
382 /*--.. Operations on all values ............................................--*/
384 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
385 return wrap(unwrap(Val)->getType());
388 const char *LLVMGetValueName(LLVMValueRef Val) {
389 return unwrap(Val)->getName().data();
392 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
393 unwrap(Val)->setName(Name);
396 void LLVMDumpValue(LLVMValueRef Val) {
400 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
401 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
404 int LLVMHasMetadata(LLVMValueRef Inst) {
405 return unwrap<Instruction>(Inst)->hasMetadata();
408 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
409 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
412 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
413 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
416 /*--.. Conversion functions ................................................--*/
418 #define LLVM_DEFINE_VALUE_CAST(name) \
419 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
420 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
423 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
425 /*--.. Operations on Uses ..................................................--*/
426 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
427 Value *V = unwrap(Val);
428 Value::use_iterator I = V->use_begin();
429 if (I == V->use_end())
431 return wrap(&(I.getUse()));
434 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
435 Use *Next = unwrap(U)->getNext();
441 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
442 return wrap(unwrap(U)->getUser());
445 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
446 return wrap(unwrap(U)->get());
449 /*--.. Operations on Users .................................................--*/
450 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
451 return wrap(unwrap<User>(Val)->getOperand(Index));
454 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
455 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
458 int LLVMGetNumOperands(LLVMValueRef Val) {
459 return unwrap<User>(Val)->getNumOperands();
462 /*--.. Operations on constants of any type .................................--*/
464 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
465 return wrap(Constant::getNullValue(unwrap(Ty)));
468 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
469 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
472 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
473 return wrap(UndefValue::get(unwrap(Ty)));
476 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
477 return isa<Constant>(unwrap(Ty));
480 LLVMBool LLVMIsNull(LLVMValueRef Val) {
481 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
482 return C->isNullValue();
486 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
487 return isa<UndefValue>(unwrap(Val));
490 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
492 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
495 /*--.. Operations on metadata nodes ........................................--*/
497 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
499 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
502 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
503 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
506 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
508 return wrap(MDNode::get(*unwrap(C),
509 ArrayRef<Value*>(unwrap<Value>(Vals, Count), Count)));
512 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
513 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
516 /*--.. Operations on scalar constants ......................................--*/
518 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
519 LLVMBool SignExtend) {
520 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
523 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
525 const uint64_t Words[]) {
526 IntegerType *Ty = unwrap<IntegerType>(IntTy);
527 return wrap(ConstantInt::get(Ty->getContext(),
528 APInt(Ty->getBitWidth(), NumWords, Words)));
531 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
533 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
537 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
538 unsigned SLen, uint8_t Radix) {
539 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
543 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
544 return wrap(ConstantFP::get(unwrap(RealTy), N));
547 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
548 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
551 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
553 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
556 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
557 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
560 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
561 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
564 /*--.. Operations on composite constants ...................................--*/
566 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
568 LLVMBool DontNullTerminate) {
569 /* Inverted the sense of AddNull because ', 0)' is a
570 better mnemonic for null termination than ', 1)'. */
571 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
572 DontNullTerminate == 0));
574 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
575 LLVMValueRef *ConstantVals,
576 unsigned Count, LLVMBool Packed) {
577 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
578 return wrap(ConstantStruct::getAnon(*unwrap(C),
579 ArrayRef<Constant*>(Elements, Count),
583 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
584 LLVMBool DontNullTerminate) {
585 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
588 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
589 LLVMValueRef *ConstantVals, unsigned Length) {
590 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
591 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
593 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
595 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
599 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
600 LLVMValueRef *ConstantVals,
602 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
603 const StructType *Ty = cast<StructType>(unwrap(StructTy));
605 return wrap(ConstantStruct::get(Ty, ArrayRef<Constant*>(Elements, Count)));
608 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
609 return wrap(ConstantVector::get(ArrayRef<Constant*>(
610 unwrap<Constant>(ScalarConstantVals, Size), Size)));
612 /*--.. Constant expressions ................................................--*/
614 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
615 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
618 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
619 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
622 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
623 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
626 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
627 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
630 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
631 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
634 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
635 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
639 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
640 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
643 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
644 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
647 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
648 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
649 unwrap<Constant>(RHSConstant)));
652 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
653 LLVMValueRef RHSConstant) {
654 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
655 unwrap<Constant>(RHSConstant)));
658 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
659 LLVMValueRef RHSConstant) {
660 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
661 unwrap<Constant>(RHSConstant)));
664 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
665 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
666 unwrap<Constant>(RHSConstant)));
669 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
670 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
671 unwrap<Constant>(RHSConstant)));
674 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
675 LLVMValueRef RHSConstant) {
676 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
677 unwrap<Constant>(RHSConstant)));
680 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
681 LLVMValueRef RHSConstant) {
682 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
683 unwrap<Constant>(RHSConstant)));
686 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
687 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
688 unwrap<Constant>(RHSConstant)));
691 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
692 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
693 unwrap<Constant>(RHSConstant)));
696 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
697 LLVMValueRef RHSConstant) {
698 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
699 unwrap<Constant>(RHSConstant)));
702 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
703 LLVMValueRef RHSConstant) {
704 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
705 unwrap<Constant>(RHSConstant)));
708 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
709 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
710 unwrap<Constant>(RHSConstant)));
713 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
714 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
715 unwrap<Constant>(RHSConstant)));
718 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
719 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
720 unwrap<Constant>(RHSConstant)));
723 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
724 LLVMValueRef RHSConstant) {
725 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
726 unwrap<Constant>(RHSConstant)));
729 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
730 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
731 unwrap<Constant>(RHSConstant)));
734 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
735 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
736 unwrap<Constant>(RHSConstant)));
739 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
740 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
741 unwrap<Constant>(RHSConstant)));
744 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
745 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
746 unwrap<Constant>(RHSConstant)));
749 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
750 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
751 unwrap<Constant>(RHSConstant)));
754 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
755 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
756 unwrap<Constant>(RHSConstant)));
759 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
760 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
761 unwrap<Constant>(RHSConstant)));
764 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
765 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
766 return wrap(ConstantExpr::getICmp(Predicate,
767 unwrap<Constant>(LHSConstant),
768 unwrap<Constant>(RHSConstant)));
771 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
772 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
773 return wrap(ConstantExpr::getFCmp(Predicate,
774 unwrap<Constant>(LHSConstant),
775 unwrap<Constant>(RHSConstant)));
778 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
779 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
780 unwrap<Constant>(RHSConstant)));
783 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
784 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
785 unwrap<Constant>(RHSConstant)));
788 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
789 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
790 unwrap<Constant>(RHSConstant)));
793 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
794 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
795 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
796 unwrap<Constant>(ConstantIndices,
801 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
802 LLVMValueRef *ConstantIndices,
803 unsigned NumIndices) {
804 Constant* Val = unwrap<Constant>(ConstantVal);
805 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
806 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
809 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
810 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
814 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
815 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
819 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
820 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
824 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
825 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
829 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
830 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
834 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
835 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
839 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
840 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
844 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
845 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
849 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
850 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
854 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
855 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
859 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
860 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
864 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
865 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
869 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
870 LLVMTypeRef ToType) {
871 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
875 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
876 LLVMTypeRef ToType) {
877 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
881 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
882 LLVMTypeRef ToType) {
883 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
887 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
888 LLVMTypeRef ToType) {
889 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
893 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
895 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
896 unwrap(ToType), isSigned));
899 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
900 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
904 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
905 LLVMValueRef ConstantIfTrue,
906 LLVMValueRef ConstantIfFalse) {
907 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
908 unwrap<Constant>(ConstantIfTrue),
909 unwrap<Constant>(ConstantIfFalse)));
912 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
913 LLVMValueRef IndexConstant) {
914 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
915 unwrap<Constant>(IndexConstant)));
918 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
919 LLVMValueRef ElementValueConstant,
920 LLVMValueRef IndexConstant) {
921 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
922 unwrap<Constant>(ElementValueConstant),
923 unwrap<Constant>(IndexConstant)));
926 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
927 LLVMValueRef VectorBConstant,
928 LLVMValueRef MaskConstant) {
929 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
930 unwrap<Constant>(VectorBConstant),
931 unwrap<Constant>(MaskConstant)));
934 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
936 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
937 ArrayRef<unsigned>(IdxList,
941 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
942 LLVMValueRef ElementValueConstant,
943 unsigned *IdxList, unsigned NumIdx) {
944 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
945 unwrap<Constant>(ElementValueConstant),
946 ArrayRef<unsigned>(IdxList,
950 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
951 const char *Constraints,
952 LLVMBool HasSideEffects,
953 LLVMBool IsAlignStack) {
954 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
955 Constraints, HasSideEffects, IsAlignStack));
958 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
959 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
962 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
964 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
965 return wrap(unwrap<GlobalValue>(Global)->getParent());
968 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
969 return unwrap<GlobalValue>(Global)->isDeclaration();
972 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
973 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
975 assert(false && "Unhandled Linkage Type.");
976 case GlobalValue::ExternalLinkage:
977 return LLVMExternalLinkage;
978 case GlobalValue::AvailableExternallyLinkage:
979 return LLVMAvailableExternallyLinkage;
980 case GlobalValue::LinkOnceAnyLinkage:
981 return LLVMLinkOnceAnyLinkage;
982 case GlobalValue::LinkOnceODRLinkage:
983 return LLVMLinkOnceODRLinkage;
984 case GlobalValue::WeakAnyLinkage:
985 return LLVMWeakAnyLinkage;
986 case GlobalValue::WeakODRLinkage:
987 return LLVMWeakODRLinkage;
988 case GlobalValue::AppendingLinkage:
989 return LLVMAppendingLinkage;
990 case GlobalValue::InternalLinkage:
991 return LLVMInternalLinkage;
992 case GlobalValue::PrivateLinkage:
993 return LLVMPrivateLinkage;
994 case GlobalValue::LinkerPrivateLinkage:
995 return LLVMLinkerPrivateLinkage;
996 case GlobalValue::LinkerPrivateWeakLinkage:
997 return LLVMLinkerPrivateWeakLinkage;
998 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
999 return LLVMLinkerPrivateWeakDefAutoLinkage;
1000 case GlobalValue::DLLImportLinkage:
1001 return LLVMDLLImportLinkage;
1002 case GlobalValue::DLLExportLinkage:
1003 return LLVMDLLExportLinkage;
1004 case GlobalValue::ExternalWeakLinkage:
1005 return LLVMExternalWeakLinkage;
1006 case GlobalValue::CommonLinkage:
1007 return LLVMCommonLinkage;
1010 // Should never get here.
1011 return static_cast<LLVMLinkage>(0);
1014 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1015 GlobalValue *GV = unwrap<GlobalValue>(Global);
1019 assert(false && "Unhandled Linkage Type.");
1020 case LLVMExternalLinkage:
1021 GV->setLinkage(GlobalValue::ExternalLinkage);
1023 case LLVMAvailableExternallyLinkage:
1024 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1026 case LLVMLinkOnceAnyLinkage:
1027 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1029 case LLVMLinkOnceODRLinkage:
1030 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1032 case LLVMWeakAnyLinkage:
1033 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1035 case LLVMWeakODRLinkage:
1036 GV->setLinkage(GlobalValue::WeakODRLinkage);
1038 case LLVMAppendingLinkage:
1039 GV->setLinkage(GlobalValue::AppendingLinkage);
1041 case LLVMInternalLinkage:
1042 GV->setLinkage(GlobalValue::InternalLinkage);
1044 case LLVMPrivateLinkage:
1045 GV->setLinkage(GlobalValue::PrivateLinkage);
1047 case LLVMLinkerPrivateLinkage:
1048 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1050 case LLVMLinkerPrivateWeakLinkage:
1051 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1053 case LLVMLinkerPrivateWeakDefAutoLinkage:
1054 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1056 case LLVMDLLImportLinkage:
1057 GV->setLinkage(GlobalValue::DLLImportLinkage);
1059 case LLVMDLLExportLinkage:
1060 GV->setLinkage(GlobalValue::DLLExportLinkage);
1062 case LLVMExternalWeakLinkage:
1063 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1065 case LLVMGhostLinkage:
1067 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1069 case LLVMCommonLinkage:
1070 GV->setLinkage(GlobalValue::CommonLinkage);
1075 const char *LLVMGetSection(LLVMValueRef Global) {
1076 return unwrap<GlobalValue>(Global)->getSection().c_str();
1079 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1080 unwrap<GlobalValue>(Global)->setSection(Section);
1083 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1084 return static_cast<LLVMVisibility>(
1085 unwrap<GlobalValue>(Global)->getVisibility());
1088 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1089 unwrap<GlobalValue>(Global)
1090 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1093 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1094 return unwrap<GlobalValue>(Global)->getAlignment();
1097 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1098 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1101 /*--.. Operations on global variables ......................................--*/
1103 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1104 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1105 GlobalValue::ExternalLinkage, 0, Name));
1108 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1110 unsigned AddressSpace) {
1111 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1112 GlobalValue::ExternalLinkage, 0, Name, 0,
1113 false, AddressSpace));
1116 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1117 return wrap(unwrap(M)->getNamedGlobal(Name));
1120 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1121 Module *Mod = unwrap(M);
1122 Module::global_iterator I = Mod->global_begin();
1123 if (I == Mod->global_end())
1128 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1129 Module *Mod = unwrap(M);
1130 Module::global_iterator I = Mod->global_end();
1131 if (I == Mod->global_begin())
1136 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1137 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1138 Module::global_iterator I = GV;
1139 if (++I == GV->getParent()->global_end())
1144 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1145 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1146 Module::global_iterator I = GV;
1147 if (I == GV->getParent()->global_begin())
1152 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1153 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1156 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1157 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1158 if ( !GV->hasInitializer() )
1160 return wrap(GV->getInitializer());
1163 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1164 unwrap<GlobalVariable>(GlobalVar)
1165 ->setInitializer(unwrap<Constant>(ConstantVal));
1168 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1169 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1172 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1173 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1176 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1177 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1180 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1181 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1184 /*--.. Operations on aliases ......................................--*/
1186 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1188 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1189 unwrap<Constant>(Aliasee), unwrap (M)));
1192 /*--.. Operations on functions .............................................--*/
1194 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1195 LLVMTypeRef FunctionTy) {
1196 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1197 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1200 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1201 return wrap(unwrap(M)->getFunction(Name));
1204 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1205 Module *Mod = unwrap(M);
1206 Module::iterator I = Mod->begin();
1207 if (I == Mod->end())
1212 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1213 Module *Mod = unwrap(M);
1214 Module::iterator I = Mod->end();
1215 if (I == Mod->begin())
1220 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1221 Function *Func = unwrap<Function>(Fn);
1222 Module::iterator I = Func;
1223 if (++I == Func->getParent()->end())
1228 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1229 Function *Func = unwrap<Function>(Fn);
1230 Module::iterator I = Func;
1231 if (I == Func->getParent()->begin())
1236 void LLVMDeleteFunction(LLVMValueRef Fn) {
1237 unwrap<Function>(Fn)->eraseFromParent();
1240 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1241 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1242 return F->getIntrinsicID();
1246 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1247 return unwrap<Function>(Fn)->getCallingConv();
1250 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1251 return unwrap<Function>(Fn)->setCallingConv(
1252 static_cast<CallingConv::ID>(CC));
1255 const char *LLVMGetGC(LLVMValueRef Fn) {
1256 Function *F = unwrap<Function>(Fn);
1257 return F->hasGC()? F->getGC() : 0;
1260 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1261 Function *F = unwrap<Function>(Fn);
1268 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1269 Function *Func = unwrap<Function>(Fn);
1270 const AttrListPtr PAL = Func->getAttributes();
1271 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1272 Func->setAttributes(PALnew);
1275 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1276 Function *Func = unwrap<Function>(Fn);
1277 const AttrListPtr PAL = Func->getAttributes();
1278 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1279 Func->setAttributes(PALnew);
1282 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1283 Function *Func = unwrap<Function>(Fn);
1284 const AttrListPtr PAL = Func->getAttributes();
1285 Attributes attr = PAL.getFnAttributes();
1286 return (LLVMAttribute)attr;
1289 /*--.. Operations on parameters ............................................--*/
1291 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1292 // This function is strictly redundant to
1293 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1294 return unwrap<Function>(FnRef)->arg_size();
1297 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1298 Function *Fn = unwrap<Function>(FnRef);
1299 for (Function::arg_iterator I = Fn->arg_begin(),
1300 E = Fn->arg_end(); I != E; I++)
1301 *ParamRefs++ = wrap(I);
1304 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1305 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1311 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1312 return wrap(unwrap<Argument>(V)->getParent());
1315 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1316 Function *Func = unwrap<Function>(Fn);
1317 Function::arg_iterator I = Func->arg_begin();
1318 if (I == Func->arg_end())
1323 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1324 Function *Func = unwrap<Function>(Fn);
1325 Function::arg_iterator I = Func->arg_end();
1326 if (I == Func->arg_begin())
1331 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1332 Argument *A = unwrap<Argument>(Arg);
1333 Function::arg_iterator I = A;
1334 if (++I == A->getParent()->arg_end())
1339 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1340 Argument *A = unwrap<Argument>(Arg);
1341 Function::arg_iterator I = A;
1342 if (I == A->getParent()->arg_begin())
1347 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1348 unwrap<Argument>(Arg)->addAttr(PA);
1351 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1352 unwrap<Argument>(Arg)->removeAttr(PA);
1355 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1356 Argument *A = unwrap<Argument>(Arg);
1357 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1359 return (LLVMAttribute)attr;
1363 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1364 unwrap<Argument>(Arg)->addAttr(
1365 Attribute::constructAlignmentFromInt(align));
1368 /*--.. Operations on basic blocks ..........................................--*/
1370 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1371 return wrap(static_cast<Value*>(unwrap(BB)));
1374 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1375 return isa<BasicBlock>(unwrap(Val));
1378 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1379 return wrap(unwrap<BasicBlock>(Val));
1382 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1383 return wrap(unwrap(BB)->getParent());
1386 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1387 return unwrap<Function>(FnRef)->size();
1390 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1391 Function *Fn = unwrap<Function>(FnRef);
1392 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1393 *BasicBlocksRefs++ = wrap(I);
1396 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1397 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1400 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1401 Function *Func = unwrap<Function>(Fn);
1402 Function::iterator I = Func->begin();
1403 if (I == Func->end())
1408 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1409 Function *Func = unwrap<Function>(Fn);
1410 Function::iterator I = Func->end();
1411 if (I == Func->begin())
1416 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1417 BasicBlock *Block = unwrap(BB);
1418 Function::iterator I = Block;
1419 if (++I == Block->getParent()->end())
1424 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1425 BasicBlock *Block = unwrap(BB);
1426 Function::iterator I = Block;
1427 if (I == Block->getParent()->begin())
1432 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1435 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1438 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1439 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1442 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1443 LLVMBasicBlockRef BBRef,
1445 BasicBlock *BB = unwrap(BBRef);
1446 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1449 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1451 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1454 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1455 unwrap(BBRef)->eraseFromParent();
1458 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1459 unwrap(BB)->moveBefore(unwrap(MovePos));
1462 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1463 unwrap(BB)->moveAfter(unwrap(MovePos));
1466 /*--.. Operations on instructions ..........................................--*/
1468 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1469 return wrap(unwrap<Instruction>(Inst)->getParent());
1472 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1473 BasicBlock *Block = unwrap(BB);
1474 BasicBlock::iterator I = Block->begin();
1475 if (I == Block->end())
1480 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1481 BasicBlock *Block = unwrap(BB);
1482 BasicBlock::iterator I = Block->end();
1483 if (I == Block->begin())
1488 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1489 Instruction *Instr = unwrap<Instruction>(Inst);
1490 BasicBlock::iterator I = Instr;
1491 if (++I == Instr->getParent()->end())
1496 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1497 Instruction *Instr = unwrap<Instruction>(Inst);
1498 BasicBlock::iterator I = Instr;
1499 if (I == Instr->getParent()->begin())
1504 /*--.. Call and invoke instructions ........................................--*/
1506 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1507 Value *V = unwrap(Instr);
1508 if (CallInst *CI = dyn_cast<CallInst>(V))
1509 return CI->getCallingConv();
1510 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1511 return II->getCallingConv();
1512 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1516 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1517 Value *V = unwrap(Instr);
1518 if (CallInst *CI = dyn_cast<CallInst>(V))
1519 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1520 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1521 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1522 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1525 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1527 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1529 Call.getAttributes().addAttr(index, PA));
1532 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1534 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1536 Call.getAttributes().removeAttr(index, PA));
1539 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1541 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1543 Call.getAttributes().addAttr(index,
1544 Attribute::constructAlignmentFromInt(align)));
1547 /*--.. Operations on call instructions (only) ..............................--*/
1549 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1550 return unwrap<CallInst>(Call)->isTailCall();
1553 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1554 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1557 /*--.. Operations on phi nodes .............................................--*/
1559 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1560 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1561 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1562 for (unsigned I = 0; I != Count; ++I)
1563 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1566 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1567 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1570 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1571 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1574 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1575 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1579 /*===-- Instruction builders ----------------------------------------------===*/
1581 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1582 return wrap(new IRBuilder<>(*unwrap(C)));
1585 LLVMBuilderRef LLVMCreateBuilder(void) {
1586 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1589 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1590 LLVMValueRef Instr) {
1591 BasicBlock *BB = unwrap(Block);
1592 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1593 unwrap(Builder)->SetInsertPoint(BB, I);
1596 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1597 Instruction *I = unwrap<Instruction>(Instr);
1598 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1601 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1602 BasicBlock *BB = unwrap(Block);
1603 unwrap(Builder)->SetInsertPoint(BB);
1606 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1607 return wrap(unwrap(Builder)->GetInsertBlock());
1610 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1611 unwrap(Builder)->ClearInsertionPoint();
1614 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1615 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1618 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1620 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1623 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1624 delete unwrap(Builder);
1627 /*--.. Metadata builders ...................................................--*/
1629 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1630 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1631 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1634 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1635 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1636 .getAsMDNode(unwrap(Builder)->getContext()));
1639 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1640 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1644 /*--.. Instruction builders ................................................--*/
1646 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1647 return wrap(unwrap(B)->CreateRetVoid());
1650 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1651 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1654 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1656 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1659 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1660 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1663 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1664 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1665 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1668 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1669 LLVMBasicBlockRef Else, unsigned NumCases) {
1670 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1673 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1674 unsigned NumDests) {
1675 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1678 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1679 LLVMValueRef *Args, unsigned NumArgs,
1680 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1682 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1683 unwrap(Args), unwrap(Args) + NumArgs,
1687 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1688 return wrap(unwrap(B)->CreateUnwind());
1691 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1692 return wrap(unwrap(B)->CreateUnreachable());
1695 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1696 LLVMBasicBlockRef Dest) {
1697 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1700 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1701 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1704 /*--.. Arithmetic ..........................................................--*/
1706 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1708 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1711 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1713 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1716 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1718 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1721 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1723 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1726 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1728 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1731 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1733 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1736 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1738 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1741 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1743 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1746 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1748 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1751 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1753 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1756 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1758 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1761 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1763 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1766 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1768 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1771 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1773 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1776 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1777 LLVMValueRef RHS, const char *Name) {
1778 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1781 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1783 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1786 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1788 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1791 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1793 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1796 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1798 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1801 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1803 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1806 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1808 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1811 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1813 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1816 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1818 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1821 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1823 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1826 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1828 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1831 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1832 LLVMValueRef LHS, LLVMValueRef RHS,
1834 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1835 unwrap(RHS), Name));
1838 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1839 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1842 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1844 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1847 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1849 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1852 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1853 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1856 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1857 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1860 /*--.. Memory ..............................................................--*/
1862 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1864 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1865 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1866 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1867 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1868 ITy, unwrap(Ty), AllocSize,
1870 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1873 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1874 LLVMValueRef Val, const char *Name) {
1875 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1876 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1877 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1878 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1879 ITy, unwrap(Ty), AllocSize,
1880 unwrap(Val), 0, "");
1881 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1884 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1886 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1889 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1890 LLVMValueRef Val, const char *Name) {
1891 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1894 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1895 return wrap(unwrap(B)->Insert(
1896 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1900 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1902 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1905 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1906 LLVMValueRef PointerVal) {
1907 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1910 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1911 LLVMValueRef *Indices, unsigned NumIndices,
1913 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1914 unwrap(Indices) + NumIndices, Name));
1917 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1918 LLVMValueRef *Indices, unsigned NumIndices,
1920 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1921 unwrap(Indices) + NumIndices, Name));
1924 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1925 unsigned Idx, const char *Name) {
1926 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1929 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1931 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1934 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1936 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1939 /*--.. Casts ...............................................................--*/
1941 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1942 LLVMTypeRef DestTy, const char *Name) {
1943 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1946 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1947 LLVMTypeRef DestTy, const char *Name) {
1948 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1951 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1952 LLVMTypeRef DestTy, const char *Name) {
1953 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1956 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1957 LLVMTypeRef DestTy, const char *Name) {
1958 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1961 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1962 LLVMTypeRef DestTy, const char *Name) {
1963 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1966 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1967 LLVMTypeRef DestTy, const char *Name) {
1968 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1971 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1972 LLVMTypeRef DestTy, const char *Name) {
1973 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1976 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1977 LLVMTypeRef DestTy, const char *Name) {
1978 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1981 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1982 LLVMTypeRef DestTy, const char *Name) {
1983 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1986 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1987 LLVMTypeRef DestTy, const char *Name) {
1988 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1991 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1992 LLVMTypeRef DestTy, const char *Name) {
1993 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1996 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1997 LLVMTypeRef DestTy, const char *Name) {
1998 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2001 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2002 LLVMTypeRef DestTy, const char *Name) {
2003 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2007 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2008 LLVMTypeRef DestTy, const char *Name) {
2009 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2013 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2014 LLVMTypeRef DestTy, const char *Name) {
2015 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2019 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2020 LLVMTypeRef DestTy, const char *Name) {
2021 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2022 unwrap(DestTy), Name));
2025 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2026 LLVMTypeRef DestTy, const char *Name) {
2027 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2030 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2031 LLVMTypeRef DestTy, const char *Name) {
2032 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2033 /*isSigned*/true, Name));
2036 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2037 LLVMTypeRef DestTy, const char *Name) {
2038 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2041 /*--.. Comparisons .........................................................--*/
2043 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2044 LLVMValueRef LHS, LLVMValueRef RHS,
2046 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2047 unwrap(LHS), unwrap(RHS), Name));
2050 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2051 LLVMValueRef LHS, LLVMValueRef RHS,
2053 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2054 unwrap(LHS), unwrap(RHS), Name));
2057 /*--.. Miscellaneous instructions ..........................................--*/
2059 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2060 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2063 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2064 LLVMValueRef *Args, unsigned NumArgs,
2066 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2067 unwrap(Args) + NumArgs, Name));
2070 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2071 LLVMValueRef Then, LLVMValueRef Else,
2073 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2077 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2078 LLVMTypeRef Ty, const char *Name) {
2079 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2082 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2083 LLVMValueRef Index, const char *Name) {
2084 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2088 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2089 LLVMValueRef EltVal, LLVMValueRef Index,
2091 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2092 unwrap(Index), Name));
2095 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2096 LLVMValueRef V2, LLVMValueRef Mask,
2098 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2099 unwrap(Mask), Name));
2102 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2103 unsigned Index, const char *Name) {
2104 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2107 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2108 LLVMValueRef EltVal, unsigned Index,
2110 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2114 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2116 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2119 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2121 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2124 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2125 LLVMValueRef RHS, const char *Name) {
2126 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2130 /*===-- Module providers --------------------------------------------------===*/
2132 LLVMModuleProviderRef
2133 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2134 return reinterpret_cast<LLVMModuleProviderRef>(M);
2137 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2142 /*===-- Memory buffers ----------------------------------------------------===*/
2144 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2146 LLVMMemoryBufferRef *OutMemBuf,
2147 char **OutMessage) {
2149 OwningPtr<MemoryBuffer> MB;
2151 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2152 *OutMemBuf = wrap(MB.take());
2156 *OutMessage = strdup(ec.message().c_str());
2160 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2161 char **OutMessage) {
2162 OwningPtr<MemoryBuffer> MB;
2164 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2165 *OutMemBuf = wrap(MB.take());
2169 *OutMessage = strdup(ec.message().c_str());
2173 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2174 delete unwrap(MemBuf);
2177 /*===-- Pass Registry -----------------------------------------------------===*/
2179 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2180 return wrap(PassRegistry::getPassRegistry());
2183 /*===-- Pass Manager ------------------------------------------------------===*/
2185 LLVMPassManagerRef LLVMCreatePassManager() {
2186 return wrap(new PassManager());
2189 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2190 return wrap(new FunctionPassManager(unwrap(M)));
2193 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2194 return LLVMCreateFunctionPassManagerForModule(
2195 reinterpret_cast<LLVMModuleRef>(P));
2198 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2199 return unwrap<PassManager>(PM)->run(*unwrap(M));
2202 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2203 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2206 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2207 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2210 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2211 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2214 void LLVMDisposePassManager(LLVMPassManagerRef PM) {