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 C bindings for libLLVMCore.a, which implements
11 // 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/TypeSymbolTable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.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"
37 /*===-- Error handling ----------------------------------------------------===*/
39 void LLVMDisposeMessage(char *Message) {
44 /*===-- Operations on contexts --------------------------------------------===*/
46 LLVMContextRef LLVMContextCreate() {
47 return wrap(new LLVMContext());
50 LLVMContextRef LLVMGetGlobalContext() {
51 return wrap(&getGlobalContext());
54 void LLVMContextDispose(LLVMContextRef C) {
59 /*===-- Operations on modules ---------------------------------------------===*/
61 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
62 return wrap(new Module(ModuleID, getGlobalContext()));
65 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
67 return wrap(new Module(ModuleID, *unwrap(C)));
70 void LLVMDisposeModule(LLVMModuleRef M) {
74 /*--.. Data layout .........................................................--*/
75 const char * LLVMGetDataLayout(LLVMModuleRef M) {
76 return unwrap(M)->getDataLayout().c_str();
79 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
80 unwrap(M)->setDataLayout(Triple);
83 /*--.. Target triple .......................................................--*/
84 const char * LLVMGetTarget(LLVMModuleRef M) {
85 return unwrap(M)->getTargetTriple().c_str();
88 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
89 unwrap(M)->setTargetTriple(Triple);
92 /*--.. Type names ..........................................................--*/
93 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
94 return unwrap(M)->addTypeName(Name, unwrap(Ty));
97 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
100 TypeSymbolTable::iterator I = TST.find(Name);
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106 return wrap(unwrap(M)->getTypeByName(Name));
109 void LLVMDumpModule(LLVMModuleRef M) {
114 /*===-- Operations on types -----------------------------------------------===*/
116 /*--.. Operations on all types (mostly) ....................................--*/
118 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
119 switch (unwrap(Ty)->getTypeID()) {
121 assert(false && "Unhandled TypeID.");
123 return LLVMVoidTypeKind;
124 case Type::FloatTyID:
125 return LLVMFloatTypeKind;
126 case Type::DoubleTyID:
127 return LLVMDoubleTypeKind;
128 case Type::X86_FP80TyID:
129 return LLVMX86_FP80TypeKind;
130 case Type::FP128TyID:
131 return LLVMFP128TypeKind;
132 case Type::PPC_FP128TyID:
133 return LLVMPPC_FP128TypeKind;
134 case Type::LabelTyID:
135 return LLVMLabelTypeKind;
136 case Type::MetadataTyID:
137 return LLVMMetadataTypeKind;
138 case Type::IntegerTyID:
139 return LLVMIntegerTypeKind;
140 case Type::FunctionTyID:
141 return LLVMFunctionTypeKind;
142 case Type::StructTyID:
143 return LLVMStructTypeKind;
144 case Type::UnionTyID:
145 return LLVMUnionTypeKind;
146 case Type::ArrayTyID:
147 return LLVMArrayTypeKind;
148 case Type::PointerTyID:
149 return LLVMPointerTypeKind;
150 case Type::OpaqueTyID:
151 return LLVMOpaqueTypeKind;
152 case Type::VectorTyID:
153 return LLVMVectorTypeKind;
157 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
158 return wrap(&unwrap(Ty)->getContext());
161 /*--.. Operations on integer types .........................................--*/
163 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
164 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
166 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
167 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
169 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
170 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
172 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
173 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
175 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
176 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
178 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
179 return wrap(IntegerType::get(*unwrap(C), NumBits));
182 LLVMTypeRef LLVMInt1Type(void) {
183 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
185 LLVMTypeRef LLVMInt8Type(void) {
186 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
188 LLVMTypeRef LLVMInt16Type(void) {
189 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
191 LLVMTypeRef LLVMInt32Type(void) {
192 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
194 LLVMTypeRef LLVMInt64Type(void) {
195 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
197 LLVMTypeRef LLVMIntType(unsigned NumBits) {
198 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
201 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
202 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
205 /*--.. Operations on real types ............................................--*/
207 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
208 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
210 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
211 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
213 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
214 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
216 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
217 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
219 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
220 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
223 LLVMTypeRef LLVMFloatType(void) {
224 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
226 LLVMTypeRef LLVMDoubleType(void) {
227 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
229 LLVMTypeRef LLVMX86FP80Type(void) {
230 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
232 LLVMTypeRef LLVMFP128Type(void) {
233 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
235 LLVMTypeRef LLVMPPCFP128Type(void) {
236 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
239 /*--.. Operations on function types ........................................--*/
241 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
242 LLVMTypeRef *ParamTypes, unsigned ParamCount,
244 std::vector<const Type*> Tys;
245 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
246 Tys.push_back(unwrap(*I));
248 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
251 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
252 return unwrap<FunctionType>(FunctionTy)->isVarArg();
255 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
256 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
259 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
260 return unwrap<FunctionType>(FunctionTy)->getNumParams();
263 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
264 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
265 for (FunctionType::param_iterator I = Ty->param_begin(),
266 E = Ty->param_end(); I != E; ++I)
270 /*--.. Operations on struct types ..........................................--*/
272 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
273 unsigned ElementCount, LLVMBool Packed) {
274 std::vector<const Type*> Tys;
275 for (LLVMTypeRef *I = ElementTypes,
276 *E = ElementTypes + ElementCount; I != E; ++I)
277 Tys.push_back(unwrap(*I));
279 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
282 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
283 unsigned ElementCount, LLVMBool Packed) {
284 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
285 ElementCount, Packed);
289 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
290 return unwrap<StructType>(StructTy)->getNumElements();
293 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
294 StructType *Ty = unwrap<StructType>(StructTy);
295 for (FunctionType::param_iterator I = Ty->element_begin(),
296 E = Ty->element_end(); I != E; ++I)
300 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
301 return unwrap<StructType>(StructTy)->isPacked();
304 /*--.. Operations on union types ..........................................--*/
306 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
307 unsigned ElementCount) {
308 SmallVector<const Type*, 8> Tys;
309 for (LLVMTypeRef *I = ElementTypes,
310 *E = ElementTypes + ElementCount; I != E; ++I)
311 Tys.push_back(unwrap(*I));
313 return wrap(UnionType::get(&Tys[0], Tys.size()));
316 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes,
317 unsigned ElementCount, int Packed) {
318 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
322 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
323 return unwrap<UnionType>(UnionTy)->getNumElements();
326 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
327 UnionType *Ty = unwrap<UnionType>(UnionTy);
328 for (FunctionType::param_iterator I = Ty->element_begin(),
329 E = Ty->element_end(); I != E; ++I)
333 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
335 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
336 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
339 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
340 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
343 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
344 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
347 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
348 return wrap(unwrap<SequentialType>(Ty)->getElementType());
351 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
352 return unwrap<ArrayType>(ArrayTy)->getNumElements();
355 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
356 return unwrap<PointerType>(PointerTy)->getAddressSpace();
359 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
360 return unwrap<VectorType>(VectorTy)->getNumElements();
363 /*--.. Operations on other types ...........................................--*/
365 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
366 return wrap(Type::getVoidTy(*unwrap(C)));
368 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
369 return wrap(Type::getLabelTy(*unwrap(C)));
371 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
372 return wrap(OpaqueType::get(*unwrap(C)));
375 LLVMTypeRef LLVMVoidType(void) {
376 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
378 LLVMTypeRef LLVMLabelType(void) {
379 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
381 LLVMTypeRef LLVMOpaqueType(void) {
382 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
385 /*--.. Operations on type handles ..........................................--*/
387 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
388 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
391 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
392 delete unwrap(TypeHandle);
395 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
396 return wrap(unwrap(TypeHandle)->get());
399 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
400 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
404 /*===-- Operations on values ----------------------------------------------===*/
406 /*--.. Operations on all values ............................................--*/
408 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
409 return wrap(unwrap(Val)->getType());
412 const char *LLVMGetValueName(LLVMValueRef Val) {
413 return unwrap(Val)->getName().data();
416 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
417 unwrap(Val)->setName(Name);
420 void LLVMDumpValue(LLVMValueRef Val) {
424 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
425 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
428 /*--.. Conversion functions ................................................--*/
430 #define LLVM_DEFINE_VALUE_CAST(name) \
431 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
432 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
435 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
437 /*--.. Operations on Uses ..................................................--*/
438 LLVMUseIteratorRef LLVMGetFirstUse(LLVMValueRef Val) {
439 Value *V = unwrap(Val);
440 Value::use_iterator I = V->use_begin();
441 if (I == V->use_end())
443 return wrap(&(I.getUse()));
446 LLVMUseIteratorRef LLVMGetNextUse(LLVMUseIteratorRef UR) {
447 return wrap(unwrap(UR)->getNext());
450 LLVMValueRef LLVMGetUser(LLVMUseIteratorRef UR) {
451 return wrap(unwrap(UR)->getUser());
454 LLVMValueRef LLVMGetUsedValue(LLVMUseIteratorRef UR) {
455 return wrap(unwrap(UR)->get());
458 /*--.. Operations on Users .................................................--*/
459 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
460 return wrap(unwrap<User>(Val)->getOperand(Index));
463 /*--.. Operations on constants of any type .................................--*/
465 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
466 return wrap(Constant::getNullValue(unwrap(Ty)));
469 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
470 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
473 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
474 return wrap(UndefValue::get(unwrap(Ty)));
477 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
478 return isa<Constant>(unwrap(Ty));
481 LLVMBool LLVMIsNull(LLVMValueRef Val) {
482 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
483 return C->isNullValue();
487 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
488 return isa<UndefValue>(unwrap(Val));
491 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
493 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
496 /*--.. Operations on scalar constants ......................................--*/
498 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
499 LLVMBool SignExtend) {
500 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
503 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
505 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
509 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
510 unsigned SLen, uint8_t Radix) {
511 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
515 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
516 return wrap(ConstantFP::get(unwrap(RealTy), N));
519 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
520 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
523 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
525 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
528 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
529 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
532 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
533 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
536 /*--.. Operations on composite constants ...................................--*/
538 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
540 LLVMBool DontNullTerminate) {
541 /* Inverted the sense of AddNull because ', 0)' is a
542 better mnemonic for null termination than ', 1)'. */
543 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
544 DontNullTerminate == 0));
546 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
547 LLVMValueRef *ConstantVals,
548 unsigned Count, LLVMBool Packed) {
549 return wrap(ConstantStruct::get(*unwrap(C),
550 unwrap<Constant>(ConstantVals, Count),
551 Count, Packed != 0));
554 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
555 LLVMBool DontNullTerminate) {
556 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
559 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
560 LLVMValueRef *ConstantVals, unsigned Length) {
561 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
562 unwrap<Constant>(ConstantVals, Length),
565 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
567 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
570 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
571 return wrap(ConstantVector::get(
572 unwrap<Constant>(ScalarConstantVals, Size), Size));
574 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
575 return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
578 /*--.. Constant expressions ................................................--*/
580 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
581 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
584 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
585 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
588 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
589 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
592 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
593 return wrap(ConstantExpr::getNeg(
594 unwrap<Constant>(ConstantVal)));
597 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
598 return wrap(ConstantExpr::getNSWNeg(
599 unwrap<Constant>(ConstantVal)));
602 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
603 return wrap(ConstantExpr::getNUWNeg(
604 unwrap<Constant>(ConstantVal)));
608 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
609 return wrap(ConstantExpr::getFNeg(
610 unwrap<Constant>(ConstantVal)));
613 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
614 return wrap(ConstantExpr::getNot(
615 unwrap<Constant>(ConstantVal)));
618 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
619 return wrap(ConstantExpr::getAdd(
620 unwrap<Constant>(LHSConstant),
621 unwrap<Constant>(RHSConstant)));
624 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
625 LLVMValueRef RHSConstant) {
626 return wrap(ConstantExpr::getNSWAdd(
627 unwrap<Constant>(LHSConstant),
628 unwrap<Constant>(RHSConstant)));
631 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
632 LLVMValueRef RHSConstant) {
633 return wrap(ConstantExpr::getNUWAdd(
634 unwrap<Constant>(LHSConstant),
635 unwrap<Constant>(RHSConstant)));
638 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
639 return wrap(ConstantExpr::getFAdd(
640 unwrap<Constant>(LHSConstant),
641 unwrap<Constant>(RHSConstant)));
644 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
645 return wrap(ConstantExpr::getSub(
646 unwrap<Constant>(LHSConstant),
647 unwrap<Constant>(RHSConstant)));
650 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
651 LLVMValueRef RHSConstant) {
652 return wrap(ConstantExpr::getNSWSub(
653 unwrap<Constant>(LHSConstant),
654 unwrap<Constant>(RHSConstant)));
657 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
658 LLVMValueRef RHSConstant) {
659 return wrap(ConstantExpr::getNUWSub(
660 unwrap<Constant>(LHSConstant),
661 unwrap<Constant>(RHSConstant)));
664 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
665 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
666 unwrap<Constant>(RHSConstant)));
669 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
670 return wrap(ConstantExpr::getMul(
671 unwrap<Constant>(LHSConstant),
672 unwrap<Constant>(RHSConstant)));
675 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
676 LLVMValueRef RHSConstant) {
677 return wrap(ConstantExpr::getNSWMul(
678 unwrap<Constant>(LHSConstant),
679 unwrap<Constant>(RHSConstant)));
682 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
683 LLVMValueRef RHSConstant) {
684 return wrap(ConstantExpr::getNUWMul(
685 unwrap<Constant>(LHSConstant),
686 unwrap<Constant>(RHSConstant)));
689 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
690 return wrap(ConstantExpr::getFMul(
691 unwrap<Constant>(LHSConstant),
692 unwrap<Constant>(RHSConstant)));
695 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
696 return wrap(ConstantExpr::getUDiv(
697 unwrap<Constant>(LHSConstant),
698 unwrap<Constant>(RHSConstant)));
701 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
702 return wrap(ConstantExpr::getSDiv(
703 unwrap<Constant>(LHSConstant),
704 unwrap<Constant>(RHSConstant)));
707 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
708 LLVMValueRef RHSConstant) {
709 return wrap(ConstantExpr::getExactSDiv(
710 unwrap<Constant>(LHSConstant),
711 unwrap<Constant>(RHSConstant)));
714 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
715 return wrap(ConstantExpr::getFDiv(
716 unwrap<Constant>(LHSConstant),
717 unwrap<Constant>(RHSConstant)));
720 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
721 return wrap(ConstantExpr::getURem(
722 unwrap<Constant>(LHSConstant),
723 unwrap<Constant>(RHSConstant)));
726 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727 return wrap(ConstantExpr::getSRem(
728 unwrap<Constant>(LHSConstant),
729 unwrap<Constant>(RHSConstant)));
732 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
733 return wrap(ConstantExpr::getFRem(
734 unwrap<Constant>(LHSConstant),
735 unwrap<Constant>(RHSConstant)));
738 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
739 return wrap(ConstantExpr::getAnd(
740 unwrap<Constant>(LHSConstant),
741 unwrap<Constant>(RHSConstant)));
744 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
745 return wrap(ConstantExpr::getOr(
746 unwrap<Constant>(LHSConstant),
747 unwrap<Constant>(RHSConstant)));
750 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
751 return wrap(ConstantExpr::getXor(
752 unwrap<Constant>(LHSConstant),
753 unwrap<Constant>(RHSConstant)));
756 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
757 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758 return wrap(ConstantExpr::getICmp(Predicate,
759 unwrap<Constant>(LHSConstant),
760 unwrap<Constant>(RHSConstant)));
763 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
764 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
765 return wrap(ConstantExpr::getFCmp(Predicate,
766 unwrap<Constant>(LHSConstant),
767 unwrap<Constant>(RHSConstant)));
770 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
771 return wrap(ConstantExpr::getShl(
772 unwrap<Constant>(LHSConstant),
773 unwrap<Constant>(RHSConstant)));
776 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
777 return wrap(ConstantExpr::getLShr(
778 unwrap<Constant>(LHSConstant),
779 unwrap<Constant>(RHSConstant)));
782 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
783 return wrap(ConstantExpr::getAShr(
784 unwrap<Constant>(LHSConstant),
785 unwrap<Constant>(RHSConstant)));
788 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
789 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
790 return wrap(ConstantExpr::getGetElementPtr(
791 unwrap<Constant>(ConstantVal),
792 unwrap<Constant>(ConstantIndices,
797 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
798 LLVMValueRef *ConstantIndices,
799 unsigned NumIndices) {
800 Constant* Val = unwrap<Constant>(ConstantVal);
801 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
802 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
805 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
806 return wrap(ConstantExpr::getTrunc(
807 unwrap<Constant>(ConstantVal),
811 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
812 return wrap(ConstantExpr::getSExt(
813 unwrap<Constant>(ConstantVal),
817 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
818 return wrap(ConstantExpr::getZExt(
819 unwrap<Constant>(ConstantVal),
823 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
824 return wrap(ConstantExpr::getFPTrunc(
825 unwrap<Constant>(ConstantVal),
829 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
830 return wrap(ConstantExpr::getFPExtend(
831 unwrap<Constant>(ConstantVal),
835 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
836 return wrap(ConstantExpr::getUIToFP(
837 unwrap<Constant>(ConstantVal),
841 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
842 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
846 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
847 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
851 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
852 return wrap(ConstantExpr::getFPToSI(
853 unwrap<Constant>(ConstantVal),
857 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
858 return wrap(ConstantExpr::getPtrToInt(
859 unwrap<Constant>(ConstantVal),
863 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864 return wrap(ConstantExpr::getIntToPtr(
865 unwrap<Constant>(ConstantVal),
869 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
870 return wrap(ConstantExpr::getBitCast(
871 unwrap<Constant>(ConstantVal),
875 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
876 LLVMTypeRef ToType) {
877 return wrap(ConstantExpr::getZExtOrBitCast(
878 unwrap<Constant>(ConstantVal),
882 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
883 LLVMTypeRef ToType) {
884 return wrap(ConstantExpr::getSExtOrBitCast(
885 unwrap<Constant>(ConstantVal),
889 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
890 LLVMTypeRef ToType) {
891 return wrap(ConstantExpr::getTruncOrBitCast(
892 unwrap<Constant>(ConstantVal),
896 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
897 LLVMTypeRef ToType) {
898 return wrap(ConstantExpr::getPointerCast(
899 unwrap<Constant>(ConstantVal),
903 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
905 return wrap(ConstantExpr::getIntegerCast(
906 unwrap<Constant>(ConstantVal),
911 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
912 return wrap(ConstantExpr::getFPCast(
913 unwrap<Constant>(ConstantVal),
917 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
918 LLVMValueRef ConstantIfTrue,
919 LLVMValueRef ConstantIfFalse) {
920 return wrap(ConstantExpr::getSelect(
921 unwrap<Constant>(ConstantCondition),
922 unwrap<Constant>(ConstantIfTrue),
923 unwrap<Constant>(ConstantIfFalse)));
926 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
927 LLVMValueRef IndexConstant) {
928 return wrap(ConstantExpr::getExtractElement(
929 unwrap<Constant>(VectorConstant),
930 unwrap<Constant>(IndexConstant)));
933 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
934 LLVMValueRef ElementValueConstant,
935 LLVMValueRef IndexConstant) {
936 return wrap(ConstantExpr::getInsertElement(
937 unwrap<Constant>(VectorConstant),
938 unwrap<Constant>(ElementValueConstant),
939 unwrap<Constant>(IndexConstant)));
942 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
943 LLVMValueRef VectorBConstant,
944 LLVMValueRef MaskConstant) {
945 return wrap(ConstantExpr::getShuffleVector(
946 unwrap<Constant>(VectorAConstant),
947 unwrap<Constant>(VectorBConstant),
948 unwrap<Constant>(MaskConstant)));
951 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
953 return wrap(ConstantExpr::getExtractValue(
954 unwrap<Constant>(AggConstant),
958 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
959 LLVMValueRef ElementValueConstant,
960 unsigned *IdxList, unsigned NumIdx) {
961 return wrap(ConstantExpr::getInsertValue(
962 unwrap<Constant>(AggConstant),
963 unwrap<Constant>(ElementValueConstant),
967 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
968 const char *Constraints,
969 LLVMBool HasSideEffects,
970 LLVMBool IsAlignStack) {
971 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
972 Constraints, HasSideEffects, IsAlignStack));
975 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
977 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
978 return wrap(unwrap<GlobalValue>(Global)->getParent());
981 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
982 return unwrap<GlobalValue>(Global)->isDeclaration();
985 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
986 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
988 assert(false && "Unhandled Linkage Type.");
989 case GlobalValue::ExternalLinkage:
990 return LLVMExternalLinkage;
991 case GlobalValue::AvailableExternallyLinkage:
992 return LLVMAvailableExternallyLinkage;
993 case GlobalValue::LinkOnceAnyLinkage:
994 return LLVMLinkOnceAnyLinkage;
995 case GlobalValue::LinkOnceODRLinkage:
996 return LLVMLinkOnceODRLinkage;
997 case GlobalValue::WeakAnyLinkage:
998 return LLVMWeakAnyLinkage;
999 case GlobalValue::WeakODRLinkage:
1000 return LLVMWeakODRLinkage;
1001 case GlobalValue::AppendingLinkage:
1002 return LLVMAppendingLinkage;
1003 case GlobalValue::InternalLinkage:
1004 return LLVMInternalLinkage;
1005 case GlobalValue::PrivateLinkage:
1006 return LLVMPrivateLinkage;
1007 case GlobalValue::LinkerPrivateLinkage:
1008 return LLVMLinkerPrivateLinkage;
1009 case GlobalValue::DLLImportLinkage:
1010 return LLVMDLLImportLinkage;
1011 case GlobalValue::DLLExportLinkage:
1012 return LLVMDLLExportLinkage;
1013 case GlobalValue::ExternalWeakLinkage:
1014 return LLVMExternalWeakLinkage;
1015 case GlobalValue::CommonLinkage:
1016 return LLVMCommonLinkage;
1019 // Should never get here.
1020 return static_cast<LLVMLinkage>(0);
1023 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1024 GlobalValue *GV = unwrap<GlobalValue>(Global);
1028 assert(false && "Unhandled Linkage Type.");
1029 case LLVMExternalLinkage:
1030 GV->setLinkage(GlobalValue::ExternalLinkage);
1032 case LLVMAvailableExternallyLinkage:
1033 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1035 case LLVMLinkOnceAnyLinkage:
1036 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1038 case LLVMLinkOnceODRLinkage:
1039 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1041 case LLVMWeakAnyLinkage:
1042 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1044 case LLVMWeakODRLinkage:
1045 GV->setLinkage(GlobalValue::WeakODRLinkage);
1047 case LLVMAppendingLinkage:
1048 GV->setLinkage(GlobalValue::AppendingLinkage);
1050 case LLVMInternalLinkage:
1051 GV->setLinkage(GlobalValue::InternalLinkage);
1053 case LLVMPrivateLinkage:
1054 GV->setLinkage(GlobalValue::PrivateLinkage);
1056 case LLVMLinkerPrivateLinkage:
1057 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1059 case LLVMDLLImportLinkage:
1060 GV->setLinkage(GlobalValue::DLLImportLinkage);
1062 case LLVMDLLExportLinkage:
1063 GV->setLinkage(GlobalValue::DLLExportLinkage);
1065 case LLVMExternalWeakLinkage:
1066 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1068 case LLVMGhostLinkage:
1070 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1072 case LLVMCommonLinkage:
1073 GV->setLinkage(GlobalValue::CommonLinkage);
1078 const char *LLVMGetSection(LLVMValueRef Global) {
1079 return unwrap<GlobalValue>(Global)->getSection().c_str();
1082 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1083 unwrap<GlobalValue>(Global)->setSection(Section);
1086 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1087 return static_cast<LLVMVisibility>(
1088 unwrap<GlobalValue>(Global)->getVisibility());
1091 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1092 unwrap<GlobalValue>(Global)
1093 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1096 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1097 return unwrap<GlobalValue>(Global)->getAlignment();
1100 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1101 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1104 /*--.. Operations on global variables ......................................--*/
1106 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1107 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1108 GlobalValue::ExternalLinkage, 0, Name));
1111 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1112 return wrap(unwrap(M)->getNamedGlobal(Name));
1115 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1116 Module *Mod = unwrap(M);
1117 Module::global_iterator I = Mod->global_begin();
1118 if (I == Mod->global_end())
1123 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1124 Module *Mod = unwrap(M);
1125 Module::global_iterator I = Mod->global_end();
1126 if (I == Mod->global_begin())
1131 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1132 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1133 Module::global_iterator I = GV;
1134 if (++I == GV->getParent()->global_end())
1139 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1140 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1141 Module::global_iterator I = GV;
1142 if (I == GV->getParent()->global_begin())
1147 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1148 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1151 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1152 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1153 if ( !GV->hasInitializer() )
1155 return wrap(GV->getInitializer());
1158 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1159 unwrap<GlobalVariable>(GlobalVar)
1160 ->setInitializer(unwrap<Constant>(ConstantVal));
1163 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1164 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1167 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1168 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1171 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1172 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1175 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1176 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1179 /*--.. Operations on aliases ......................................--*/
1181 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1183 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1184 unwrap<Constant>(Aliasee), unwrap (M)));
1187 /*--.. Operations on functions .............................................--*/
1189 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1190 LLVMTypeRef FunctionTy) {
1191 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1192 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1195 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1196 return wrap(unwrap(M)->getFunction(Name));
1199 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1200 Module *Mod = unwrap(M);
1201 Module::iterator I = Mod->begin();
1202 if (I == Mod->end())
1207 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1208 Module *Mod = unwrap(M);
1209 Module::iterator I = Mod->end();
1210 if (I == Mod->begin())
1215 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1216 Function *Func = unwrap<Function>(Fn);
1217 Module::iterator I = Func;
1218 if (++I == Func->getParent()->end())
1223 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1224 Function *Func = unwrap<Function>(Fn);
1225 Module::iterator I = Func;
1226 if (I == Func->getParent()->begin())
1231 void LLVMDeleteFunction(LLVMValueRef Fn) {
1232 unwrap<Function>(Fn)->eraseFromParent();
1235 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1236 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1237 return F->getIntrinsicID();
1241 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1242 return unwrap<Function>(Fn)->getCallingConv();
1245 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1246 return unwrap<Function>(Fn)->setCallingConv(
1247 static_cast<CallingConv::ID>(CC));
1250 const char *LLVMGetGC(LLVMValueRef Fn) {
1251 Function *F = unwrap<Function>(Fn);
1252 return F->hasGC()? F->getGC() : 0;
1255 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1256 Function *F = unwrap<Function>(Fn);
1263 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1264 Function *Func = unwrap<Function>(Fn);
1265 const AttrListPtr PAL = Func->getAttributes();
1266 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1267 Func->setAttributes(PALnew);
1270 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1271 Function *Func = unwrap<Function>(Fn);
1272 const AttrListPtr PAL = Func->getAttributes();
1273 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1274 Func->setAttributes(PALnew);
1277 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1278 Function *Func = unwrap<Function>(Fn);
1279 const AttrListPtr PAL = Func->getAttributes();
1280 Attributes attr = PAL.getFnAttributes();
1281 return (LLVMAttribute)attr;
1284 /*--.. Operations on parameters ............................................--*/
1286 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1287 // This function is strictly redundant to
1288 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1289 return unwrap<Function>(FnRef)->arg_size();
1292 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1293 Function *Fn = unwrap<Function>(FnRef);
1294 for (Function::arg_iterator I = Fn->arg_begin(),
1295 E = Fn->arg_end(); I != E; I++)
1296 *ParamRefs++ = wrap(I);
1299 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1300 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1306 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1307 return wrap(unwrap<Argument>(V)->getParent());
1310 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1311 Function *Func = unwrap<Function>(Fn);
1312 Function::arg_iterator I = Func->arg_begin();
1313 if (I == Func->arg_end())
1318 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1319 Function *Func = unwrap<Function>(Fn);
1320 Function::arg_iterator I = Func->arg_end();
1321 if (I == Func->arg_begin())
1326 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1327 Argument *A = unwrap<Argument>(Arg);
1328 Function::arg_iterator I = A;
1329 if (++I == A->getParent()->arg_end())
1334 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1335 Argument *A = unwrap<Argument>(Arg);
1336 Function::arg_iterator I = A;
1337 if (I == A->getParent()->arg_begin())
1342 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1343 unwrap<Argument>(Arg)->addAttr(PA);
1346 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1347 unwrap<Argument>(Arg)->removeAttr(PA);
1350 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1351 Argument *A = unwrap<Argument>(Arg);
1352 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1354 return (LLVMAttribute)attr;
1358 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1359 unwrap<Argument>(Arg)->addAttr(
1360 Attribute::constructAlignmentFromInt(align));
1363 /*--.. Operations on basic blocks ..........................................--*/
1365 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1366 return wrap(static_cast<Value*>(unwrap(BB)));
1369 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1370 return isa<BasicBlock>(unwrap(Val));
1373 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1374 return wrap(unwrap<BasicBlock>(Val));
1377 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1378 return wrap(unwrap(BB)->getParent());
1381 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1382 return unwrap<Function>(FnRef)->size();
1385 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1386 Function *Fn = unwrap<Function>(FnRef);
1387 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1388 *BasicBlocksRefs++ = wrap(I);
1391 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1392 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1395 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1396 Function *Func = unwrap<Function>(Fn);
1397 Function::iterator I = Func->begin();
1398 if (I == Func->end())
1403 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1404 Function *Func = unwrap<Function>(Fn);
1405 Function::iterator I = Func->end();
1406 if (I == Func->begin())
1411 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1412 BasicBlock *Block = unwrap(BB);
1413 Function::iterator I = Block;
1414 if (++I == Block->getParent()->end())
1419 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1420 BasicBlock *Block = unwrap(BB);
1421 Function::iterator I = Block;
1422 if (I == Block->getParent()->begin())
1427 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1430 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1433 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1434 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1437 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1438 LLVMBasicBlockRef BBRef,
1440 BasicBlock *BB = unwrap(BBRef);
1441 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1444 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1446 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1449 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1450 unwrap(BBRef)->eraseFromParent();
1453 /*--.. Operations on instructions ..........................................--*/
1455 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1456 return wrap(unwrap<Instruction>(Inst)->getParent());
1459 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1460 BasicBlock *Block = unwrap(BB);
1461 BasicBlock::iterator I = Block->begin();
1462 if (I == Block->end())
1467 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1468 BasicBlock *Block = unwrap(BB);
1469 BasicBlock::iterator I = Block->end();
1470 if (I == Block->begin())
1475 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1476 Instruction *Instr = unwrap<Instruction>(Inst);
1477 BasicBlock::iterator I = Instr;
1478 if (++I == Instr->getParent()->end())
1483 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1484 Instruction *Instr = unwrap<Instruction>(Inst);
1485 BasicBlock::iterator I = Instr;
1486 if (I == Instr->getParent()->begin())
1491 /*--.. Call and invoke instructions ........................................--*/
1493 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1494 Value *V = unwrap(Instr);
1495 if (CallInst *CI = dyn_cast<CallInst>(V))
1496 return CI->getCallingConv();
1497 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1498 return II->getCallingConv();
1499 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1503 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1504 Value *V = unwrap(Instr);
1505 if (CallInst *CI = dyn_cast<CallInst>(V))
1506 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1507 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1508 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1509 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1512 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1514 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1516 Call.getAttributes().addAttr(index, PA));
1519 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1521 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1523 Call.getAttributes().removeAttr(index, PA));
1526 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1528 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1530 Call.getAttributes().addAttr(index,
1531 Attribute::constructAlignmentFromInt(align)));
1534 /*--.. Operations on call instructions (only) ..............................--*/
1536 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1537 return unwrap<CallInst>(Call)->isTailCall();
1540 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1541 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1544 /*--.. Operations on phi nodes .............................................--*/
1546 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1547 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1548 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1549 for (unsigned I = 0; I != Count; ++I)
1550 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1553 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1554 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1557 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1558 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1561 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1562 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1566 /*===-- Instruction builders ----------------------------------------------===*/
1568 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1569 return wrap(new IRBuilder<>(*unwrap(C)));
1572 LLVMBuilderRef LLVMCreateBuilder(void) {
1573 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1576 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1577 LLVMValueRef Instr) {
1578 BasicBlock *BB = unwrap(Block);
1579 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1580 unwrap(Builder)->SetInsertPoint(BB, I);
1583 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1584 Instruction *I = unwrap<Instruction>(Instr);
1585 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1588 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1589 BasicBlock *BB = unwrap(Block);
1590 unwrap(Builder)->SetInsertPoint(BB);
1593 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1594 return wrap(unwrap(Builder)->GetInsertBlock());
1597 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1598 unwrap(Builder)->ClearInsertionPoint ();
1601 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1602 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1605 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1607 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1610 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1611 delete unwrap(Builder);
1614 /*--.. Instruction builders ................................................--*/
1616 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1617 return wrap(unwrap(B)->CreateRetVoid());
1620 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1621 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1624 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1626 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1629 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1630 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1633 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1634 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1635 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1638 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1639 LLVMBasicBlockRef Else, unsigned NumCases) {
1640 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1643 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1644 LLVMValueRef *Args, unsigned NumArgs,
1645 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1647 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1648 unwrap(Args), unwrap(Args) + NumArgs,
1652 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1653 return wrap(unwrap(B)->CreateUnwind());
1656 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1657 return wrap(unwrap(B)->CreateUnreachable());
1660 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1661 LLVMBasicBlockRef Dest) {
1662 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1665 /*--.. Arithmetic ..........................................................--*/
1667 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1669 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1672 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1674 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1677 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1679 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1682 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1684 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1687 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1689 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1692 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1694 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1697 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1699 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1702 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1704 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1707 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1709 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1712 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1714 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1717 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1719 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1722 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1724 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1727 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1729 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1732 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1734 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1737 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1738 LLVMValueRef RHS, const char *Name) {
1739 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1742 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1744 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1747 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1749 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1752 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1754 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1757 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1759 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1762 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1764 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1767 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1769 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1772 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1774 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1777 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1779 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1782 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1784 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1787 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1789 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1792 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1793 LLVMValueRef LHS, LLVMValueRef RHS,
1795 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1796 unwrap(RHS), Name));
1799 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1800 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1803 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1805 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1808 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1810 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1813 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1814 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1817 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1818 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1821 /*--.. Memory ..............................................................--*/
1823 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1825 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1826 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1827 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1828 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1829 ITy, unwrap(Ty), AllocSize,
1831 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1834 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1835 LLVMValueRef Val, const char *Name) {
1836 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1837 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1838 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1839 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1840 ITy, unwrap(Ty), AllocSize,
1841 unwrap(Val), 0, "");
1842 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1845 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1847 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1850 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1851 LLVMValueRef Val, const char *Name) {
1852 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1855 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1856 return wrap(unwrap(B)->Insert(
1857 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1861 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1863 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1866 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1867 LLVMValueRef PointerVal) {
1868 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1871 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1872 LLVMValueRef *Indices, unsigned NumIndices,
1874 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1875 unwrap(Indices) + NumIndices, Name));
1878 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1879 LLVMValueRef *Indices, unsigned NumIndices,
1881 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1882 unwrap(Indices) + NumIndices, Name));
1885 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1886 unsigned Idx, const char *Name) {
1887 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1890 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1892 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1895 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1897 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1900 /*--.. Casts ...............................................................--*/
1902 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1903 LLVMTypeRef DestTy, const char *Name) {
1904 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1907 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1908 LLVMTypeRef DestTy, const char *Name) {
1909 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1912 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1913 LLVMTypeRef DestTy, const char *Name) {
1914 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1917 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1918 LLVMTypeRef DestTy, const char *Name) {
1919 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1922 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1923 LLVMTypeRef DestTy, const char *Name) {
1924 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1927 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1928 LLVMTypeRef DestTy, const char *Name) {
1929 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1932 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1933 LLVMTypeRef DestTy, const char *Name) {
1934 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1937 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1938 LLVMTypeRef DestTy, const char *Name) {
1939 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1942 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1943 LLVMTypeRef DestTy, const char *Name) {
1944 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1947 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1948 LLVMTypeRef DestTy, const char *Name) {
1949 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1952 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1953 LLVMTypeRef DestTy, const char *Name) {
1954 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1957 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1958 LLVMTypeRef DestTy, const char *Name) {
1959 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1962 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1963 LLVMTypeRef DestTy, const char *Name) {
1964 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1968 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1969 LLVMTypeRef DestTy, const char *Name) {
1970 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1974 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1975 LLVMTypeRef DestTy, const char *Name) {
1976 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1980 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
1981 LLVMTypeRef DestTy, const char *Name) {
1982 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
1983 unwrap(DestTy), Name));
1986 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1987 LLVMTypeRef DestTy, const char *Name) {
1988 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1991 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1992 LLVMTypeRef DestTy, const char *Name) {
1993 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
1994 /*isSigned*/true, Name));
1997 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1998 LLVMTypeRef DestTy, const char *Name) {
1999 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2002 /*--.. Comparisons .........................................................--*/
2004 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2005 LLVMValueRef LHS, LLVMValueRef RHS,
2007 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2008 unwrap(LHS), unwrap(RHS), Name));
2011 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2012 LLVMValueRef LHS, LLVMValueRef RHS,
2014 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2015 unwrap(LHS), unwrap(RHS), Name));
2018 /*--.. Miscellaneous instructions ..........................................--*/
2020 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2021 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2024 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2025 LLVMValueRef *Args, unsigned NumArgs,
2027 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2028 unwrap(Args) + NumArgs, Name));
2031 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2032 LLVMValueRef Then, LLVMValueRef Else,
2034 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2038 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2039 LLVMTypeRef Ty, const char *Name) {
2040 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2043 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2044 LLVMValueRef Index, const char *Name) {
2045 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2049 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2050 LLVMValueRef EltVal, LLVMValueRef Index,
2052 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2053 unwrap(Index), Name));
2056 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2057 LLVMValueRef V2, LLVMValueRef Mask,
2059 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2060 unwrap(Mask), Name));
2063 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2064 unsigned Index, const char *Name) {
2065 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2068 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2069 LLVMValueRef EltVal, unsigned Index,
2071 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2075 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2077 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2080 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2082 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2085 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2086 LLVMValueRef RHS, const char *Name) {
2087 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2091 /*===-- Module providers --------------------------------------------------===*/
2093 LLVMModuleProviderRef
2094 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2095 return reinterpret_cast<LLVMModuleProviderRef>(M);
2098 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2103 /*===-- Memory buffers ----------------------------------------------------===*/
2105 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2107 LLVMMemoryBufferRef *OutMemBuf,
2108 char **OutMessage) {
2111 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2112 *OutMemBuf = wrap(MB);
2116 *OutMessage = strdup(Error.c_str());
2120 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2121 char **OutMessage) {
2122 MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2123 if (!MB->getBufferSize()) {
2125 *OutMessage = strdup("stdin is empty.");
2129 *OutMemBuf = wrap(MB);
2133 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2134 delete unwrap(MemBuf);