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::LinkerPrivateWeakLinkage:
1062 return LLVMLinkerPrivateWeakLinkage;
1063 case GlobalValue::DLLImportLinkage:
1064 return LLVMDLLImportLinkage;
1065 case GlobalValue::DLLExportLinkage:
1066 return LLVMDLLExportLinkage;
1067 case GlobalValue::ExternalWeakLinkage:
1068 return LLVMExternalWeakLinkage;
1069 case GlobalValue::CommonLinkage:
1070 return LLVMCommonLinkage;
1073 // Should never get here.
1074 return static_cast<LLVMLinkage>(0);
1077 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1078 GlobalValue *GV = unwrap<GlobalValue>(Global);
1082 assert(false && "Unhandled Linkage Type.");
1083 case LLVMExternalLinkage:
1084 GV->setLinkage(GlobalValue::ExternalLinkage);
1086 case LLVMAvailableExternallyLinkage:
1087 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1089 case LLVMLinkOnceAnyLinkage:
1090 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1092 case LLVMLinkOnceODRLinkage:
1093 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1095 case LLVMWeakAnyLinkage:
1096 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1098 case LLVMWeakODRLinkage:
1099 GV->setLinkage(GlobalValue::WeakODRLinkage);
1101 case LLVMAppendingLinkage:
1102 GV->setLinkage(GlobalValue::AppendingLinkage);
1104 case LLVMInternalLinkage:
1105 GV->setLinkage(GlobalValue::InternalLinkage);
1107 case LLVMPrivateLinkage:
1108 GV->setLinkage(GlobalValue::PrivateLinkage);
1110 case LLVMLinkerPrivateLinkage:
1111 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1113 case LLVMLinkerPrivateWeakLinkage:
1114 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1116 case LLVMDLLImportLinkage:
1117 GV->setLinkage(GlobalValue::DLLImportLinkage);
1119 case LLVMDLLExportLinkage:
1120 GV->setLinkage(GlobalValue::DLLExportLinkage);
1122 case LLVMExternalWeakLinkage:
1123 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1125 case LLVMGhostLinkage:
1127 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1129 case LLVMCommonLinkage:
1130 GV->setLinkage(GlobalValue::CommonLinkage);
1135 const char *LLVMGetSection(LLVMValueRef Global) {
1136 return unwrap<GlobalValue>(Global)->getSection().c_str();
1139 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1140 unwrap<GlobalValue>(Global)->setSection(Section);
1143 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1144 return static_cast<LLVMVisibility>(
1145 unwrap<GlobalValue>(Global)->getVisibility());
1148 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1149 unwrap<GlobalValue>(Global)
1150 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1153 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1154 return unwrap<GlobalValue>(Global)->getAlignment();
1157 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1158 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1161 /*--.. Operations on global variables ......................................--*/
1163 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1164 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1165 GlobalValue::ExternalLinkage, 0, Name));
1168 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1170 unsigned AddressSpace) {
1171 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1172 GlobalValue::ExternalLinkage, 0, Name, 0,
1173 false, AddressSpace));
1176 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1177 return wrap(unwrap(M)->getNamedGlobal(Name));
1180 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1181 Module *Mod = unwrap(M);
1182 Module::global_iterator I = Mod->global_begin();
1183 if (I == Mod->global_end())
1188 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1189 Module *Mod = unwrap(M);
1190 Module::global_iterator I = Mod->global_end();
1191 if (I == Mod->global_begin())
1196 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1197 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1198 Module::global_iterator I = GV;
1199 if (++I == GV->getParent()->global_end())
1204 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1205 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1206 Module::global_iterator I = GV;
1207 if (I == GV->getParent()->global_begin())
1212 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1213 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1216 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1217 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1218 if ( !GV->hasInitializer() )
1220 return wrap(GV->getInitializer());
1223 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1224 unwrap<GlobalVariable>(GlobalVar)
1225 ->setInitializer(unwrap<Constant>(ConstantVal));
1228 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1229 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1232 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1233 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1236 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1237 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1240 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1241 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1244 /*--.. Operations on aliases ......................................--*/
1246 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1248 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1249 unwrap<Constant>(Aliasee), unwrap (M)));
1252 /*--.. Operations on functions .............................................--*/
1254 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1255 LLVMTypeRef FunctionTy) {
1256 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1257 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1260 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1261 return wrap(unwrap(M)->getFunction(Name));
1264 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1265 Module *Mod = unwrap(M);
1266 Module::iterator I = Mod->begin();
1267 if (I == Mod->end())
1272 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1273 Module *Mod = unwrap(M);
1274 Module::iterator I = Mod->end();
1275 if (I == Mod->begin())
1280 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1281 Function *Func = unwrap<Function>(Fn);
1282 Module::iterator I = Func;
1283 if (++I == Func->getParent()->end())
1288 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1289 Function *Func = unwrap<Function>(Fn);
1290 Module::iterator I = Func;
1291 if (I == Func->getParent()->begin())
1296 void LLVMDeleteFunction(LLVMValueRef Fn) {
1297 unwrap<Function>(Fn)->eraseFromParent();
1300 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1301 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1302 return F->getIntrinsicID();
1306 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1307 return unwrap<Function>(Fn)->getCallingConv();
1310 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1311 return unwrap<Function>(Fn)->setCallingConv(
1312 static_cast<CallingConv::ID>(CC));
1315 const char *LLVMGetGC(LLVMValueRef Fn) {
1316 Function *F = unwrap<Function>(Fn);
1317 return F->hasGC()? F->getGC() : 0;
1320 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1321 Function *F = unwrap<Function>(Fn);
1328 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1329 Function *Func = unwrap<Function>(Fn);
1330 const AttrListPtr PAL = Func->getAttributes();
1331 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1332 Func->setAttributes(PALnew);
1335 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1336 Function *Func = unwrap<Function>(Fn);
1337 const AttrListPtr PAL = Func->getAttributes();
1338 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1339 Func->setAttributes(PALnew);
1342 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1343 Function *Func = unwrap<Function>(Fn);
1344 const AttrListPtr PAL = Func->getAttributes();
1345 Attributes attr = PAL.getFnAttributes();
1346 return (LLVMAttribute)attr;
1349 /*--.. Operations on parameters ............................................--*/
1351 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1352 // This function is strictly redundant to
1353 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1354 return unwrap<Function>(FnRef)->arg_size();
1357 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1358 Function *Fn = unwrap<Function>(FnRef);
1359 for (Function::arg_iterator I = Fn->arg_begin(),
1360 E = Fn->arg_end(); I != E; I++)
1361 *ParamRefs++ = wrap(I);
1364 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1365 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1371 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1372 return wrap(unwrap<Argument>(V)->getParent());
1375 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1376 Function *Func = unwrap<Function>(Fn);
1377 Function::arg_iterator I = Func->arg_begin();
1378 if (I == Func->arg_end())
1383 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1384 Function *Func = unwrap<Function>(Fn);
1385 Function::arg_iterator I = Func->arg_end();
1386 if (I == Func->arg_begin())
1391 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1392 Argument *A = unwrap<Argument>(Arg);
1393 Function::arg_iterator I = A;
1394 if (++I == A->getParent()->arg_end())
1399 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1400 Argument *A = unwrap<Argument>(Arg);
1401 Function::arg_iterator I = A;
1402 if (I == A->getParent()->arg_begin())
1407 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1408 unwrap<Argument>(Arg)->addAttr(PA);
1411 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1412 unwrap<Argument>(Arg)->removeAttr(PA);
1415 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1416 Argument *A = unwrap<Argument>(Arg);
1417 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1419 return (LLVMAttribute)attr;
1423 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1424 unwrap<Argument>(Arg)->addAttr(
1425 Attribute::constructAlignmentFromInt(align));
1428 /*--.. Operations on basic blocks ..........................................--*/
1430 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1431 return wrap(static_cast<Value*>(unwrap(BB)));
1434 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1435 return isa<BasicBlock>(unwrap(Val));
1438 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1439 return wrap(unwrap<BasicBlock>(Val));
1442 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1443 return wrap(unwrap(BB)->getParent());
1446 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1447 return unwrap<Function>(FnRef)->size();
1450 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1451 Function *Fn = unwrap<Function>(FnRef);
1452 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1453 *BasicBlocksRefs++ = wrap(I);
1456 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1457 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1460 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1461 Function *Func = unwrap<Function>(Fn);
1462 Function::iterator I = Func->begin();
1463 if (I == Func->end())
1468 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1469 Function *Func = unwrap<Function>(Fn);
1470 Function::iterator I = Func->end();
1471 if (I == Func->begin())
1476 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1477 BasicBlock *Block = unwrap(BB);
1478 Function::iterator I = Block;
1479 if (++I == Block->getParent()->end())
1484 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1485 BasicBlock *Block = unwrap(BB);
1486 Function::iterator I = Block;
1487 if (I == Block->getParent()->begin())
1492 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1495 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1498 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1499 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1502 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1503 LLVMBasicBlockRef BBRef,
1505 BasicBlock *BB = unwrap(BBRef);
1506 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1509 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1511 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1514 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1515 unwrap(BBRef)->eraseFromParent();
1518 /*--.. Operations on instructions ..........................................--*/
1520 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1521 return wrap(unwrap<Instruction>(Inst)->getParent());
1524 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1525 BasicBlock *Block = unwrap(BB);
1526 BasicBlock::iterator I = Block->begin();
1527 if (I == Block->end())
1532 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1533 BasicBlock *Block = unwrap(BB);
1534 BasicBlock::iterator I = Block->end();
1535 if (I == Block->begin())
1540 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1541 Instruction *Instr = unwrap<Instruction>(Inst);
1542 BasicBlock::iterator I = Instr;
1543 if (++I == Instr->getParent()->end())
1548 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1549 Instruction *Instr = unwrap<Instruction>(Inst);
1550 BasicBlock::iterator I = Instr;
1551 if (I == Instr->getParent()->begin())
1556 /*--.. Call and invoke instructions ........................................--*/
1558 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1559 Value *V = unwrap(Instr);
1560 if (CallInst *CI = dyn_cast<CallInst>(V))
1561 return CI->getCallingConv();
1562 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1563 return II->getCallingConv();
1564 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1568 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1569 Value *V = unwrap(Instr);
1570 if (CallInst *CI = dyn_cast<CallInst>(V))
1571 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1572 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1573 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1574 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1577 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1579 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1581 Call.getAttributes().addAttr(index, PA));
1584 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1586 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1588 Call.getAttributes().removeAttr(index, PA));
1591 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1593 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1595 Call.getAttributes().addAttr(index,
1596 Attribute::constructAlignmentFromInt(align)));
1599 /*--.. Operations on call instructions (only) ..............................--*/
1601 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1602 return unwrap<CallInst>(Call)->isTailCall();
1605 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1606 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1609 /*--.. Operations on phi nodes .............................................--*/
1611 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1612 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1613 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1614 for (unsigned I = 0; I != Count; ++I)
1615 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1618 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1619 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1622 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1623 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1626 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1627 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1631 /*===-- Instruction builders ----------------------------------------------===*/
1633 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1634 return wrap(new IRBuilder<>(*unwrap(C)));
1637 LLVMBuilderRef LLVMCreateBuilder(void) {
1638 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1641 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1642 LLVMValueRef Instr) {
1643 BasicBlock *BB = unwrap(Block);
1644 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1645 unwrap(Builder)->SetInsertPoint(BB, I);
1648 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1649 Instruction *I = unwrap<Instruction>(Instr);
1650 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1653 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1654 BasicBlock *BB = unwrap(Block);
1655 unwrap(Builder)->SetInsertPoint(BB);
1658 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1659 return wrap(unwrap(Builder)->GetInsertBlock());
1662 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1663 unwrap(Builder)->ClearInsertionPoint();
1666 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1667 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1670 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1672 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1675 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1676 delete unwrap(Builder);
1679 /*--.. Metadata builders ...................................................--*/
1681 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1682 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1683 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1686 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1687 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1688 .getAsMDNode(unwrap(Builder)->getContext()));
1691 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1692 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1696 /*--.. Instruction builders ................................................--*/
1698 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1699 return wrap(unwrap(B)->CreateRetVoid());
1702 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1703 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1706 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1708 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1711 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1712 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1715 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1716 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1717 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1720 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1721 LLVMBasicBlockRef Else, unsigned NumCases) {
1722 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1725 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1726 unsigned NumDests) {
1727 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1730 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1731 LLVMValueRef *Args, unsigned NumArgs,
1732 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1734 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1735 unwrap(Args), unwrap(Args) + NumArgs,
1739 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1740 return wrap(unwrap(B)->CreateUnwind());
1743 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1744 return wrap(unwrap(B)->CreateUnreachable());
1747 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1748 LLVMBasicBlockRef Dest) {
1749 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1752 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1753 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1756 /*--.. Arithmetic ..........................................................--*/
1758 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1760 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1763 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1765 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1768 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1770 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1773 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1775 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1778 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1780 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1783 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1785 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1788 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1790 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1793 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1795 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1798 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1800 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1803 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1805 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1808 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1810 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1813 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1815 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1818 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1820 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1823 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1825 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1828 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1829 LLVMValueRef RHS, const char *Name) {
1830 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1833 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1835 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1838 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1840 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1843 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1845 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1848 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1850 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1853 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1855 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1858 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1860 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1863 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1865 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1868 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1870 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1873 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1875 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1878 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1880 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1883 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1884 LLVMValueRef LHS, LLVMValueRef RHS,
1886 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1887 unwrap(RHS), Name));
1890 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1891 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1894 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1896 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1899 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1901 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1904 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1905 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1908 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1909 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1912 /*--.. Memory ..............................................................--*/
1914 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1916 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1917 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1918 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1919 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1920 ITy, unwrap(Ty), AllocSize,
1922 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1925 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1926 LLVMValueRef Val, const char *Name) {
1927 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1928 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1929 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1930 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1931 ITy, unwrap(Ty), AllocSize,
1932 unwrap(Val), 0, "");
1933 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1936 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1938 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1941 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1942 LLVMValueRef Val, const char *Name) {
1943 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1946 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1947 return wrap(unwrap(B)->Insert(
1948 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1952 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1954 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1957 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1958 LLVMValueRef PointerVal) {
1959 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1962 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1963 LLVMValueRef *Indices, unsigned NumIndices,
1965 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1966 unwrap(Indices) + NumIndices, Name));
1969 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1970 LLVMValueRef *Indices, unsigned NumIndices,
1972 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1973 unwrap(Indices) + NumIndices, Name));
1976 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1977 unsigned Idx, const char *Name) {
1978 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1981 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1983 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1986 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1988 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1991 /*--.. Casts ...............................................................--*/
1993 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1994 LLVMTypeRef DestTy, const char *Name) {
1995 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1998 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1999 LLVMTypeRef DestTy, const char *Name) {
2000 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2003 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2004 LLVMTypeRef DestTy, const char *Name) {
2005 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2008 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2009 LLVMTypeRef DestTy, const char *Name) {
2010 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2013 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2014 LLVMTypeRef DestTy, const char *Name) {
2015 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2018 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2019 LLVMTypeRef DestTy, const char *Name) {
2020 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2023 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2024 LLVMTypeRef DestTy, const char *Name) {
2025 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2028 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2029 LLVMTypeRef DestTy, const char *Name) {
2030 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2033 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2034 LLVMTypeRef DestTy, const char *Name) {
2035 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2038 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2039 LLVMTypeRef DestTy, const char *Name) {
2040 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2043 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2044 LLVMTypeRef DestTy, const char *Name) {
2045 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2048 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2049 LLVMTypeRef DestTy, const char *Name) {
2050 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2053 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2054 LLVMTypeRef DestTy, const char *Name) {
2055 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2059 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2060 LLVMTypeRef DestTy, const char *Name) {
2061 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2065 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2066 LLVMTypeRef DestTy, const char *Name) {
2067 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2071 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2072 LLVMTypeRef DestTy, const char *Name) {
2073 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2074 unwrap(DestTy), Name));
2077 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2078 LLVMTypeRef DestTy, const char *Name) {
2079 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2082 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2083 LLVMTypeRef DestTy, const char *Name) {
2084 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2085 /*isSigned*/true, Name));
2088 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2089 LLVMTypeRef DestTy, const char *Name) {
2090 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2093 /*--.. Comparisons .........................................................--*/
2095 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2096 LLVMValueRef LHS, LLVMValueRef RHS,
2098 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2099 unwrap(LHS), unwrap(RHS), Name));
2102 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2103 LLVMValueRef LHS, LLVMValueRef RHS,
2105 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2106 unwrap(LHS), unwrap(RHS), Name));
2109 /*--.. Miscellaneous instructions ..........................................--*/
2111 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2112 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2115 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2116 LLVMValueRef *Args, unsigned NumArgs,
2118 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2119 unwrap(Args) + NumArgs, Name));
2122 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2123 LLVMValueRef Then, LLVMValueRef Else,
2125 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2129 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2130 LLVMTypeRef Ty, const char *Name) {
2131 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2134 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2135 LLVMValueRef Index, const char *Name) {
2136 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2140 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2141 LLVMValueRef EltVal, LLVMValueRef Index,
2143 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2144 unwrap(Index), Name));
2147 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2148 LLVMValueRef V2, LLVMValueRef Mask,
2150 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2151 unwrap(Mask), Name));
2154 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2155 unsigned Index, const char *Name) {
2156 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2159 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2160 LLVMValueRef EltVal, unsigned Index,
2162 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2166 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2168 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2171 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2173 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2176 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2177 LLVMValueRef RHS, const char *Name) {
2178 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2182 /*===-- Module providers --------------------------------------------------===*/
2184 LLVMModuleProviderRef
2185 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2186 return reinterpret_cast<LLVMModuleProviderRef>(M);
2189 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2194 /*===-- Memory buffers ----------------------------------------------------===*/
2196 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2198 LLVMMemoryBufferRef *OutMemBuf,
2199 char **OutMessage) {
2202 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2203 *OutMemBuf = wrap(MB);
2207 *OutMessage = strdup(Error.c_str());
2211 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2212 char **OutMessage) {
2214 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) {
2215 *OutMemBuf = wrap(MB);
2219 *OutMessage = strdup(Error.c_str());
2223 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2224 delete unwrap(MemBuf);