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) {
58 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
60 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
63 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
64 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
68 /*===-- Operations on modules ---------------------------------------------===*/
70 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
71 return wrap(new Module(ModuleID, getGlobalContext()));
74 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
76 return wrap(new Module(ModuleID, *unwrap(C)));
79 void LLVMDisposeModule(LLVMModuleRef M) {
83 /*--.. Data layout .........................................................--*/
84 const char * LLVMGetDataLayout(LLVMModuleRef M) {
85 return unwrap(M)->getDataLayout().c_str();
88 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
89 unwrap(M)->setDataLayout(Triple);
92 /*--.. Target triple .......................................................--*/
93 const char * LLVMGetTarget(LLVMModuleRef M) {
94 return unwrap(M)->getTargetTriple().c_str();
97 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
98 unwrap(M)->setTargetTriple(Triple);
101 /*--.. Type names ..........................................................--*/
102 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
103 return unwrap(M)->addTypeName(Name, unwrap(Ty));
106 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
107 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
109 TypeSymbolTable::iterator I = TST.find(Name);
114 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
115 return wrap(unwrap(M)->getTypeByName(Name));
118 void LLVMDumpModule(LLVMModuleRef M) {
122 /*--.. Operations on inline assembler ......................................--*/
123 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
124 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
128 /*===-- Operations on types -----------------------------------------------===*/
130 /*--.. Operations on all types (mostly) ....................................--*/
132 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
133 switch (unwrap(Ty)->getTypeID()) {
135 assert(false && "Unhandled TypeID.");
137 return LLVMVoidTypeKind;
138 case Type::FloatTyID:
139 return LLVMFloatTypeKind;
140 case Type::DoubleTyID:
141 return LLVMDoubleTypeKind;
142 case Type::X86_FP80TyID:
143 return LLVMX86_FP80TypeKind;
144 case Type::FP128TyID:
145 return LLVMFP128TypeKind;
146 case Type::PPC_FP128TyID:
147 return LLVMPPC_FP128TypeKind;
148 case Type::LabelTyID:
149 return LLVMLabelTypeKind;
150 case Type::MetadataTyID:
151 return LLVMMetadataTypeKind;
152 case Type::IntegerTyID:
153 return LLVMIntegerTypeKind;
154 case Type::FunctionTyID:
155 return LLVMFunctionTypeKind;
156 case Type::StructTyID:
157 return LLVMStructTypeKind;
158 case Type::UnionTyID:
159 return LLVMUnionTypeKind;
160 case Type::ArrayTyID:
161 return LLVMArrayTypeKind;
162 case Type::PointerTyID:
163 return LLVMPointerTypeKind;
164 case Type::OpaqueTyID:
165 return LLVMOpaqueTypeKind;
166 case Type::VectorTyID:
167 return LLVMVectorTypeKind;
171 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
172 return wrap(&unwrap(Ty)->getContext());
175 /*--.. Operations on integer types .........................................--*/
177 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
178 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
180 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
181 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
183 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
184 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
186 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
187 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
189 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
190 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
192 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
193 return wrap(IntegerType::get(*unwrap(C), NumBits));
196 LLVMTypeRef LLVMInt1Type(void) {
197 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
199 LLVMTypeRef LLVMInt8Type(void) {
200 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
202 LLVMTypeRef LLVMInt16Type(void) {
203 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
205 LLVMTypeRef LLVMInt32Type(void) {
206 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
208 LLVMTypeRef LLVMInt64Type(void) {
209 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
211 LLVMTypeRef LLVMIntType(unsigned NumBits) {
212 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
215 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
216 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
219 /*--.. Operations on real types ............................................--*/
221 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
222 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
224 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
225 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
227 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
228 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
230 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
231 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
233 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
234 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
237 LLVMTypeRef LLVMFloatType(void) {
238 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
240 LLVMTypeRef LLVMDoubleType(void) {
241 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
243 LLVMTypeRef LLVMX86FP80Type(void) {
244 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
246 LLVMTypeRef LLVMFP128Type(void) {
247 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
249 LLVMTypeRef LLVMPPCFP128Type(void) {
250 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
253 /*--.. Operations on function types ........................................--*/
255 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
256 LLVMTypeRef *ParamTypes, unsigned ParamCount,
258 std::vector<const Type*> Tys;
259 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
260 Tys.push_back(unwrap(*I));
262 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
265 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
266 return unwrap<FunctionType>(FunctionTy)->isVarArg();
269 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
270 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
273 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
274 return unwrap<FunctionType>(FunctionTy)->getNumParams();
277 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
278 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
279 for (FunctionType::param_iterator I = Ty->param_begin(),
280 E = Ty->param_end(); I != E; ++I)
284 /*--.. Operations on struct types ..........................................--*/
286 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
287 unsigned ElementCount, LLVMBool Packed) {
288 std::vector<const Type*> Tys;
289 for (LLVMTypeRef *I = ElementTypes,
290 *E = ElementTypes + ElementCount; I != E; ++I)
291 Tys.push_back(unwrap(*I));
293 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
296 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
297 unsigned ElementCount, LLVMBool Packed) {
298 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
299 ElementCount, Packed);
303 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
304 return unwrap<StructType>(StructTy)->getNumElements();
307 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
308 StructType *Ty = unwrap<StructType>(StructTy);
309 for (FunctionType::param_iterator I = Ty->element_begin(),
310 E = Ty->element_end(); I != E; ++I)
314 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
315 return unwrap<StructType>(StructTy)->isPacked();
318 /*--.. Operations on union types ..........................................--*/
320 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
321 unsigned ElementCount) {
322 SmallVector<const Type*, 8> Tys;
323 for (LLVMTypeRef *I = ElementTypes,
324 *E = ElementTypes + ElementCount; I != E; ++I)
325 Tys.push_back(unwrap(*I));
327 return wrap(UnionType::get(&Tys[0], Tys.size()));
330 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes, unsigned ElementCount) {
331 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
335 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
336 return unwrap<UnionType>(UnionTy)->getNumElements();
339 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
340 UnionType *Ty = unwrap<UnionType>(UnionTy);
341 for (FunctionType::param_iterator I = Ty->element_begin(),
342 E = Ty->element_end(); I != E; ++I)
346 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
348 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
349 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
352 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
353 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
356 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
357 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
360 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
361 return wrap(unwrap<SequentialType>(Ty)->getElementType());
364 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
365 return unwrap<ArrayType>(ArrayTy)->getNumElements();
368 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
369 return unwrap<PointerType>(PointerTy)->getAddressSpace();
372 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
373 return unwrap<VectorType>(VectorTy)->getNumElements();
376 /*--.. Operations on other types ...........................................--*/
378 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
379 return wrap(Type::getVoidTy(*unwrap(C)));
381 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
382 return wrap(Type::getLabelTy(*unwrap(C)));
384 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
385 return wrap(OpaqueType::get(*unwrap(C)));
388 LLVMTypeRef LLVMVoidType(void) {
389 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
391 LLVMTypeRef LLVMLabelType(void) {
392 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
394 LLVMTypeRef LLVMOpaqueType(void) {
395 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
398 /*--.. Operations on type handles ..........................................--*/
400 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
401 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
404 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
405 delete unwrap(TypeHandle);
408 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
409 return wrap(unwrap(TypeHandle)->get());
412 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
413 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
417 /*===-- Operations on values ----------------------------------------------===*/
419 /*--.. Operations on all values ............................................--*/
421 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
422 return wrap(unwrap(Val)->getType());
425 const char *LLVMGetValueName(LLVMValueRef Val) {
426 return unwrap(Val)->getName().data();
429 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
430 unwrap(Val)->setName(Name);
433 void LLVMDumpValue(LLVMValueRef Val) {
437 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
438 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
441 int LLVMHasMetadata(LLVMValueRef Inst) {
442 return unwrap<Instruction>(Inst)->hasMetadata();
445 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
446 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
449 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
450 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
453 /*--.. Conversion functions ................................................--*/
455 #define LLVM_DEFINE_VALUE_CAST(name) \
456 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
457 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
460 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
462 /*--.. Operations on Uses ..................................................--*/
463 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
464 Value *V = unwrap(Val);
465 Value::use_iterator I = V->use_begin();
466 if (I == V->use_end())
468 return wrap(&(I.getUse()));
471 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
472 Use *Next = unwrap(U)->getNext();
478 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
479 return wrap(unwrap(U)->getUser());
482 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
483 return wrap(unwrap(U)->get());
486 /*--.. Operations on Users .................................................--*/
487 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
488 return wrap(unwrap<User>(Val)->getOperand(Index));
491 /*--.. Operations on constants of any type .................................--*/
493 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
494 return wrap(Constant::getNullValue(unwrap(Ty)));
497 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
498 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
501 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
502 return wrap(UndefValue::get(unwrap(Ty)));
505 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
506 return isa<Constant>(unwrap(Ty));
509 LLVMBool LLVMIsNull(LLVMValueRef Val) {
510 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
511 return C->isNullValue();
515 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
516 return isa<UndefValue>(unwrap(Val));
519 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
521 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
524 /*--.. Operations on metadata nodes ........................................--*/
526 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
528 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
531 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
532 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
535 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
537 return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
540 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
541 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
544 /*--.. Operations on scalar constants ......................................--*/
546 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
547 LLVMBool SignExtend) {
548 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
551 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
553 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
557 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
558 unsigned SLen, uint8_t Radix) {
559 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
563 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
564 return wrap(ConstantFP::get(unwrap(RealTy), N));
567 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
568 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
571 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
573 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
576 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
577 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
580 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
581 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
584 /*--.. Operations on composite constants ...................................--*/
586 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
588 LLVMBool DontNullTerminate) {
589 /* Inverted the sense of AddNull because ', 0)' is a
590 better mnemonic for null termination than ', 1)'. */
591 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
592 DontNullTerminate == 0));
594 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
595 LLVMValueRef *ConstantVals,
596 unsigned Count, LLVMBool Packed) {
597 return wrap(ConstantStruct::get(*unwrap(C),
598 unwrap<Constant>(ConstantVals, Count),
599 Count, Packed != 0));
602 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
603 LLVMBool DontNullTerminate) {
604 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
607 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
608 LLVMValueRef *ConstantVals, unsigned Length) {
609 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
610 unwrap<Constant>(ConstantVals, Length),
613 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
615 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
618 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
619 return wrap(ConstantVector::get(
620 unwrap<Constant>(ScalarConstantVals, Size), Size));
622 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
623 return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
626 /*--.. Constant expressions ................................................--*/
628 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
629 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
632 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
633 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
636 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
637 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
640 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
641 return wrap(ConstantExpr::getNeg(
642 unwrap<Constant>(ConstantVal)));
645 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
646 return wrap(ConstantExpr::getNSWNeg(
647 unwrap<Constant>(ConstantVal)));
650 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
651 return wrap(ConstantExpr::getNUWNeg(
652 unwrap<Constant>(ConstantVal)));
656 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
657 return wrap(ConstantExpr::getFNeg(
658 unwrap<Constant>(ConstantVal)));
661 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
662 return wrap(ConstantExpr::getNot(
663 unwrap<Constant>(ConstantVal)));
666 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
667 return wrap(ConstantExpr::getAdd(
668 unwrap<Constant>(LHSConstant),
669 unwrap<Constant>(RHSConstant)));
672 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
673 LLVMValueRef RHSConstant) {
674 return wrap(ConstantExpr::getNSWAdd(
675 unwrap<Constant>(LHSConstant),
676 unwrap<Constant>(RHSConstant)));
679 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
680 LLVMValueRef RHSConstant) {
681 return wrap(ConstantExpr::getNUWAdd(
682 unwrap<Constant>(LHSConstant),
683 unwrap<Constant>(RHSConstant)));
686 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
687 return wrap(ConstantExpr::getFAdd(
688 unwrap<Constant>(LHSConstant),
689 unwrap<Constant>(RHSConstant)));
692 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
693 return wrap(ConstantExpr::getSub(
694 unwrap<Constant>(LHSConstant),
695 unwrap<Constant>(RHSConstant)));
698 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
699 LLVMValueRef RHSConstant) {
700 return wrap(ConstantExpr::getNSWSub(
701 unwrap<Constant>(LHSConstant),
702 unwrap<Constant>(RHSConstant)));
705 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
706 LLVMValueRef RHSConstant) {
707 return wrap(ConstantExpr::getNUWSub(
708 unwrap<Constant>(LHSConstant),
709 unwrap<Constant>(RHSConstant)));
712 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
713 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
714 unwrap<Constant>(RHSConstant)));
717 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
718 return wrap(ConstantExpr::getMul(
719 unwrap<Constant>(LHSConstant),
720 unwrap<Constant>(RHSConstant)));
723 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
724 LLVMValueRef RHSConstant) {
725 return wrap(ConstantExpr::getNSWMul(
726 unwrap<Constant>(LHSConstant),
727 unwrap<Constant>(RHSConstant)));
730 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
731 LLVMValueRef RHSConstant) {
732 return wrap(ConstantExpr::getNUWMul(
733 unwrap<Constant>(LHSConstant),
734 unwrap<Constant>(RHSConstant)));
737 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
738 return wrap(ConstantExpr::getFMul(
739 unwrap<Constant>(LHSConstant),
740 unwrap<Constant>(RHSConstant)));
743 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
744 return wrap(ConstantExpr::getUDiv(
745 unwrap<Constant>(LHSConstant),
746 unwrap<Constant>(RHSConstant)));
749 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
750 return wrap(ConstantExpr::getSDiv(
751 unwrap<Constant>(LHSConstant),
752 unwrap<Constant>(RHSConstant)));
755 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
756 LLVMValueRef RHSConstant) {
757 return wrap(ConstantExpr::getExactSDiv(
758 unwrap<Constant>(LHSConstant),
759 unwrap<Constant>(RHSConstant)));
762 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
763 return wrap(ConstantExpr::getFDiv(
764 unwrap<Constant>(LHSConstant),
765 unwrap<Constant>(RHSConstant)));
768 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
769 return wrap(ConstantExpr::getURem(
770 unwrap<Constant>(LHSConstant),
771 unwrap<Constant>(RHSConstant)));
774 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
775 return wrap(ConstantExpr::getSRem(
776 unwrap<Constant>(LHSConstant),
777 unwrap<Constant>(RHSConstant)));
780 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
781 return wrap(ConstantExpr::getFRem(
782 unwrap<Constant>(LHSConstant),
783 unwrap<Constant>(RHSConstant)));
786 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
787 return wrap(ConstantExpr::getAnd(
788 unwrap<Constant>(LHSConstant),
789 unwrap<Constant>(RHSConstant)));
792 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
793 return wrap(ConstantExpr::getOr(
794 unwrap<Constant>(LHSConstant),
795 unwrap<Constant>(RHSConstant)));
798 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
799 return wrap(ConstantExpr::getXor(
800 unwrap<Constant>(LHSConstant),
801 unwrap<Constant>(RHSConstant)));
804 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
805 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
806 return wrap(ConstantExpr::getICmp(Predicate,
807 unwrap<Constant>(LHSConstant),
808 unwrap<Constant>(RHSConstant)));
811 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
812 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
813 return wrap(ConstantExpr::getFCmp(Predicate,
814 unwrap<Constant>(LHSConstant),
815 unwrap<Constant>(RHSConstant)));
818 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
819 return wrap(ConstantExpr::getShl(
820 unwrap<Constant>(LHSConstant),
821 unwrap<Constant>(RHSConstant)));
824 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
825 return wrap(ConstantExpr::getLShr(
826 unwrap<Constant>(LHSConstant),
827 unwrap<Constant>(RHSConstant)));
830 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
831 return wrap(ConstantExpr::getAShr(
832 unwrap<Constant>(LHSConstant),
833 unwrap<Constant>(RHSConstant)));
836 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
837 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
838 return wrap(ConstantExpr::getGetElementPtr(
839 unwrap<Constant>(ConstantVal),
840 unwrap<Constant>(ConstantIndices,
845 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
846 LLVMValueRef *ConstantIndices,
847 unsigned NumIndices) {
848 Constant* Val = unwrap<Constant>(ConstantVal);
849 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
850 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
853 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
854 return wrap(ConstantExpr::getTrunc(
855 unwrap<Constant>(ConstantVal),
859 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
860 return wrap(ConstantExpr::getSExt(
861 unwrap<Constant>(ConstantVal),
865 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
866 return wrap(ConstantExpr::getZExt(
867 unwrap<Constant>(ConstantVal),
871 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
872 return wrap(ConstantExpr::getFPTrunc(
873 unwrap<Constant>(ConstantVal),
877 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
878 return wrap(ConstantExpr::getFPExtend(
879 unwrap<Constant>(ConstantVal),
883 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
884 return wrap(ConstantExpr::getUIToFP(
885 unwrap<Constant>(ConstantVal),
889 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
890 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
894 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
895 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
899 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
900 return wrap(ConstantExpr::getFPToSI(
901 unwrap<Constant>(ConstantVal),
905 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
906 return wrap(ConstantExpr::getPtrToInt(
907 unwrap<Constant>(ConstantVal),
911 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
912 return wrap(ConstantExpr::getIntToPtr(
913 unwrap<Constant>(ConstantVal),
917 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
918 return wrap(ConstantExpr::getBitCast(
919 unwrap<Constant>(ConstantVal),
923 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
924 LLVMTypeRef ToType) {
925 return wrap(ConstantExpr::getZExtOrBitCast(
926 unwrap<Constant>(ConstantVal),
930 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
931 LLVMTypeRef ToType) {
932 return wrap(ConstantExpr::getSExtOrBitCast(
933 unwrap<Constant>(ConstantVal),
937 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
938 LLVMTypeRef ToType) {
939 return wrap(ConstantExpr::getTruncOrBitCast(
940 unwrap<Constant>(ConstantVal),
944 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
945 LLVMTypeRef ToType) {
946 return wrap(ConstantExpr::getPointerCast(
947 unwrap<Constant>(ConstantVal),
951 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
953 return wrap(ConstantExpr::getIntegerCast(
954 unwrap<Constant>(ConstantVal),
959 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
960 return wrap(ConstantExpr::getFPCast(
961 unwrap<Constant>(ConstantVal),
965 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
966 LLVMValueRef ConstantIfTrue,
967 LLVMValueRef ConstantIfFalse) {
968 return wrap(ConstantExpr::getSelect(
969 unwrap<Constant>(ConstantCondition),
970 unwrap<Constant>(ConstantIfTrue),
971 unwrap<Constant>(ConstantIfFalse)));
974 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
975 LLVMValueRef IndexConstant) {
976 return wrap(ConstantExpr::getExtractElement(
977 unwrap<Constant>(VectorConstant),
978 unwrap<Constant>(IndexConstant)));
981 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
982 LLVMValueRef ElementValueConstant,
983 LLVMValueRef IndexConstant) {
984 return wrap(ConstantExpr::getInsertElement(
985 unwrap<Constant>(VectorConstant),
986 unwrap<Constant>(ElementValueConstant),
987 unwrap<Constant>(IndexConstant)));
990 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
991 LLVMValueRef VectorBConstant,
992 LLVMValueRef MaskConstant) {
993 return wrap(ConstantExpr::getShuffleVector(
994 unwrap<Constant>(VectorAConstant),
995 unwrap<Constant>(VectorBConstant),
996 unwrap<Constant>(MaskConstant)));
999 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1001 return wrap(ConstantExpr::getExtractValue(
1002 unwrap<Constant>(AggConstant),
1006 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1007 LLVMValueRef ElementValueConstant,
1008 unsigned *IdxList, unsigned NumIdx) {
1009 return wrap(ConstantExpr::getInsertValue(
1010 unwrap<Constant>(AggConstant),
1011 unwrap<Constant>(ElementValueConstant),
1015 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1016 const char *Constraints,
1017 LLVMBool HasSideEffects,
1018 LLVMBool IsAlignStack) {
1019 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1020 Constraints, HasSideEffects, IsAlignStack));
1023 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1024 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1027 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1029 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1030 return wrap(unwrap<GlobalValue>(Global)->getParent());
1033 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1034 return unwrap<GlobalValue>(Global)->isDeclaration();
1037 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1038 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1040 assert(false && "Unhandled Linkage Type.");
1041 case GlobalValue::ExternalLinkage:
1042 return LLVMExternalLinkage;
1043 case GlobalValue::AvailableExternallyLinkage:
1044 return LLVMAvailableExternallyLinkage;
1045 case GlobalValue::LinkOnceAnyLinkage:
1046 return LLVMLinkOnceAnyLinkage;
1047 case GlobalValue::LinkOnceODRLinkage:
1048 return LLVMLinkOnceODRLinkage;
1049 case GlobalValue::WeakAnyLinkage:
1050 return LLVMWeakAnyLinkage;
1051 case GlobalValue::WeakODRLinkage:
1052 return LLVMWeakODRLinkage;
1053 case GlobalValue::AppendingLinkage:
1054 return LLVMAppendingLinkage;
1055 case GlobalValue::InternalLinkage:
1056 return LLVMInternalLinkage;
1057 case GlobalValue::PrivateLinkage:
1058 return LLVMPrivateLinkage;
1059 case GlobalValue::LinkerPrivateLinkage:
1060 return LLVMLinkerPrivateLinkage;
1061 case GlobalValue::DLLImportLinkage:
1062 return LLVMDLLImportLinkage;
1063 case GlobalValue::DLLExportLinkage:
1064 return LLVMDLLExportLinkage;
1065 case GlobalValue::ExternalWeakLinkage:
1066 return LLVMExternalWeakLinkage;
1067 case GlobalValue::CommonLinkage:
1068 return LLVMCommonLinkage;
1071 // Should never get here.
1072 return static_cast<LLVMLinkage>(0);
1075 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1076 GlobalValue *GV = unwrap<GlobalValue>(Global);
1080 assert(false && "Unhandled Linkage Type.");
1081 case LLVMExternalLinkage:
1082 GV->setLinkage(GlobalValue::ExternalLinkage);
1084 case LLVMAvailableExternallyLinkage:
1085 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1087 case LLVMLinkOnceAnyLinkage:
1088 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1090 case LLVMLinkOnceODRLinkage:
1091 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1093 case LLVMWeakAnyLinkage:
1094 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1096 case LLVMWeakODRLinkage:
1097 GV->setLinkage(GlobalValue::WeakODRLinkage);
1099 case LLVMAppendingLinkage:
1100 GV->setLinkage(GlobalValue::AppendingLinkage);
1102 case LLVMInternalLinkage:
1103 GV->setLinkage(GlobalValue::InternalLinkage);
1105 case LLVMPrivateLinkage:
1106 GV->setLinkage(GlobalValue::PrivateLinkage);
1108 case LLVMLinkerPrivateLinkage:
1109 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1111 case LLVMDLLImportLinkage:
1112 GV->setLinkage(GlobalValue::DLLImportLinkage);
1114 case LLVMDLLExportLinkage:
1115 GV->setLinkage(GlobalValue::DLLExportLinkage);
1117 case LLVMExternalWeakLinkage:
1118 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1120 case LLVMGhostLinkage:
1122 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1124 case LLVMCommonLinkage:
1125 GV->setLinkage(GlobalValue::CommonLinkage);
1130 const char *LLVMGetSection(LLVMValueRef Global) {
1131 return unwrap<GlobalValue>(Global)->getSection().c_str();
1134 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1135 unwrap<GlobalValue>(Global)->setSection(Section);
1138 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1139 return static_cast<LLVMVisibility>(
1140 unwrap<GlobalValue>(Global)->getVisibility());
1143 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1144 unwrap<GlobalValue>(Global)
1145 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1148 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1149 return unwrap<GlobalValue>(Global)->getAlignment();
1152 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1153 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1156 /*--.. Operations on global variables ......................................--*/
1158 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1159 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1160 GlobalValue::ExternalLinkage, 0, Name));
1163 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1165 unsigned AddressSpace) {
1166 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1167 GlobalValue::ExternalLinkage, 0, Name, 0,
1168 false, AddressSpace));
1171 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1172 return wrap(unwrap(M)->getNamedGlobal(Name));
1175 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1176 Module *Mod = unwrap(M);
1177 Module::global_iterator I = Mod->global_begin();
1178 if (I == Mod->global_end())
1183 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1184 Module *Mod = unwrap(M);
1185 Module::global_iterator I = Mod->global_end();
1186 if (I == Mod->global_begin())
1191 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1192 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1193 Module::global_iterator I = GV;
1194 if (++I == GV->getParent()->global_end())
1199 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1200 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1201 Module::global_iterator I = GV;
1202 if (I == GV->getParent()->global_begin())
1207 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1208 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1211 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1212 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1213 if ( !GV->hasInitializer() )
1215 return wrap(GV->getInitializer());
1218 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1219 unwrap<GlobalVariable>(GlobalVar)
1220 ->setInitializer(unwrap<Constant>(ConstantVal));
1223 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1224 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1227 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1228 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1231 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1232 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1235 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1236 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1239 /*--.. Operations on aliases ......................................--*/
1241 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1243 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1244 unwrap<Constant>(Aliasee), unwrap (M)));
1247 /*--.. Operations on functions .............................................--*/
1249 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1250 LLVMTypeRef FunctionTy) {
1251 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1252 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1255 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1256 return wrap(unwrap(M)->getFunction(Name));
1259 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1260 Module *Mod = unwrap(M);
1261 Module::iterator I = Mod->begin();
1262 if (I == Mod->end())
1267 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1268 Module *Mod = unwrap(M);
1269 Module::iterator I = Mod->end();
1270 if (I == Mod->begin())
1275 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1276 Function *Func = unwrap<Function>(Fn);
1277 Module::iterator I = Func;
1278 if (++I == Func->getParent()->end())
1283 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1284 Function *Func = unwrap<Function>(Fn);
1285 Module::iterator I = Func;
1286 if (I == Func->getParent()->begin())
1291 void LLVMDeleteFunction(LLVMValueRef Fn) {
1292 unwrap<Function>(Fn)->eraseFromParent();
1295 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1296 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1297 return F->getIntrinsicID();
1301 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1302 return unwrap<Function>(Fn)->getCallingConv();
1305 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1306 return unwrap<Function>(Fn)->setCallingConv(
1307 static_cast<CallingConv::ID>(CC));
1310 const char *LLVMGetGC(LLVMValueRef Fn) {
1311 Function *F = unwrap<Function>(Fn);
1312 return F->hasGC()? F->getGC() : 0;
1315 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1316 Function *F = unwrap<Function>(Fn);
1323 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1324 Function *Func = unwrap<Function>(Fn);
1325 const AttrListPtr PAL = Func->getAttributes();
1326 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1327 Func->setAttributes(PALnew);
1330 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1331 Function *Func = unwrap<Function>(Fn);
1332 const AttrListPtr PAL = Func->getAttributes();
1333 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1334 Func->setAttributes(PALnew);
1337 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1338 Function *Func = unwrap<Function>(Fn);
1339 const AttrListPtr PAL = Func->getAttributes();
1340 Attributes attr = PAL.getFnAttributes();
1341 return (LLVMAttribute)attr;
1344 /*--.. Operations on parameters ............................................--*/
1346 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1347 // This function is strictly redundant to
1348 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1349 return unwrap<Function>(FnRef)->arg_size();
1352 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1353 Function *Fn = unwrap<Function>(FnRef);
1354 for (Function::arg_iterator I = Fn->arg_begin(),
1355 E = Fn->arg_end(); I != E; I++)
1356 *ParamRefs++ = wrap(I);
1359 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1360 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1366 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1367 return wrap(unwrap<Argument>(V)->getParent());
1370 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1371 Function *Func = unwrap<Function>(Fn);
1372 Function::arg_iterator I = Func->arg_begin();
1373 if (I == Func->arg_end())
1378 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1379 Function *Func = unwrap<Function>(Fn);
1380 Function::arg_iterator I = Func->arg_end();
1381 if (I == Func->arg_begin())
1386 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1387 Argument *A = unwrap<Argument>(Arg);
1388 Function::arg_iterator I = A;
1389 if (++I == A->getParent()->arg_end())
1394 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1395 Argument *A = unwrap<Argument>(Arg);
1396 Function::arg_iterator I = A;
1397 if (I == A->getParent()->arg_begin())
1402 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1403 unwrap<Argument>(Arg)->addAttr(PA);
1406 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1407 unwrap<Argument>(Arg)->removeAttr(PA);
1410 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1411 Argument *A = unwrap<Argument>(Arg);
1412 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1414 return (LLVMAttribute)attr;
1418 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1419 unwrap<Argument>(Arg)->addAttr(
1420 Attribute::constructAlignmentFromInt(align));
1423 /*--.. Operations on basic blocks ..........................................--*/
1425 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1426 return wrap(static_cast<Value*>(unwrap(BB)));
1429 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1430 return isa<BasicBlock>(unwrap(Val));
1433 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1434 return wrap(unwrap<BasicBlock>(Val));
1437 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1438 return wrap(unwrap(BB)->getParent());
1441 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1442 return unwrap<Function>(FnRef)->size();
1445 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1446 Function *Fn = unwrap<Function>(FnRef);
1447 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1448 *BasicBlocksRefs++ = wrap(I);
1451 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1452 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1455 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1456 Function *Func = unwrap<Function>(Fn);
1457 Function::iterator I = Func->begin();
1458 if (I == Func->end())
1463 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1464 Function *Func = unwrap<Function>(Fn);
1465 Function::iterator I = Func->end();
1466 if (I == Func->begin())
1471 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1472 BasicBlock *Block = unwrap(BB);
1473 Function::iterator I = Block;
1474 if (++I == Block->getParent()->end())
1479 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1480 BasicBlock *Block = unwrap(BB);
1481 Function::iterator I = Block;
1482 if (I == Block->getParent()->begin())
1487 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1490 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1493 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1494 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1497 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1498 LLVMBasicBlockRef BBRef,
1500 BasicBlock *BB = unwrap(BBRef);
1501 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1504 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1506 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1509 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1510 unwrap(BBRef)->eraseFromParent();
1513 /*--.. Operations on instructions ..........................................--*/
1515 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1516 return wrap(unwrap<Instruction>(Inst)->getParent());
1519 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1520 BasicBlock *Block = unwrap(BB);
1521 BasicBlock::iterator I = Block->begin();
1522 if (I == Block->end())
1527 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1528 BasicBlock *Block = unwrap(BB);
1529 BasicBlock::iterator I = Block->end();
1530 if (I == Block->begin())
1535 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1536 Instruction *Instr = unwrap<Instruction>(Inst);
1537 BasicBlock::iterator I = Instr;
1538 if (++I == Instr->getParent()->end())
1543 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1544 Instruction *Instr = unwrap<Instruction>(Inst);
1545 BasicBlock::iterator I = Instr;
1546 if (I == Instr->getParent()->begin())
1551 /*--.. Call and invoke instructions ........................................--*/
1553 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1554 Value *V = unwrap(Instr);
1555 if (CallInst *CI = dyn_cast<CallInst>(V))
1556 return CI->getCallingConv();
1557 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1558 return II->getCallingConv();
1559 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1563 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1564 Value *V = unwrap(Instr);
1565 if (CallInst *CI = dyn_cast<CallInst>(V))
1566 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1567 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1568 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1569 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1572 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1574 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1576 Call.getAttributes().addAttr(index, PA));
1579 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1581 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1583 Call.getAttributes().removeAttr(index, PA));
1586 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1588 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1590 Call.getAttributes().addAttr(index,
1591 Attribute::constructAlignmentFromInt(align)));
1594 /*--.. Operations on call instructions (only) ..............................--*/
1596 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1597 return unwrap<CallInst>(Call)->isTailCall();
1600 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1601 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1604 /*--.. Operations on phi nodes .............................................--*/
1606 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1607 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1608 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1609 for (unsigned I = 0; I != Count; ++I)
1610 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1613 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1614 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1617 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1618 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1621 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1622 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1626 /*===-- Instruction builders ----------------------------------------------===*/
1628 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1629 return wrap(new IRBuilder<>(*unwrap(C)));
1632 LLVMBuilderRef LLVMCreateBuilder(void) {
1633 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1636 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1637 LLVMValueRef Instr) {
1638 BasicBlock *BB = unwrap(Block);
1639 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1640 unwrap(Builder)->SetInsertPoint(BB, I);
1643 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1644 Instruction *I = unwrap<Instruction>(Instr);
1645 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1648 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1649 BasicBlock *BB = unwrap(Block);
1650 unwrap(Builder)->SetInsertPoint(BB);
1653 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1654 return wrap(unwrap(Builder)->GetInsertBlock());
1657 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1658 unwrap(Builder)->ClearInsertionPoint();
1661 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1662 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1665 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1667 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1670 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1671 delete unwrap(Builder);
1674 /*--.. Metadata builders ...................................................--*/
1676 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1677 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1678 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1681 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1682 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1683 .getAsMDNode(unwrap(Builder)->getContext()));
1686 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1687 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1691 /*--.. Instruction builders ................................................--*/
1693 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1694 return wrap(unwrap(B)->CreateRetVoid());
1697 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1698 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1701 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1703 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1706 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1707 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1710 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1711 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1712 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1715 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1716 LLVMBasicBlockRef Else, unsigned NumCases) {
1717 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1720 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1721 unsigned NumDests) {
1722 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1725 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1726 LLVMValueRef *Args, unsigned NumArgs,
1727 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1729 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1730 unwrap(Args), unwrap(Args) + NumArgs,
1734 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1735 return wrap(unwrap(B)->CreateUnwind());
1738 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1739 return wrap(unwrap(B)->CreateUnreachable());
1742 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1743 LLVMBasicBlockRef Dest) {
1744 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1747 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1748 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1751 /*--.. Arithmetic ..........................................................--*/
1753 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1755 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1758 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1760 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1763 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1765 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1768 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1770 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1773 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1775 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1778 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1780 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1783 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1785 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1788 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1790 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1793 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1795 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1798 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1800 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1803 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1805 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1808 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1810 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1813 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1815 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1818 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1820 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1823 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1824 LLVMValueRef RHS, const char *Name) {
1825 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1828 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1830 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1833 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1835 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1838 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1840 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1843 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1845 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1848 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1850 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1853 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1855 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1858 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1860 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1863 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1865 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1868 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1870 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1873 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1875 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1878 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1879 LLVMValueRef LHS, LLVMValueRef RHS,
1881 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1882 unwrap(RHS), Name));
1885 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1886 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1889 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1891 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1894 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1896 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1899 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1900 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1903 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1904 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1907 /*--.. Memory ..............................................................--*/
1909 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1911 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1912 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1913 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1914 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1915 ITy, unwrap(Ty), AllocSize,
1917 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1920 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1921 LLVMValueRef Val, const char *Name) {
1922 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1923 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1924 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1925 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1926 ITy, unwrap(Ty), AllocSize,
1927 unwrap(Val), 0, "");
1928 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1931 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1933 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1936 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1937 LLVMValueRef Val, const char *Name) {
1938 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1941 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1942 return wrap(unwrap(B)->Insert(
1943 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1947 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1949 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1952 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1953 LLVMValueRef PointerVal) {
1954 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1957 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1958 LLVMValueRef *Indices, unsigned NumIndices,
1960 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1961 unwrap(Indices) + NumIndices, Name));
1964 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1965 LLVMValueRef *Indices, unsigned NumIndices,
1967 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1968 unwrap(Indices) + NumIndices, Name));
1971 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1972 unsigned Idx, const char *Name) {
1973 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1976 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1978 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1981 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1983 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1986 /*--.. Casts ...............................................................--*/
1988 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1989 LLVMTypeRef DestTy, const char *Name) {
1990 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1993 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1994 LLVMTypeRef DestTy, const char *Name) {
1995 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1998 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1999 LLVMTypeRef DestTy, const char *Name) {
2000 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2003 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2004 LLVMTypeRef DestTy, const char *Name) {
2005 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2008 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2009 LLVMTypeRef DestTy, const char *Name) {
2010 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2013 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2014 LLVMTypeRef DestTy, const char *Name) {
2015 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2018 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2019 LLVMTypeRef DestTy, const char *Name) {
2020 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2023 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2024 LLVMTypeRef DestTy, const char *Name) {
2025 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2028 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2029 LLVMTypeRef DestTy, const char *Name) {
2030 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2033 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2034 LLVMTypeRef DestTy, const char *Name) {
2035 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2038 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2039 LLVMTypeRef DestTy, const char *Name) {
2040 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2043 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2044 LLVMTypeRef DestTy, const char *Name) {
2045 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2048 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2049 LLVMTypeRef DestTy, const char *Name) {
2050 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2054 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2055 LLVMTypeRef DestTy, const char *Name) {
2056 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2060 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2061 LLVMTypeRef DestTy, const char *Name) {
2062 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2066 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2067 LLVMTypeRef DestTy, const char *Name) {
2068 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2069 unwrap(DestTy), Name));
2072 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2073 LLVMTypeRef DestTy, const char *Name) {
2074 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2077 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2078 LLVMTypeRef DestTy, const char *Name) {
2079 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2080 /*isSigned*/true, Name));
2083 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2084 LLVMTypeRef DestTy, const char *Name) {
2085 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2088 /*--.. Comparisons .........................................................--*/
2090 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2091 LLVMValueRef LHS, LLVMValueRef RHS,
2093 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2094 unwrap(LHS), unwrap(RHS), Name));
2097 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2098 LLVMValueRef LHS, LLVMValueRef RHS,
2100 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2101 unwrap(LHS), unwrap(RHS), Name));
2104 /*--.. Miscellaneous instructions ..........................................--*/
2106 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2107 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2110 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2111 LLVMValueRef *Args, unsigned NumArgs,
2113 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2114 unwrap(Args) + NumArgs, Name));
2117 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2118 LLVMValueRef Then, LLVMValueRef Else,
2120 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2124 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2125 LLVMTypeRef Ty, const char *Name) {
2126 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2129 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2130 LLVMValueRef Index, const char *Name) {
2131 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2135 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2136 LLVMValueRef EltVal, LLVMValueRef Index,
2138 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2139 unwrap(Index), Name));
2142 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2143 LLVMValueRef V2, LLVMValueRef Mask,
2145 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2146 unwrap(Mask), Name));
2149 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2150 unsigned Index, const char *Name) {
2151 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2154 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2155 LLVMValueRef EltVal, unsigned Index,
2157 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2161 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2163 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2166 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2168 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2171 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2172 LLVMValueRef RHS, const char *Name) {
2173 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2177 /*===-- Module providers --------------------------------------------------===*/
2179 LLVMModuleProviderRef
2180 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2181 return reinterpret_cast<LLVMModuleProviderRef>(M);
2184 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2189 /*===-- Memory buffers ----------------------------------------------------===*/
2191 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2193 LLVMMemoryBufferRef *OutMemBuf,
2194 char **OutMessage) {
2197 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2198 *OutMemBuf = wrap(MB);
2202 *OutMessage = strdup(Error.c_str());
2206 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2207 char **OutMessage) {
2209 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) {
2210 *OutMemBuf = wrap(MB);
2214 *OutMessage = strdup(Error.c_str());
2218 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2219 delete unwrap(MemBuf);