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 makeArrayRef(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), makeArrayRef(Elements, Count),
582 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
583 LLVMBool DontNullTerminate) {
584 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
587 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
588 LLVMValueRef *ConstantVals, unsigned Length) {
589 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
590 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
592 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
594 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
598 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
599 LLVMValueRef *ConstantVals,
601 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
602 StructType *Ty = cast<StructType>(unwrap(StructTy));
604 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
607 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
608 return wrap(ConstantVector::get(makeArrayRef(
609 unwrap<Constant>(ScalarConstantVals, Size), Size)));
611 /*--.. Constant expressions ................................................--*/
613 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
614 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
617 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
618 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
621 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
622 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
625 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
626 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
629 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
630 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
633 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
634 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
638 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
639 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
642 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
643 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
646 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
647 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
648 unwrap<Constant>(RHSConstant)));
651 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
652 LLVMValueRef RHSConstant) {
653 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
654 unwrap<Constant>(RHSConstant)));
657 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
658 LLVMValueRef RHSConstant) {
659 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
660 unwrap<Constant>(RHSConstant)));
663 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
664 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
665 unwrap<Constant>(RHSConstant)));
668 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
669 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
670 unwrap<Constant>(RHSConstant)));
673 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
674 LLVMValueRef RHSConstant) {
675 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
676 unwrap<Constant>(RHSConstant)));
679 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
680 LLVMValueRef RHSConstant) {
681 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
682 unwrap<Constant>(RHSConstant)));
685 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
686 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
687 unwrap<Constant>(RHSConstant)));
690 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
691 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
692 unwrap<Constant>(RHSConstant)));
695 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
696 LLVMValueRef RHSConstant) {
697 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
698 unwrap<Constant>(RHSConstant)));
701 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
702 LLVMValueRef RHSConstant) {
703 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
704 unwrap<Constant>(RHSConstant)));
707 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
708 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
709 unwrap<Constant>(RHSConstant)));
712 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
713 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
714 unwrap<Constant>(RHSConstant)));
717 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
718 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
719 unwrap<Constant>(RHSConstant)));
722 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
723 LLVMValueRef RHSConstant) {
724 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
725 unwrap<Constant>(RHSConstant)));
728 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
729 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
730 unwrap<Constant>(RHSConstant)));
733 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
734 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
735 unwrap<Constant>(RHSConstant)));
738 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
739 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
740 unwrap<Constant>(RHSConstant)));
743 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
744 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
745 unwrap<Constant>(RHSConstant)));
748 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
749 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
750 unwrap<Constant>(RHSConstant)));
753 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
755 unwrap<Constant>(RHSConstant)));
758 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
759 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
760 unwrap<Constant>(RHSConstant)));
763 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
764 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
765 return wrap(ConstantExpr::getICmp(Predicate,
766 unwrap<Constant>(LHSConstant),
767 unwrap<Constant>(RHSConstant)));
770 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
771 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
772 return wrap(ConstantExpr::getFCmp(Predicate,
773 unwrap<Constant>(LHSConstant),
774 unwrap<Constant>(RHSConstant)));
777 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
778 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
779 unwrap<Constant>(RHSConstant)));
782 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
783 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
784 unwrap<Constant>(RHSConstant)));
787 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
788 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
789 unwrap<Constant>(RHSConstant)));
792 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
793 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
794 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
795 unwrap<Constant>(ConstantIndices,
800 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
801 LLVMValueRef *ConstantIndices,
802 unsigned NumIndices) {
803 Constant* Val = unwrap<Constant>(ConstantVal);
804 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
805 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
808 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
809 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
813 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
814 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
818 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
819 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
823 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
824 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
828 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
829 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
833 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
834 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
838 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
839 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
843 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
844 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
848 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
849 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
853 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
854 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
858 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
859 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
863 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
868 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
869 LLVMTypeRef ToType) {
870 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
874 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
875 LLVMTypeRef ToType) {
876 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
880 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
881 LLVMTypeRef ToType) {
882 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
886 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
887 LLVMTypeRef ToType) {
888 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
892 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
894 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
895 unwrap(ToType), isSigned));
898 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
899 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
903 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
904 LLVMValueRef ConstantIfTrue,
905 LLVMValueRef ConstantIfFalse) {
906 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
907 unwrap<Constant>(ConstantIfTrue),
908 unwrap<Constant>(ConstantIfFalse)));
911 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
912 LLVMValueRef IndexConstant) {
913 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
914 unwrap<Constant>(IndexConstant)));
917 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
918 LLVMValueRef ElementValueConstant,
919 LLVMValueRef IndexConstant) {
920 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
921 unwrap<Constant>(ElementValueConstant),
922 unwrap<Constant>(IndexConstant)));
925 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
926 LLVMValueRef VectorBConstant,
927 LLVMValueRef MaskConstant) {
928 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
929 unwrap<Constant>(VectorBConstant),
930 unwrap<Constant>(MaskConstant)));
933 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
935 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
936 makeArrayRef(IdxList, NumIdx)));
939 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
940 LLVMValueRef ElementValueConstant,
941 unsigned *IdxList, unsigned NumIdx) {
942 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
943 unwrap<Constant>(ElementValueConstant),
944 makeArrayRef(IdxList, NumIdx)));
947 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
948 const char *Constraints,
949 LLVMBool HasSideEffects,
950 LLVMBool IsAlignStack) {
951 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
952 Constraints, HasSideEffects, IsAlignStack));
955 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
956 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
959 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
961 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
962 return wrap(unwrap<GlobalValue>(Global)->getParent());
965 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
966 return unwrap<GlobalValue>(Global)->isDeclaration();
969 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
970 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
972 assert(false && "Unhandled Linkage Type.");
973 case GlobalValue::ExternalLinkage:
974 return LLVMExternalLinkage;
975 case GlobalValue::AvailableExternallyLinkage:
976 return LLVMAvailableExternallyLinkage;
977 case GlobalValue::LinkOnceAnyLinkage:
978 return LLVMLinkOnceAnyLinkage;
979 case GlobalValue::LinkOnceODRLinkage:
980 return LLVMLinkOnceODRLinkage;
981 case GlobalValue::WeakAnyLinkage:
982 return LLVMWeakAnyLinkage;
983 case GlobalValue::WeakODRLinkage:
984 return LLVMWeakODRLinkage;
985 case GlobalValue::AppendingLinkage:
986 return LLVMAppendingLinkage;
987 case GlobalValue::InternalLinkage:
988 return LLVMInternalLinkage;
989 case GlobalValue::PrivateLinkage:
990 return LLVMPrivateLinkage;
991 case GlobalValue::LinkerPrivateLinkage:
992 return LLVMLinkerPrivateLinkage;
993 case GlobalValue::LinkerPrivateWeakLinkage:
994 return LLVMLinkerPrivateWeakLinkage;
995 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
996 return LLVMLinkerPrivateWeakDefAutoLinkage;
997 case GlobalValue::DLLImportLinkage:
998 return LLVMDLLImportLinkage;
999 case GlobalValue::DLLExportLinkage:
1000 return LLVMDLLExportLinkage;
1001 case GlobalValue::ExternalWeakLinkage:
1002 return LLVMExternalWeakLinkage;
1003 case GlobalValue::CommonLinkage:
1004 return LLVMCommonLinkage;
1007 // Should never get here.
1008 return static_cast<LLVMLinkage>(0);
1011 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1012 GlobalValue *GV = unwrap<GlobalValue>(Global);
1016 assert(false && "Unhandled Linkage Type.");
1017 case LLVMExternalLinkage:
1018 GV->setLinkage(GlobalValue::ExternalLinkage);
1020 case LLVMAvailableExternallyLinkage:
1021 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1023 case LLVMLinkOnceAnyLinkage:
1024 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1026 case LLVMLinkOnceODRLinkage:
1027 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1029 case LLVMWeakAnyLinkage:
1030 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1032 case LLVMWeakODRLinkage:
1033 GV->setLinkage(GlobalValue::WeakODRLinkage);
1035 case LLVMAppendingLinkage:
1036 GV->setLinkage(GlobalValue::AppendingLinkage);
1038 case LLVMInternalLinkage:
1039 GV->setLinkage(GlobalValue::InternalLinkage);
1041 case LLVMPrivateLinkage:
1042 GV->setLinkage(GlobalValue::PrivateLinkage);
1044 case LLVMLinkerPrivateLinkage:
1045 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1047 case LLVMLinkerPrivateWeakLinkage:
1048 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1050 case LLVMLinkerPrivateWeakDefAutoLinkage:
1051 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1053 case LLVMDLLImportLinkage:
1054 GV->setLinkage(GlobalValue::DLLImportLinkage);
1056 case LLVMDLLExportLinkage:
1057 GV->setLinkage(GlobalValue::DLLExportLinkage);
1059 case LLVMExternalWeakLinkage:
1060 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1062 case LLVMGhostLinkage:
1064 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1066 case LLVMCommonLinkage:
1067 GV->setLinkage(GlobalValue::CommonLinkage);
1072 const char *LLVMGetSection(LLVMValueRef Global) {
1073 return unwrap<GlobalValue>(Global)->getSection().c_str();
1076 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1077 unwrap<GlobalValue>(Global)->setSection(Section);
1080 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1081 return static_cast<LLVMVisibility>(
1082 unwrap<GlobalValue>(Global)->getVisibility());
1085 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1086 unwrap<GlobalValue>(Global)
1087 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1090 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1091 return unwrap<GlobalValue>(Global)->getAlignment();
1094 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1095 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1098 /*--.. Operations on global variables ......................................--*/
1100 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1101 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1102 GlobalValue::ExternalLinkage, 0, Name));
1105 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1107 unsigned AddressSpace) {
1108 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1109 GlobalValue::ExternalLinkage, 0, Name, 0,
1110 false, AddressSpace));
1113 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1114 return wrap(unwrap(M)->getNamedGlobal(Name));
1117 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1118 Module *Mod = unwrap(M);
1119 Module::global_iterator I = Mod->global_begin();
1120 if (I == Mod->global_end())
1125 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1126 Module *Mod = unwrap(M);
1127 Module::global_iterator I = Mod->global_end();
1128 if (I == Mod->global_begin())
1133 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1134 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1135 Module::global_iterator I = GV;
1136 if (++I == GV->getParent()->global_end())
1141 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1142 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1143 Module::global_iterator I = GV;
1144 if (I == GV->getParent()->global_begin())
1149 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1150 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1153 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1154 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1155 if ( !GV->hasInitializer() )
1157 return wrap(GV->getInitializer());
1160 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1161 unwrap<GlobalVariable>(GlobalVar)
1162 ->setInitializer(unwrap<Constant>(ConstantVal));
1165 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1166 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1169 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1170 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1173 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1174 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1177 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1178 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1181 /*--.. Operations on aliases ......................................--*/
1183 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1185 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1186 unwrap<Constant>(Aliasee), unwrap (M)));
1189 /*--.. Operations on functions .............................................--*/
1191 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1192 LLVMTypeRef FunctionTy) {
1193 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1194 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1197 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1198 return wrap(unwrap(M)->getFunction(Name));
1201 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1202 Module *Mod = unwrap(M);
1203 Module::iterator I = Mod->begin();
1204 if (I == Mod->end())
1209 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1210 Module *Mod = unwrap(M);
1211 Module::iterator I = Mod->end();
1212 if (I == Mod->begin())
1217 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1218 Function *Func = unwrap<Function>(Fn);
1219 Module::iterator I = Func;
1220 if (++I == Func->getParent()->end())
1225 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1226 Function *Func = unwrap<Function>(Fn);
1227 Module::iterator I = Func;
1228 if (I == Func->getParent()->begin())
1233 void LLVMDeleteFunction(LLVMValueRef Fn) {
1234 unwrap<Function>(Fn)->eraseFromParent();
1237 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1238 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1239 return F->getIntrinsicID();
1243 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1244 return unwrap<Function>(Fn)->getCallingConv();
1247 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1248 return unwrap<Function>(Fn)->setCallingConv(
1249 static_cast<CallingConv::ID>(CC));
1252 const char *LLVMGetGC(LLVMValueRef Fn) {
1253 Function *F = unwrap<Function>(Fn);
1254 return F->hasGC()? F->getGC() : 0;
1257 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1258 Function *F = unwrap<Function>(Fn);
1265 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1266 Function *Func = unwrap<Function>(Fn);
1267 const AttrListPtr PAL = Func->getAttributes();
1268 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1269 Func->setAttributes(PALnew);
1272 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1273 Function *Func = unwrap<Function>(Fn);
1274 const AttrListPtr PAL = Func->getAttributes();
1275 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1276 Func->setAttributes(PALnew);
1279 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1280 Function *Func = unwrap<Function>(Fn);
1281 const AttrListPtr PAL = Func->getAttributes();
1282 Attributes attr = PAL.getFnAttributes();
1283 return (LLVMAttribute)attr;
1286 /*--.. Operations on parameters ............................................--*/
1288 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1289 // This function is strictly redundant to
1290 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1291 return unwrap<Function>(FnRef)->arg_size();
1294 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1295 Function *Fn = unwrap<Function>(FnRef);
1296 for (Function::arg_iterator I = Fn->arg_begin(),
1297 E = Fn->arg_end(); I != E; I++)
1298 *ParamRefs++ = wrap(I);
1301 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1302 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1308 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1309 return wrap(unwrap<Argument>(V)->getParent());
1312 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1313 Function *Func = unwrap<Function>(Fn);
1314 Function::arg_iterator I = Func->arg_begin();
1315 if (I == Func->arg_end())
1320 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1321 Function *Func = unwrap<Function>(Fn);
1322 Function::arg_iterator I = Func->arg_end();
1323 if (I == Func->arg_begin())
1328 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1329 Argument *A = unwrap<Argument>(Arg);
1330 Function::arg_iterator I = A;
1331 if (++I == A->getParent()->arg_end())
1336 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1337 Argument *A = unwrap<Argument>(Arg);
1338 Function::arg_iterator I = A;
1339 if (I == A->getParent()->arg_begin())
1344 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1345 unwrap<Argument>(Arg)->addAttr(PA);
1348 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1349 unwrap<Argument>(Arg)->removeAttr(PA);
1352 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1353 Argument *A = unwrap<Argument>(Arg);
1354 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1356 return (LLVMAttribute)attr;
1360 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1361 unwrap<Argument>(Arg)->addAttr(
1362 Attribute::constructAlignmentFromInt(align));
1365 /*--.. Operations on basic blocks ..........................................--*/
1367 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1368 return wrap(static_cast<Value*>(unwrap(BB)));
1371 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1372 return isa<BasicBlock>(unwrap(Val));
1375 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1376 return wrap(unwrap<BasicBlock>(Val));
1379 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1380 return wrap(unwrap(BB)->getParent());
1383 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1384 return unwrap<Function>(FnRef)->size();
1387 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1388 Function *Fn = unwrap<Function>(FnRef);
1389 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1390 *BasicBlocksRefs++ = wrap(I);
1393 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1394 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1397 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1398 Function *Func = unwrap<Function>(Fn);
1399 Function::iterator I = Func->begin();
1400 if (I == Func->end())
1405 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1406 Function *Func = unwrap<Function>(Fn);
1407 Function::iterator I = Func->end();
1408 if (I == Func->begin())
1413 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1414 BasicBlock *Block = unwrap(BB);
1415 Function::iterator I = Block;
1416 if (++I == Block->getParent()->end())
1421 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1422 BasicBlock *Block = unwrap(BB);
1423 Function::iterator I = Block;
1424 if (I == Block->getParent()->begin())
1429 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1432 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1435 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1436 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1439 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1440 LLVMBasicBlockRef BBRef,
1442 BasicBlock *BB = unwrap(BBRef);
1443 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1446 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1448 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1451 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1452 unwrap(BBRef)->eraseFromParent();
1455 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1456 unwrap(BB)->moveBefore(unwrap(MovePos));
1459 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1460 unwrap(BB)->moveAfter(unwrap(MovePos));
1463 /*--.. Operations on instructions ..........................................--*/
1465 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1466 return wrap(unwrap<Instruction>(Inst)->getParent());
1469 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1470 BasicBlock *Block = unwrap(BB);
1471 BasicBlock::iterator I = Block->begin();
1472 if (I == Block->end())
1477 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1478 BasicBlock *Block = unwrap(BB);
1479 BasicBlock::iterator I = Block->end();
1480 if (I == Block->begin())
1485 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1486 Instruction *Instr = unwrap<Instruction>(Inst);
1487 BasicBlock::iterator I = Instr;
1488 if (++I == Instr->getParent()->end())
1493 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1494 Instruction *Instr = unwrap<Instruction>(Inst);
1495 BasicBlock::iterator I = Instr;
1496 if (I == Instr->getParent()->begin())
1501 /*--.. Call and invoke instructions ........................................--*/
1503 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1504 Value *V = unwrap(Instr);
1505 if (CallInst *CI = dyn_cast<CallInst>(V))
1506 return CI->getCallingConv();
1507 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1508 return II->getCallingConv();
1509 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1513 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1514 Value *V = unwrap(Instr);
1515 if (CallInst *CI = dyn_cast<CallInst>(V))
1516 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1517 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1518 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1519 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1522 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1524 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1526 Call.getAttributes().addAttr(index, PA));
1529 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1531 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1533 Call.getAttributes().removeAttr(index, PA));
1536 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1538 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1540 Call.getAttributes().addAttr(index,
1541 Attribute::constructAlignmentFromInt(align)));
1544 /*--.. Operations on call instructions (only) ..............................--*/
1546 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1547 return unwrap<CallInst>(Call)->isTailCall();
1550 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1551 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1554 /*--.. Operations on phi nodes .............................................--*/
1556 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1557 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1558 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1559 for (unsigned I = 0; I != Count; ++I)
1560 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1563 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1564 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1567 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1568 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1571 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1572 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1576 /*===-- Instruction builders ----------------------------------------------===*/
1578 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1579 return wrap(new IRBuilder<>(*unwrap(C)));
1582 LLVMBuilderRef LLVMCreateBuilder(void) {
1583 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1586 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1587 LLVMValueRef Instr) {
1588 BasicBlock *BB = unwrap(Block);
1589 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1590 unwrap(Builder)->SetInsertPoint(BB, I);
1593 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1594 Instruction *I = unwrap<Instruction>(Instr);
1595 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1598 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1599 BasicBlock *BB = unwrap(Block);
1600 unwrap(Builder)->SetInsertPoint(BB);
1603 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1604 return wrap(unwrap(Builder)->GetInsertBlock());
1607 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1608 unwrap(Builder)->ClearInsertionPoint();
1611 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1612 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1615 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1617 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1620 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1621 delete unwrap(Builder);
1624 /*--.. Metadata builders ...................................................--*/
1626 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1627 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1628 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1631 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1632 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1633 .getAsMDNode(unwrap(Builder)->getContext()));
1636 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1637 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1641 /*--.. Instruction builders ................................................--*/
1643 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1644 return wrap(unwrap(B)->CreateRetVoid());
1647 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1648 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1651 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1653 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1656 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1657 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1660 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1661 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1662 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1665 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1666 LLVMBasicBlockRef Else, unsigned NumCases) {
1667 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1670 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1671 unsigned NumDests) {
1672 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1675 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1676 LLVMValueRef *Args, unsigned NumArgs,
1677 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1679 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1680 makeArrayRef(unwrap(Args), NumArgs),
1684 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1685 return wrap(unwrap(B)->CreateUnwind());
1688 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1689 return wrap(unwrap(B)->CreateUnreachable());
1692 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1693 LLVMBasicBlockRef Dest) {
1694 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1697 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1698 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1701 /*--.. Arithmetic ..........................................................--*/
1703 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1705 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1708 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1710 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1713 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1715 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1718 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1720 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1723 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1725 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1728 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1730 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1733 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1735 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1738 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1740 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1743 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1745 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1748 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1750 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1753 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1755 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1758 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1760 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1763 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1765 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1768 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1770 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1773 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1774 LLVMValueRef RHS, const char *Name) {
1775 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1778 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1780 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1783 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1785 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1788 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1790 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1793 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1795 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1798 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1800 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1803 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1805 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1808 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1810 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1813 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1815 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1818 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1820 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1823 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1825 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1828 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1829 LLVMValueRef LHS, LLVMValueRef RHS,
1831 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1832 unwrap(RHS), Name));
1835 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1836 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1839 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1841 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1844 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1846 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1849 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1850 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1853 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1854 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1857 /*--.. Memory ..............................................................--*/
1859 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1861 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1862 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1863 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1864 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1865 ITy, unwrap(Ty), AllocSize,
1867 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1870 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1871 LLVMValueRef Val, const char *Name) {
1872 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1873 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1874 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1875 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1876 ITy, unwrap(Ty), AllocSize,
1877 unwrap(Val), 0, "");
1878 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1881 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1883 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1886 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1887 LLVMValueRef Val, const char *Name) {
1888 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1891 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1892 return wrap(unwrap(B)->Insert(
1893 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1897 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1899 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1902 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1903 LLVMValueRef PointerVal) {
1904 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1907 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1908 LLVMValueRef *Indices, unsigned NumIndices,
1910 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1911 unwrap(Indices) + NumIndices, Name));
1914 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1915 LLVMValueRef *Indices, unsigned NumIndices,
1917 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1918 unwrap(Indices) + NumIndices, Name));
1921 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1922 unsigned Idx, const char *Name) {
1923 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1926 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1928 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1931 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1933 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1936 /*--.. Casts ...............................................................--*/
1938 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1939 LLVMTypeRef DestTy, const char *Name) {
1940 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1943 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1944 LLVMTypeRef DestTy, const char *Name) {
1945 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1948 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1949 LLVMTypeRef DestTy, const char *Name) {
1950 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1953 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1954 LLVMTypeRef DestTy, const char *Name) {
1955 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1958 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1959 LLVMTypeRef DestTy, const char *Name) {
1960 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1963 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1964 LLVMTypeRef DestTy, const char *Name) {
1965 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1968 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1969 LLVMTypeRef DestTy, const char *Name) {
1970 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1973 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1974 LLVMTypeRef DestTy, const char *Name) {
1975 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1978 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1979 LLVMTypeRef DestTy, const char *Name) {
1980 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1983 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1984 LLVMTypeRef DestTy, const char *Name) {
1985 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1988 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1989 LLVMTypeRef DestTy, const char *Name) {
1990 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1993 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1994 LLVMTypeRef DestTy, const char *Name) {
1995 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1998 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1999 LLVMTypeRef DestTy, const char *Name) {
2000 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2004 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2005 LLVMTypeRef DestTy, const char *Name) {
2006 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2010 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2011 LLVMTypeRef DestTy, const char *Name) {
2012 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2016 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2017 LLVMTypeRef DestTy, const char *Name) {
2018 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2019 unwrap(DestTy), Name));
2022 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2023 LLVMTypeRef DestTy, const char *Name) {
2024 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2027 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2028 LLVMTypeRef DestTy, const char *Name) {
2029 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2030 /*isSigned*/true, Name));
2033 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2034 LLVMTypeRef DestTy, const char *Name) {
2035 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2038 /*--.. Comparisons .........................................................--*/
2040 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2041 LLVMValueRef LHS, LLVMValueRef RHS,
2043 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2044 unwrap(LHS), unwrap(RHS), Name));
2047 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2048 LLVMValueRef LHS, LLVMValueRef RHS,
2050 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2051 unwrap(LHS), unwrap(RHS), Name));
2054 /*--.. Miscellaneous instructions ..........................................--*/
2056 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2057 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2060 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2061 LLVMValueRef *Args, unsigned NumArgs,
2063 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2064 makeArrayRef(unwrap(Args), NumArgs),
2068 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2069 LLVMValueRef Then, LLVMValueRef Else,
2071 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2075 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2076 LLVMTypeRef Ty, const char *Name) {
2077 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2080 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2081 LLVMValueRef Index, const char *Name) {
2082 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2086 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2087 LLVMValueRef EltVal, LLVMValueRef Index,
2089 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2090 unwrap(Index), Name));
2093 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2094 LLVMValueRef V2, LLVMValueRef Mask,
2096 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2097 unwrap(Mask), Name));
2100 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2101 unsigned Index, const char *Name) {
2102 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2105 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2106 LLVMValueRef EltVal, unsigned Index,
2108 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2112 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2114 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2117 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2119 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2122 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2123 LLVMValueRef RHS, const char *Name) {
2124 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2128 /*===-- Module providers --------------------------------------------------===*/
2130 LLVMModuleProviderRef
2131 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2132 return reinterpret_cast<LLVMModuleProviderRef>(M);
2135 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2140 /*===-- Memory buffers ----------------------------------------------------===*/
2142 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2144 LLVMMemoryBufferRef *OutMemBuf,
2145 char **OutMessage) {
2147 OwningPtr<MemoryBuffer> MB;
2149 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2150 *OutMemBuf = wrap(MB.take());
2154 *OutMessage = strdup(ec.message().c_str());
2158 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2159 char **OutMessage) {
2160 OwningPtr<MemoryBuffer> MB;
2162 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2163 *OutMemBuf = wrap(MB.take());
2167 *OutMessage = strdup(ec.message().c_str());
2171 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2172 delete unwrap(MemBuf);
2175 /*===-- Pass Registry -----------------------------------------------------===*/
2177 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2178 return wrap(PassRegistry::getPassRegistry());
2181 /*===-- Pass Manager ------------------------------------------------------===*/
2183 LLVMPassManagerRef LLVMCreatePassManager() {
2184 return wrap(new PassManager());
2187 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2188 return wrap(new FunctionPassManager(unwrap(M)));
2191 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2192 return LLVMCreateFunctionPassManagerForModule(
2193 reinterpret_cast<LLVMModuleRef>(P));
2196 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2197 return unwrap<PassManager>(PM)->run(*unwrap(M));
2200 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2201 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2204 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2205 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2208 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2209 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2212 void LLVMDisposePassManager(LLVMPassManagerRef PM) {