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 LLVMConstFNeg(LLVMValueRef ConstantVal) {
598 return wrap(ConstantExpr::getFNeg(
599 unwrap<Constant>(ConstantVal)));
602 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
603 return wrap(ConstantExpr::getNot(
604 unwrap<Constant>(ConstantVal)));
607 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
608 return wrap(ConstantExpr::getAdd(
609 unwrap<Constant>(LHSConstant),
610 unwrap<Constant>(RHSConstant)));
613 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
614 LLVMValueRef RHSConstant) {
615 return wrap(ConstantExpr::getNSWAdd(
616 unwrap<Constant>(LHSConstant),
617 unwrap<Constant>(RHSConstant)));
620 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
621 return wrap(ConstantExpr::getFAdd(
622 unwrap<Constant>(LHSConstant),
623 unwrap<Constant>(RHSConstant)));
626 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
627 return wrap(ConstantExpr::getSub(
628 unwrap<Constant>(LHSConstant),
629 unwrap<Constant>(RHSConstant)));
632 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
633 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
634 unwrap<Constant>(RHSConstant)));
637 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
638 return wrap(ConstantExpr::getMul(
639 unwrap<Constant>(LHSConstant),
640 unwrap<Constant>(RHSConstant)));
643 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
644 return wrap(ConstantExpr::getFMul(
645 unwrap<Constant>(LHSConstant),
646 unwrap<Constant>(RHSConstant)));
649 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
650 return wrap(ConstantExpr::getUDiv(
651 unwrap<Constant>(LHSConstant),
652 unwrap<Constant>(RHSConstant)));
655 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
656 return wrap(ConstantExpr::getSDiv(
657 unwrap<Constant>(LHSConstant),
658 unwrap<Constant>(RHSConstant)));
661 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
662 LLVMValueRef RHSConstant) {
663 return wrap(ConstantExpr::getExactSDiv(
664 unwrap<Constant>(LHSConstant),
665 unwrap<Constant>(RHSConstant)));
668 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
669 return wrap(ConstantExpr::getFDiv(
670 unwrap<Constant>(LHSConstant),
671 unwrap<Constant>(RHSConstant)));
674 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
675 return wrap(ConstantExpr::getURem(
676 unwrap<Constant>(LHSConstant),
677 unwrap<Constant>(RHSConstant)));
680 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
681 return wrap(ConstantExpr::getSRem(
682 unwrap<Constant>(LHSConstant),
683 unwrap<Constant>(RHSConstant)));
686 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
687 return wrap(ConstantExpr::getFRem(
688 unwrap<Constant>(LHSConstant),
689 unwrap<Constant>(RHSConstant)));
692 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
693 return wrap(ConstantExpr::getAnd(
694 unwrap<Constant>(LHSConstant),
695 unwrap<Constant>(RHSConstant)));
698 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
699 return wrap(ConstantExpr::getOr(
700 unwrap<Constant>(LHSConstant),
701 unwrap<Constant>(RHSConstant)));
704 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
705 return wrap(ConstantExpr::getXor(
706 unwrap<Constant>(LHSConstant),
707 unwrap<Constant>(RHSConstant)));
710 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
711 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
712 return wrap(ConstantExpr::getICmp(Predicate,
713 unwrap<Constant>(LHSConstant),
714 unwrap<Constant>(RHSConstant)));
717 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
718 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
719 return wrap(ConstantExpr::getFCmp(Predicate,
720 unwrap<Constant>(LHSConstant),
721 unwrap<Constant>(RHSConstant)));
724 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
725 return wrap(ConstantExpr::getShl(
726 unwrap<Constant>(LHSConstant),
727 unwrap<Constant>(RHSConstant)));
730 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
731 return wrap(ConstantExpr::getLShr(
732 unwrap<Constant>(LHSConstant),
733 unwrap<Constant>(RHSConstant)));
736 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
737 return wrap(ConstantExpr::getAShr(
738 unwrap<Constant>(LHSConstant),
739 unwrap<Constant>(RHSConstant)));
742 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
743 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
744 return wrap(ConstantExpr::getGetElementPtr(
745 unwrap<Constant>(ConstantVal),
746 unwrap<Constant>(ConstantIndices,
751 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
752 LLVMValueRef *ConstantIndices,
753 unsigned NumIndices) {
754 Constant* Val = unwrap<Constant>(ConstantVal);
755 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
756 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
759 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
760 return wrap(ConstantExpr::getTrunc(
761 unwrap<Constant>(ConstantVal),
765 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
766 return wrap(ConstantExpr::getSExt(
767 unwrap<Constant>(ConstantVal),
771 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
772 return wrap(ConstantExpr::getZExt(
773 unwrap<Constant>(ConstantVal),
777 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
778 return wrap(ConstantExpr::getFPTrunc(
779 unwrap<Constant>(ConstantVal),
783 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
784 return wrap(ConstantExpr::getFPExtend(
785 unwrap<Constant>(ConstantVal),
789 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
790 return wrap(ConstantExpr::getUIToFP(
791 unwrap<Constant>(ConstantVal),
795 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
796 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
800 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
801 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
805 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
806 return wrap(ConstantExpr::getFPToSI(
807 unwrap<Constant>(ConstantVal),
811 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
812 return wrap(ConstantExpr::getPtrToInt(
813 unwrap<Constant>(ConstantVal),
817 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
818 return wrap(ConstantExpr::getIntToPtr(
819 unwrap<Constant>(ConstantVal),
823 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
824 return wrap(ConstantExpr::getBitCast(
825 unwrap<Constant>(ConstantVal),
829 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
830 LLVMTypeRef ToType) {
831 return wrap(ConstantExpr::getZExtOrBitCast(
832 unwrap<Constant>(ConstantVal),
836 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
837 LLVMTypeRef ToType) {
838 return wrap(ConstantExpr::getSExtOrBitCast(
839 unwrap<Constant>(ConstantVal),
843 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
844 LLVMTypeRef ToType) {
845 return wrap(ConstantExpr::getTruncOrBitCast(
846 unwrap<Constant>(ConstantVal),
850 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
851 LLVMTypeRef ToType) {
852 return wrap(ConstantExpr::getPointerCast(
853 unwrap<Constant>(ConstantVal),
857 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
859 return wrap(ConstantExpr::getIntegerCast(
860 unwrap<Constant>(ConstantVal),
865 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
866 return wrap(ConstantExpr::getFPCast(
867 unwrap<Constant>(ConstantVal),
871 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
872 LLVMValueRef ConstantIfTrue,
873 LLVMValueRef ConstantIfFalse) {
874 return wrap(ConstantExpr::getSelect(
875 unwrap<Constant>(ConstantCondition),
876 unwrap<Constant>(ConstantIfTrue),
877 unwrap<Constant>(ConstantIfFalse)));
880 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
881 LLVMValueRef IndexConstant) {
882 return wrap(ConstantExpr::getExtractElement(
883 unwrap<Constant>(VectorConstant),
884 unwrap<Constant>(IndexConstant)));
887 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
888 LLVMValueRef ElementValueConstant,
889 LLVMValueRef IndexConstant) {
890 return wrap(ConstantExpr::getInsertElement(
891 unwrap<Constant>(VectorConstant),
892 unwrap<Constant>(ElementValueConstant),
893 unwrap<Constant>(IndexConstant)));
896 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
897 LLVMValueRef VectorBConstant,
898 LLVMValueRef MaskConstant) {
899 return wrap(ConstantExpr::getShuffleVector(
900 unwrap<Constant>(VectorAConstant),
901 unwrap<Constant>(VectorBConstant),
902 unwrap<Constant>(MaskConstant)));
905 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
907 return wrap(ConstantExpr::getExtractValue(
908 unwrap<Constant>(AggConstant),
912 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
913 LLVMValueRef ElementValueConstant,
914 unsigned *IdxList, unsigned NumIdx) {
915 return wrap(ConstantExpr::getInsertValue(
916 unwrap<Constant>(AggConstant),
917 unwrap<Constant>(ElementValueConstant),
921 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
922 const char *Constraints,
923 LLVMBool HasSideEffects,
924 LLVMBool IsAlignStack) {
925 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
926 Constraints, HasSideEffects, IsAlignStack));
929 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
931 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
932 return wrap(unwrap<GlobalValue>(Global)->getParent());
935 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
936 return unwrap<GlobalValue>(Global)->isDeclaration();
939 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
940 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
942 assert(false && "Unhandled Linkage Type.");
943 case GlobalValue::ExternalLinkage:
944 return LLVMExternalLinkage;
945 case GlobalValue::AvailableExternallyLinkage:
946 return LLVMAvailableExternallyLinkage;
947 case GlobalValue::LinkOnceAnyLinkage:
948 return LLVMLinkOnceAnyLinkage;
949 case GlobalValue::LinkOnceODRLinkage:
950 return LLVMLinkOnceODRLinkage;
951 case GlobalValue::WeakAnyLinkage:
952 return LLVMWeakAnyLinkage;
953 case GlobalValue::WeakODRLinkage:
954 return LLVMWeakODRLinkage;
955 case GlobalValue::AppendingLinkage:
956 return LLVMAppendingLinkage;
957 case GlobalValue::InternalLinkage:
958 return LLVMInternalLinkage;
959 case GlobalValue::PrivateLinkage:
960 return LLVMPrivateLinkage;
961 case GlobalValue::LinkerPrivateLinkage:
962 return LLVMLinkerPrivateLinkage;
963 case GlobalValue::DLLImportLinkage:
964 return LLVMDLLImportLinkage;
965 case GlobalValue::DLLExportLinkage:
966 return LLVMDLLExportLinkage;
967 case GlobalValue::ExternalWeakLinkage:
968 return LLVMExternalWeakLinkage;
969 case GlobalValue::CommonLinkage:
970 return LLVMCommonLinkage;
973 // Should never get here.
974 return static_cast<LLVMLinkage>(0);
977 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
978 GlobalValue *GV = unwrap<GlobalValue>(Global);
982 assert(false && "Unhandled Linkage Type.");
983 case LLVMExternalLinkage:
984 GV->setLinkage(GlobalValue::ExternalLinkage);
986 case LLVMAvailableExternallyLinkage:
987 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
989 case LLVMLinkOnceAnyLinkage:
990 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
992 case LLVMLinkOnceODRLinkage:
993 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
995 case LLVMWeakAnyLinkage:
996 GV->setLinkage(GlobalValue::WeakAnyLinkage);
998 case LLVMWeakODRLinkage:
999 GV->setLinkage(GlobalValue::WeakODRLinkage);
1001 case LLVMAppendingLinkage:
1002 GV->setLinkage(GlobalValue::AppendingLinkage);
1004 case LLVMInternalLinkage:
1005 GV->setLinkage(GlobalValue::InternalLinkage);
1007 case LLVMPrivateLinkage:
1008 GV->setLinkage(GlobalValue::PrivateLinkage);
1010 case LLVMLinkerPrivateLinkage:
1011 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1013 case LLVMDLLImportLinkage:
1014 GV->setLinkage(GlobalValue::DLLImportLinkage);
1016 case LLVMDLLExportLinkage:
1017 GV->setLinkage(GlobalValue::DLLExportLinkage);
1019 case LLVMExternalWeakLinkage:
1020 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1022 case LLVMGhostLinkage:
1024 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1026 case LLVMCommonLinkage:
1027 GV->setLinkage(GlobalValue::CommonLinkage);
1032 const char *LLVMGetSection(LLVMValueRef Global) {
1033 return unwrap<GlobalValue>(Global)->getSection().c_str();
1036 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1037 unwrap<GlobalValue>(Global)->setSection(Section);
1040 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1041 return static_cast<LLVMVisibility>(
1042 unwrap<GlobalValue>(Global)->getVisibility());
1045 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1046 unwrap<GlobalValue>(Global)
1047 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1050 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1051 return unwrap<GlobalValue>(Global)->getAlignment();
1054 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1055 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1058 /*--.. Operations on global variables ......................................--*/
1060 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1061 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1062 GlobalValue::ExternalLinkage, 0, Name));
1065 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1066 return wrap(unwrap(M)->getNamedGlobal(Name));
1069 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1070 Module *Mod = unwrap(M);
1071 Module::global_iterator I = Mod->global_begin();
1072 if (I == Mod->global_end())
1077 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1078 Module *Mod = unwrap(M);
1079 Module::global_iterator I = Mod->global_end();
1080 if (I == Mod->global_begin())
1085 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1086 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1087 Module::global_iterator I = GV;
1088 if (++I == GV->getParent()->global_end())
1093 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1094 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1095 Module::global_iterator I = GV;
1096 if (I == GV->getParent()->global_begin())
1101 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1102 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1105 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1106 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1107 if ( !GV->hasInitializer() )
1109 return wrap(GV->getInitializer());
1112 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1113 unwrap<GlobalVariable>(GlobalVar)
1114 ->setInitializer(unwrap<Constant>(ConstantVal));
1117 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1118 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1121 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1122 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1125 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1126 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1129 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1130 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1133 /*--.. Operations on aliases ......................................--*/
1135 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1137 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1138 unwrap<Constant>(Aliasee), unwrap (M)));
1141 /*--.. Operations on functions .............................................--*/
1143 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1144 LLVMTypeRef FunctionTy) {
1145 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1146 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1149 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1150 return wrap(unwrap(M)->getFunction(Name));
1153 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1154 Module *Mod = unwrap(M);
1155 Module::iterator I = Mod->begin();
1156 if (I == Mod->end())
1161 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1162 Module *Mod = unwrap(M);
1163 Module::iterator I = Mod->end();
1164 if (I == Mod->begin())
1169 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1170 Function *Func = unwrap<Function>(Fn);
1171 Module::iterator I = Func;
1172 if (++I == Func->getParent()->end())
1177 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1178 Function *Func = unwrap<Function>(Fn);
1179 Module::iterator I = Func;
1180 if (I == Func->getParent()->begin())
1185 void LLVMDeleteFunction(LLVMValueRef Fn) {
1186 unwrap<Function>(Fn)->eraseFromParent();
1189 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1190 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1191 return F->getIntrinsicID();
1195 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1196 return unwrap<Function>(Fn)->getCallingConv();
1199 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1200 return unwrap<Function>(Fn)->setCallingConv(
1201 static_cast<CallingConv::ID>(CC));
1204 const char *LLVMGetGC(LLVMValueRef Fn) {
1205 Function *F = unwrap<Function>(Fn);
1206 return F->hasGC()? F->getGC() : 0;
1209 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1210 Function *F = unwrap<Function>(Fn);
1217 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1218 Function *Func = unwrap<Function>(Fn);
1219 const AttrListPtr PAL = Func->getAttributes();
1220 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1221 Func->setAttributes(PALnew);
1224 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1225 Function *Func = unwrap<Function>(Fn);
1226 const AttrListPtr PAL = Func->getAttributes();
1227 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1228 Func->setAttributes(PALnew);
1231 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1232 Function *Func = unwrap<Function>(Fn);
1233 const AttrListPtr PAL = Func->getAttributes();
1234 Attributes attr = PAL.getFnAttributes();
1235 return (LLVMAttribute)attr;
1238 /*--.. Operations on parameters ............................................--*/
1240 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1241 // This function is strictly redundant to
1242 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1243 return unwrap<Function>(FnRef)->arg_size();
1246 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1247 Function *Fn = unwrap<Function>(FnRef);
1248 for (Function::arg_iterator I = Fn->arg_begin(),
1249 E = Fn->arg_end(); I != E; I++)
1250 *ParamRefs++ = wrap(I);
1253 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1254 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1260 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1261 return wrap(unwrap<Argument>(V)->getParent());
1264 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1265 Function *Func = unwrap<Function>(Fn);
1266 Function::arg_iterator I = Func->arg_begin();
1267 if (I == Func->arg_end())
1272 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1273 Function *Func = unwrap<Function>(Fn);
1274 Function::arg_iterator I = Func->arg_end();
1275 if (I == Func->arg_begin())
1280 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1281 Argument *A = unwrap<Argument>(Arg);
1282 Function::arg_iterator I = A;
1283 if (++I == A->getParent()->arg_end())
1288 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1289 Argument *A = unwrap<Argument>(Arg);
1290 Function::arg_iterator I = A;
1291 if (I == A->getParent()->arg_begin())
1296 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1297 unwrap<Argument>(Arg)->addAttr(PA);
1300 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1301 unwrap<Argument>(Arg)->removeAttr(PA);
1304 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1305 Argument *A = unwrap<Argument>(Arg);
1306 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1308 return (LLVMAttribute)attr;
1312 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1313 unwrap<Argument>(Arg)->addAttr(
1314 Attribute::constructAlignmentFromInt(align));
1317 /*--.. Operations on basic blocks ..........................................--*/
1319 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1320 return wrap(static_cast<Value*>(unwrap(BB)));
1323 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1324 return isa<BasicBlock>(unwrap(Val));
1327 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1328 return wrap(unwrap<BasicBlock>(Val));
1331 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1332 return wrap(unwrap(BB)->getParent());
1335 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1336 return unwrap<Function>(FnRef)->size();
1339 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1340 Function *Fn = unwrap<Function>(FnRef);
1341 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1342 *BasicBlocksRefs++ = wrap(I);
1345 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1346 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1349 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1350 Function *Func = unwrap<Function>(Fn);
1351 Function::iterator I = Func->begin();
1352 if (I == Func->end())
1357 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1358 Function *Func = unwrap<Function>(Fn);
1359 Function::iterator I = Func->end();
1360 if (I == Func->begin())
1365 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1366 BasicBlock *Block = unwrap(BB);
1367 Function::iterator I = Block;
1368 if (++I == Block->getParent()->end())
1373 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1374 BasicBlock *Block = unwrap(BB);
1375 Function::iterator I = Block;
1376 if (I == Block->getParent()->begin())
1381 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1384 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1387 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1388 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1391 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1392 LLVMBasicBlockRef BBRef,
1394 BasicBlock *BB = unwrap(BBRef);
1395 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1398 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1400 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1403 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1404 unwrap(BBRef)->eraseFromParent();
1407 /*--.. Operations on instructions ..........................................--*/
1409 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1410 return wrap(unwrap<Instruction>(Inst)->getParent());
1413 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1414 BasicBlock *Block = unwrap(BB);
1415 BasicBlock::iterator I = Block->begin();
1416 if (I == Block->end())
1421 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1422 BasicBlock *Block = unwrap(BB);
1423 BasicBlock::iterator I = Block->end();
1424 if (I == Block->begin())
1429 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1430 Instruction *Instr = unwrap<Instruction>(Inst);
1431 BasicBlock::iterator I = Instr;
1432 if (++I == Instr->getParent()->end())
1437 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1438 Instruction *Instr = unwrap<Instruction>(Inst);
1439 BasicBlock::iterator I = Instr;
1440 if (I == Instr->getParent()->begin())
1445 /*--.. Call and invoke instructions ........................................--*/
1447 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1448 Value *V = unwrap(Instr);
1449 if (CallInst *CI = dyn_cast<CallInst>(V))
1450 return CI->getCallingConv();
1451 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1452 return II->getCallingConv();
1453 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1457 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1458 Value *V = unwrap(Instr);
1459 if (CallInst *CI = dyn_cast<CallInst>(V))
1460 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1461 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1462 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1463 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1466 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1468 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1470 Call.getAttributes().addAttr(index, PA));
1473 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1475 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1477 Call.getAttributes().removeAttr(index, PA));
1480 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1482 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1484 Call.getAttributes().addAttr(index,
1485 Attribute::constructAlignmentFromInt(align)));
1488 /*--.. Operations on call instructions (only) ..............................--*/
1490 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1491 return unwrap<CallInst>(Call)->isTailCall();
1494 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1495 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1498 /*--.. Operations on phi nodes .............................................--*/
1500 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1501 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1502 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1503 for (unsigned I = 0; I != Count; ++I)
1504 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1507 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1508 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1511 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1512 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1515 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1516 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1520 /*===-- Instruction builders ----------------------------------------------===*/
1522 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1523 return wrap(new IRBuilder<>(*unwrap(C)));
1526 LLVMBuilderRef LLVMCreateBuilder(void) {
1527 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1530 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1531 LLVMValueRef Instr) {
1532 BasicBlock *BB = unwrap(Block);
1533 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1534 unwrap(Builder)->SetInsertPoint(BB, I);
1537 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1538 Instruction *I = unwrap<Instruction>(Instr);
1539 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1542 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1543 BasicBlock *BB = unwrap(Block);
1544 unwrap(Builder)->SetInsertPoint(BB);
1547 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1548 return wrap(unwrap(Builder)->GetInsertBlock());
1551 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1552 unwrap(Builder)->ClearInsertionPoint ();
1555 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1556 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1559 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1561 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1564 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1565 delete unwrap(Builder);
1568 /*--.. Instruction builders ................................................--*/
1570 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1571 return wrap(unwrap(B)->CreateRetVoid());
1574 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1575 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1578 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1580 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1583 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1584 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1587 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1588 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1589 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1592 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1593 LLVMBasicBlockRef Else, unsigned NumCases) {
1594 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1597 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1598 LLVMValueRef *Args, unsigned NumArgs,
1599 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1601 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1602 unwrap(Args), unwrap(Args) + NumArgs,
1606 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1607 return wrap(unwrap(B)->CreateUnwind());
1610 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1611 return wrap(unwrap(B)->CreateUnreachable());
1614 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1615 LLVMBasicBlockRef Dest) {
1616 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1619 /*--.. Arithmetic ..........................................................--*/
1621 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1623 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1626 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1628 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1631 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1633 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1636 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1638 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1641 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1643 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1646 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1648 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1651 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1653 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1656 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1658 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1661 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1663 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1666 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1667 LLVMValueRef RHS, const char *Name) {
1668 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1671 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1673 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1676 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1678 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1681 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1683 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1686 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1688 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1691 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1693 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1696 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1698 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1701 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1703 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1706 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1708 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1711 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1713 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1716 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1718 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1721 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1722 LLVMValueRef LHS, LLVMValueRef RHS,
1724 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1725 unwrap(RHS), Name));
1728 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1729 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1732 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1733 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1736 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1737 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1740 /*--.. Memory ..............................................................--*/
1742 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1744 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1745 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1746 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1747 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1748 ITy, unwrap(Ty), AllocSize,
1750 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1753 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1754 LLVMValueRef Val, const char *Name) {
1755 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1756 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1757 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1758 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1759 ITy, unwrap(Ty), AllocSize,
1760 unwrap(Val), 0, "");
1761 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1764 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1766 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1769 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1770 LLVMValueRef Val, const char *Name) {
1771 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1774 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1775 return wrap(unwrap(B)->Insert(
1776 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1780 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1782 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1785 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1786 LLVMValueRef PointerVal) {
1787 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1790 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1791 LLVMValueRef *Indices, unsigned NumIndices,
1793 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1794 unwrap(Indices) + NumIndices, Name));
1797 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1798 LLVMValueRef *Indices, unsigned NumIndices,
1800 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1801 unwrap(Indices) + NumIndices, Name));
1804 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1805 unsigned Idx, const char *Name) {
1806 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1809 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1811 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1814 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1816 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1819 /*--.. Casts ...............................................................--*/
1821 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1822 LLVMTypeRef DestTy, const char *Name) {
1823 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1826 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1827 LLVMTypeRef DestTy, const char *Name) {
1828 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1831 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1832 LLVMTypeRef DestTy, const char *Name) {
1833 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1836 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1837 LLVMTypeRef DestTy, const char *Name) {
1838 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1841 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1842 LLVMTypeRef DestTy, const char *Name) {
1843 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1846 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1847 LLVMTypeRef DestTy, const char *Name) {
1848 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1851 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1852 LLVMTypeRef DestTy, const char *Name) {
1853 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1856 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1857 LLVMTypeRef DestTy, const char *Name) {
1858 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1861 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1862 LLVMTypeRef DestTy, const char *Name) {
1863 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1866 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1867 LLVMTypeRef DestTy, const char *Name) {
1868 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1871 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1872 LLVMTypeRef DestTy, const char *Name) {
1873 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1876 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1877 LLVMTypeRef DestTy, const char *Name) {
1878 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1881 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1882 LLVMTypeRef DestTy, const char *Name) {
1883 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1887 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1888 LLVMTypeRef DestTy, const char *Name) {
1889 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1893 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1894 LLVMTypeRef DestTy, const char *Name) {
1895 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1899 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
1900 LLVMTypeRef DestTy, const char *Name) {
1901 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
1902 unwrap(DestTy), Name));
1905 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1906 LLVMTypeRef DestTy, const char *Name) {
1907 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1910 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1911 LLVMTypeRef DestTy, const char *Name) {
1912 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
1913 /*isSigned*/true, Name));
1916 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1917 LLVMTypeRef DestTy, const char *Name) {
1918 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
1921 /*--.. Comparisons .........................................................--*/
1923 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1924 LLVMValueRef LHS, LLVMValueRef RHS,
1926 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1927 unwrap(LHS), unwrap(RHS), Name));
1930 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1931 LLVMValueRef LHS, LLVMValueRef RHS,
1933 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1934 unwrap(LHS), unwrap(RHS), Name));
1937 /*--.. Miscellaneous instructions ..........................................--*/
1939 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1940 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1943 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1944 LLVMValueRef *Args, unsigned NumArgs,
1946 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1947 unwrap(Args) + NumArgs, Name));
1950 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1951 LLVMValueRef Then, LLVMValueRef Else,
1953 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1957 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1958 LLVMTypeRef Ty, const char *Name) {
1959 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1962 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1963 LLVMValueRef Index, const char *Name) {
1964 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1968 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1969 LLVMValueRef EltVal, LLVMValueRef Index,
1971 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1972 unwrap(Index), Name));
1975 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1976 LLVMValueRef V2, LLVMValueRef Mask,
1978 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1979 unwrap(Mask), Name));
1982 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1983 unsigned Index, const char *Name) {
1984 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1987 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1988 LLVMValueRef EltVal, unsigned Index,
1990 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1994 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
1996 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
1999 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2001 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2004 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2005 LLVMValueRef RHS, const char *Name) {
2006 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2010 /*===-- Module providers --------------------------------------------------===*/
2012 LLVMModuleProviderRef
2013 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2014 return reinterpret_cast<LLVMModuleProviderRef>(M);
2017 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2022 /*===-- Memory buffers ----------------------------------------------------===*/
2024 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2026 LLVMMemoryBufferRef *OutMemBuf,
2027 char **OutMessage) {
2030 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2031 *OutMemBuf = wrap(MB);
2035 *OutMessage = strdup(Error.c_str());
2039 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2040 char **OutMessage) {
2041 MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2042 if (!MB->getBufferSize()) {
2044 *OutMessage = strdup("stdin is empty.");
2048 *OutMemBuf = wrap(MB);
2052 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2053 delete unwrap(MemBuf);