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) {
123 /*===-- Operations on types -----------------------------------------------===*/
125 /*--.. Operations on all types (mostly) ....................................--*/
127 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
128 switch (unwrap(Ty)->getTypeID()) {
130 assert(false && "Unhandled TypeID.");
132 return LLVMVoidTypeKind;
133 case Type::FloatTyID:
134 return LLVMFloatTypeKind;
135 case Type::DoubleTyID:
136 return LLVMDoubleTypeKind;
137 case Type::X86_FP80TyID:
138 return LLVMX86_FP80TypeKind;
139 case Type::FP128TyID:
140 return LLVMFP128TypeKind;
141 case Type::PPC_FP128TyID:
142 return LLVMPPC_FP128TypeKind;
143 case Type::LabelTyID:
144 return LLVMLabelTypeKind;
145 case Type::MetadataTyID:
146 return LLVMMetadataTypeKind;
147 case Type::IntegerTyID:
148 return LLVMIntegerTypeKind;
149 case Type::FunctionTyID:
150 return LLVMFunctionTypeKind;
151 case Type::StructTyID:
152 return LLVMStructTypeKind;
153 case Type::UnionTyID:
154 return LLVMUnionTypeKind;
155 case Type::ArrayTyID:
156 return LLVMArrayTypeKind;
157 case Type::PointerTyID:
158 return LLVMPointerTypeKind;
159 case Type::OpaqueTyID:
160 return LLVMOpaqueTypeKind;
161 case Type::VectorTyID:
162 return LLVMVectorTypeKind;
166 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
167 return wrap(&unwrap(Ty)->getContext());
170 /*--.. Operations on integer types .........................................--*/
172 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
173 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
175 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
176 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
178 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
179 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
181 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
182 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
184 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
185 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
187 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
188 return wrap(IntegerType::get(*unwrap(C), NumBits));
191 LLVMTypeRef LLVMInt1Type(void) {
192 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
194 LLVMTypeRef LLVMInt8Type(void) {
195 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
197 LLVMTypeRef LLVMInt16Type(void) {
198 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
200 LLVMTypeRef LLVMInt32Type(void) {
201 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
203 LLVMTypeRef LLVMInt64Type(void) {
204 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
206 LLVMTypeRef LLVMIntType(unsigned NumBits) {
207 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
210 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
211 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
214 /*--.. Operations on real types ............................................--*/
216 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
217 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
219 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
220 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
222 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
223 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
225 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
226 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
228 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
229 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
232 LLVMTypeRef LLVMFloatType(void) {
233 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
235 LLVMTypeRef LLVMDoubleType(void) {
236 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
238 LLVMTypeRef LLVMX86FP80Type(void) {
239 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
241 LLVMTypeRef LLVMFP128Type(void) {
242 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
244 LLVMTypeRef LLVMPPCFP128Type(void) {
245 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
248 /*--.. Operations on function types ........................................--*/
250 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
251 LLVMTypeRef *ParamTypes, unsigned ParamCount,
253 std::vector<const Type*> Tys;
254 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
255 Tys.push_back(unwrap(*I));
257 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
260 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
261 return unwrap<FunctionType>(FunctionTy)->isVarArg();
264 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
265 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
268 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
269 return unwrap<FunctionType>(FunctionTy)->getNumParams();
272 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
273 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
274 for (FunctionType::param_iterator I = Ty->param_begin(),
275 E = Ty->param_end(); I != E; ++I)
279 /*--.. Operations on struct types ..........................................--*/
281 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
282 unsigned ElementCount, LLVMBool Packed) {
283 std::vector<const Type*> Tys;
284 for (LLVMTypeRef *I = ElementTypes,
285 *E = ElementTypes + ElementCount; I != E; ++I)
286 Tys.push_back(unwrap(*I));
288 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
291 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
292 unsigned ElementCount, LLVMBool Packed) {
293 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
294 ElementCount, Packed);
298 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
299 return unwrap<StructType>(StructTy)->getNumElements();
302 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
303 StructType *Ty = unwrap<StructType>(StructTy);
304 for (FunctionType::param_iterator I = Ty->element_begin(),
305 E = Ty->element_end(); I != E; ++I)
309 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
310 return unwrap<StructType>(StructTy)->isPacked();
313 /*--.. Operations on union types ..........................................--*/
315 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
316 unsigned ElementCount) {
317 SmallVector<const Type*, 8> Tys;
318 for (LLVMTypeRef *I = ElementTypes,
319 *E = ElementTypes + ElementCount; I != E; ++I)
320 Tys.push_back(unwrap(*I));
322 return wrap(UnionType::get(&Tys[0], Tys.size()));
325 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes,
326 unsigned ElementCount, int Packed) {
327 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
331 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
332 return unwrap<UnionType>(UnionTy)->getNumElements();
335 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
336 UnionType *Ty = unwrap<UnionType>(UnionTy);
337 for (FunctionType::param_iterator I = Ty->element_begin(),
338 E = Ty->element_end(); I != E; ++I)
342 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
344 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
345 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
348 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
349 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
352 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
353 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
356 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
357 return wrap(unwrap<SequentialType>(Ty)->getElementType());
360 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
361 return unwrap<ArrayType>(ArrayTy)->getNumElements();
364 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
365 return unwrap<PointerType>(PointerTy)->getAddressSpace();
368 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
369 return unwrap<VectorType>(VectorTy)->getNumElements();
372 /*--.. Operations on other types ...........................................--*/
374 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
375 return wrap(Type::getVoidTy(*unwrap(C)));
377 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
378 return wrap(Type::getLabelTy(*unwrap(C)));
380 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
381 return wrap(OpaqueType::get(*unwrap(C)));
384 LLVMTypeRef LLVMVoidType(void) {
385 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
387 LLVMTypeRef LLVMLabelType(void) {
388 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
390 LLVMTypeRef LLVMOpaqueType(void) {
391 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
394 /*--.. Operations on type handles ..........................................--*/
396 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
397 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
400 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
401 delete unwrap(TypeHandle);
404 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
405 return wrap(unwrap(TypeHandle)->get());
408 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
409 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
413 /*===-- Operations on values ----------------------------------------------===*/
415 /*--.. Operations on all values ............................................--*/
417 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
418 return wrap(unwrap(Val)->getType());
421 const char *LLVMGetValueName(LLVMValueRef Val) {
422 return unwrap(Val)->getName().data();
425 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
426 unwrap(Val)->setName(Name);
429 void LLVMDumpValue(LLVMValueRef Val) {
433 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
434 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
437 int LLVMHasMetadata(LLVMValueRef Inst) {
438 return unwrap<Instruction>(Inst)->hasMetadata();
441 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
442 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
445 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
446 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
449 /*--.. Conversion functions ................................................--*/
451 #define LLVM_DEFINE_VALUE_CAST(name) \
452 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
453 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
456 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
458 /*--.. Operations on Uses ..................................................--*/
459 LLVMUseIteratorRef LLVMGetFirstUse(LLVMValueRef Val) {
460 Value *V = unwrap(Val);
461 Value::use_iterator I = V->use_begin();
462 if (I == V->use_end())
464 return wrap(&(I.getUse()));
467 LLVMUseIteratorRef LLVMGetNextUse(LLVMUseIteratorRef UR) {
468 return wrap(unwrap(UR)->getNext());
471 LLVMValueRef LLVMGetUser(LLVMUseIteratorRef UR) {
472 return wrap(unwrap(UR)->getUser());
475 LLVMValueRef LLVMGetUsedValue(LLVMUseIteratorRef UR) {
476 return wrap(unwrap(UR)->get());
479 /*--.. Operations on Users .................................................--*/
480 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
481 return wrap(unwrap<User>(Val)->getOperand(Index));
484 /*--.. Operations on constants of any type .................................--*/
486 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
487 return wrap(Constant::getNullValue(unwrap(Ty)));
490 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
491 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
494 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
495 return wrap(UndefValue::get(unwrap(Ty)));
498 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
499 return isa<Constant>(unwrap(Ty));
502 LLVMBool LLVMIsNull(LLVMValueRef Val) {
503 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
504 return C->isNullValue();
508 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
509 return isa<UndefValue>(unwrap(Val));
512 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
514 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
517 /*--.. Operations on metadata nodes ........................................--*/
519 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
521 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
524 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
525 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
528 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
530 return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
533 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
534 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
537 /*--.. Operations on scalar constants ......................................--*/
539 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
540 LLVMBool SignExtend) {
541 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
544 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
546 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
550 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
551 unsigned SLen, uint8_t Radix) {
552 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
556 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
557 return wrap(ConstantFP::get(unwrap(RealTy), N));
560 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
561 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
564 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
566 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
569 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
570 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
573 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
574 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
577 /*--.. Operations on composite constants ...................................--*/
579 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
581 LLVMBool DontNullTerminate) {
582 /* Inverted the sense of AddNull because ', 0)' is a
583 better mnemonic for null termination than ', 1)'. */
584 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
585 DontNullTerminate == 0));
587 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
588 LLVMValueRef *ConstantVals,
589 unsigned Count, LLVMBool Packed) {
590 return wrap(ConstantStruct::get(*unwrap(C),
591 unwrap<Constant>(ConstantVals, Count),
592 Count, Packed != 0));
595 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
596 LLVMBool DontNullTerminate) {
597 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
600 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
601 LLVMValueRef *ConstantVals, unsigned Length) {
602 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
603 unwrap<Constant>(ConstantVals, Length),
606 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
608 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
611 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
612 return wrap(ConstantVector::get(
613 unwrap<Constant>(ScalarConstantVals, Size), Size));
615 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
616 return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
619 /*--.. Constant expressions ................................................--*/
621 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
622 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
625 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
626 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
629 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
630 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
633 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
634 return wrap(ConstantExpr::getNeg(
635 unwrap<Constant>(ConstantVal)));
638 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
639 return wrap(ConstantExpr::getNSWNeg(
640 unwrap<Constant>(ConstantVal)));
643 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
644 return wrap(ConstantExpr::getNUWNeg(
645 unwrap<Constant>(ConstantVal)));
649 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
650 return wrap(ConstantExpr::getFNeg(
651 unwrap<Constant>(ConstantVal)));
654 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
655 return wrap(ConstantExpr::getNot(
656 unwrap<Constant>(ConstantVal)));
659 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
660 return wrap(ConstantExpr::getAdd(
661 unwrap<Constant>(LHSConstant),
662 unwrap<Constant>(RHSConstant)));
665 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
666 LLVMValueRef RHSConstant) {
667 return wrap(ConstantExpr::getNSWAdd(
668 unwrap<Constant>(LHSConstant),
669 unwrap<Constant>(RHSConstant)));
672 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
673 LLVMValueRef RHSConstant) {
674 return wrap(ConstantExpr::getNUWAdd(
675 unwrap<Constant>(LHSConstant),
676 unwrap<Constant>(RHSConstant)));
679 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
680 return wrap(ConstantExpr::getFAdd(
681 unwrap<Constant>(LHSConstant),
682 unwrap<Constant>(RHSConstant)));
685 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
686 return wrap(ConstantExpr::getSub(
687 unwrap<Constant>(LHSConstant),
688 unwrap<Constant>(RHSConstant)));
691 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
692 LLVMValueRef RHSConstant) {
693 return wrap(ConstantExpr::getNSWSub(
694 unwrap<Constant>(LHSConstant),
695 unwrap<Constant>(RHSConstant)));
698 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
699 LLVMValueRef RHSConstant) {
700 return wrap(ConstantExpr::getNUWSub(
701 unwrap<Constant>(LHSConstant),
702 unwrap<Constant>(RHSConstant)));
705 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
706 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
707 unwrap<Constant>(RHSConstant)));
710 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
711 return wrap(ConstantExpr::getMul(
712 unwrap<Constant>(LHSConstant),
713 unwrap<Constant>(RHSConstant)));
716 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
717 LLVMValueRef RHSConstant) {
718 return wrap(ConstantExpr::getNSWMul(
719 unwrap<Constant>(LHSConstant),
720 unwrap<Constant>(RHSConstant)));
723 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
724 LLVMValueRef RHSConstant) {
725 return wrap(ConstantExpr::getNUWMul(
726 unwrap<Constant>(LHSConstant),
727 unwrap<Constant>(RHSConstant)));
730 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
731 return wrap(ConstantExpr::getFMul(
732 unwrap<Constant>(LHSConstant),
733 unwrap<Constant>(RHSConstant)));
736 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
737 return wrap(ConstantExpr::getUDiv(
738 unwrap<Constant>(LHSConstant),
739 unwrap<Constant>(RHSConstant)));
742 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
743 return wrap(ConstantExpr::getSDiv(
744 unwrap<Constant>(LHSConstant),
745 unwrap<Constant>(RHSConstant)));
748 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
749 LLVMValueRef RHSConstant) {
750 return wrap(ConstantExpr::getExactSDiv(
751 unwrap<Constant>(LHSConstant),
752 unwrap<Constant>(RHSConstant)));
755 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
756 return wrap(ConstantExpr::getFDiv(
757 unwrap<Constant>(LHSConstant),
758 unwrap<Constant>(RHSConstant)));
761 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
762 return wrap(ConstantExpr::getURem(
763 unwrap<Constant>(LHSConstant),
764 unwrap<Constant>(RHSConstant)));
767 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
768 return wrap(ConstantExpr::getSRem(
769 unwrap<Constant>(LHSConstant),
770 unwrap<Constant>(RHSConstant)));
773 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
774 return wrap(ConstantExpr::getFRem(
775 unwrap<Constant>(LHSConstant),
776 unwrap<Constant>(RHSConstant)));
779 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
780 return wrap(ConstantExpr::getAnd(
781 unwrap<Constant>(LHSConstant),
782 unwrap<Constant>(RHSConstant)));
785 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
786 return wrap(ConstantExpr::getOr(
787 unwrap<Constant>(LHSConstant),
788 unwrap<Constant>(RHSConstant)));
791 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
792 return wrap(ConstantExpr::getXor(
793 unwrap<Constant>(LHSConstant),
794 unwrap<Constant>(RHSConstant)));
797 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
798 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
799 return wrap(ConstantExpr::getICmp(Predicate,
800 unwrap<Constant>(LHSConstant),
801 unwrap<Constant>(RHSConstant)));
804 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
805 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
806 return wrap(ConstantExpr::getFCmp(Predicate,
807 unwrap<Constant>(LHSConstant),
808 unwrap<Constant>(RHSConstant)));
811 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
812 return wrap(ConstantExpr::getShl(
813 unwrap<Constant>(LHSConstant),
814 unwrap<Constant>(RHSConstant)));
817 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
818 return wrap(ConstantExpr::getLShr(
819 unwrap<Constant>(LHSConstant),
820 unwrap<Constant>(RHSConstant)));
823 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
824 return wrap(ConstantExpr::getAShr(
825 unwrap<Constant>(LHSConstant),
826 unwrap<Constant>(RHSConstant)));
829 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
830 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
831 return wrap(ConstantExpr::getGetElementPtr(
832 unwrap<Constant>(ConstantVal),
833 unwrap<Constant>(ConstantIndices,
838 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
839 LLVMValueRef *ConstantIndices,
840 unsigned NumIndices) {
841 Constant* Val = unwrap<Constant>(ConstantVal);
842 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
843 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
846 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
847 return wrap(ConstantExpr::getTrunc(
848 unwrap<Constant>(ConstantVal),
852 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
853 return wrap(ConstantExpr::getSExt(
854 unwrap<Constant>(ConstantVal),
858 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
859 return wrap(ConstantExpr::getZExt(
860 unwrap<Constant>(ConstantVal),
864 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
865 return wrap(ConstantExpr::getFPTrunc(
866 unwrap<Constant>(ConstantVal),
870 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
871 return wrap(ConstantExpr::getFPExtend(
872 unwrap<Constant>(ConstantVal),
876 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
877 return wrap(ConstantExpr::getUIToFP(
878 unwrap<Constant>(ConstantVal),
882 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
883 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
887 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
888 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
892 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
893 return wrap(ConstantExpr::getFPToSI(
894 unwrap<Constant>(ConstantVal),
898 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
899 return wrap(ConstantExpr::getPtrToInt(
900 unwrap<Constant>(ConstantVal),
904 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
905 return wrap(ConstantExpr::getIntToPtr(
906 unwrap<Constant>(ConstantVal),
910 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
911 return wrap(ConstantExpr::getBitCast(
912 unwrap<Constant>(ConstantVal),
916 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
917 LLVMTypeRef ToType) {
918 return wrap(ConstantExpr::getZExtOrBitCast(
919 unwrap<Constant>(ConstantVal),
923 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
924 LLVMTypeRef ToType) {
925 return wrap(ConstantExpr::getSExtOrBitCast(
926 unwrap<Constant>(ConstantVal),
930 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
931 LLVMTypeRef ToType) {
932 return wrap(ConstantExpr::getTruncOrBitCast(
933 unwrap<Constant>(ConstantVal),
937 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
938 LLVMTypeRef ToType) {
939 return wrap(ConstantExpr::getPointerCast(
940 unwrap<Constant>(ConstantVal),
944 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
946 return wrap(ConstantExpr::getIntegerCast(
947 unwrap<Constant>(ConstantVal),
952 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
953 return wrap(ConstantExpr::getFPCast(
954 unwrap<Constant>(ConstantVal),
958 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
959 LLVMValueRef ConstantIfTrue,
960 LLVMValueRef ConstantIfFalse) {
961 return wrap(ConstantExpr::getSelect(
962 unwrap<Constant>(ConstantCondition),
963 unwrap<Constant>(ConstantIfTrue),
964 unwrap<Constant>(ConstantIfFalse)));
967 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
968 LLVMValueRef IndexConstant) {
969 return wrap(ConstantExpr::getExtractElement(
970 unwrap<Constant>(VectorConstant),
971 unwrap<Constant>(IndexConstant)));
974 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
975 LLVMValueRef ElementValueConstant,
976 LLVMValueRef IndexConstant) {
977 return wrap(ConstantExpr::getInsertElement(
978 unwrap<Constant>(VectorConstant),
979 unwrap<Constant>(ElementValueConstant),
980 unwrap<Constant>(IndexConstant)));
983 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
984 LLVMValueRef VectorBConstant,
985 LLVMValueRef MaskConstant) {
986 return wrap(ConstantExpr::getShuffleVector(
987 unwrap<Constant>(VectorAConstant),
988 unwrap<Constant>(VectorBConstant),
989 unwrap<Constant>(MaskConstant)));
992 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
994 return wrap(ConstantExpr::getExtractValue(
995 unwrap<Constant>(AggConstant),
999 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1000 LLVMValueRef ElementValueConstant,
1001 unsigned *IdxList, unsigned NumIdx) {
1002 return wrap(ConstantExpr::getInsertValue(
1003 unwrap<Constant>(AggConstant),
1004 unwrap<Constant>(ElementValueConstant),
1008 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1009 const char *Constraints,
1010 LLVMBool HasSideEffects,
1011 LLVMBool IsAlignStack) {
1012 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1013 Constraints, HasSideEffects, IsAlignStack));
1016 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1017 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1020 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1022 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1023 return wrap(unwrap<GlobalValue>(Global)->getParent());
1026 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1027 return unwrap<GlobalValue>(Global)->isDeclaration();
1030 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1031 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1033 assert(false && "Unhandled Linkage Type.");
1034 case GlobalValue::ExternalLinkage:
1035 return LLVMExternalLinkage;
1036 case GlobalValue::AvailableExternallyLinkage:
1037 return LLVMAvailableExternallyLinkage;
1038 case GlobalValue::LinkOnceAnyLinkage:
1039 return LLVMLinkOnceAnyLinkage;
1040 case GlobalValue::LinkOnceODRLinkage:
1041 return LLVMLinkOnceODRLinkage;
1042 case GlobalValue::WeakAnyLinkage:
1043 return LLVMWeakAnyLinkage;
1044 case GlobalValue::WeakODRLinkage:
1045 return LLVMWeakODRLinkage;
1046 case GlobalValue::AppendingLinkage:
1047 return LLVMAppendingLinkage;
1048 case GlobalValue::InternalLinkage:
1049 return LLVMInternalLinkage;
1050 case GlobalValue::PrivateLinkage:
1051 return LLVMPrivateLinkage;
1052 case GlobalValue::LinkerPrivateLinkage:
1053 return LLVMLinkerPrivateLinkage;
1054 case GlobalValue::DLLImportLinkage:
1055 return LLVMDLLImportLinkage;
1056 case GlobalValue::DLLExportLinkage:
1057 return LLVMDLLExportLinkage;
1058 case GlobalValue::ExternalWeakLinkage:
1059 return LLVMExternalWeakLinkage;
1060 case GlobalValue::CommonLinkage:
1061 return LLVMCommonLinkage;
1064 // Should never get here.
1065 return static_cast<LLVMLinkage>(0);
1068 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1069 GlobalValue *GV = unwrap<GlobalValue>(Global);
1073 assert(false && "Unhandled Linkage Type.");
1074 case LLVMExternalLinkage:
1075 GV->setLinkage(GlobalValue::ExternalLinkage);
1077 case LLVMAvailableExternallyLinkage:
1078 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1080 case LLVMLinkOnceAnyLinkage:
1081 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1083 case LLVMLinkOnceODRLinkage:
1084 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1086 case LLVMWeakAnyLinkage:
1087 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1089 case LLVMWeakODRLinkage:
1090 GV->setLinkage(GlobalValue::WeakODRLinkage);
1092 case LLVMAppendingLinkage:
1093 GV->setLinkage(GlobalValue::AppendingLinkage);
1095 case LLVMInternalLinkage:
1096 GV->setLinkage(GlobalValue::InternalLinkage);
1098 case LLVMPrivateLinkage:
1099 GV->setLinkage(GlobalValue::PrivateLinkage);
1101 case LLVMLinkerPrivateLinkage:
1102 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1104 case LLVMDLLImportLinkage:
1105 GV->setLinkage(GlobalValue::DLLImportLinkage);
1107 case LLVMDLLExportLinkage:
1108 GV->setLinkage(GlobalValue::DLLExportLinkage);
1110 case LLVMExternalWeakLinkage:
1111 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1113 case LLVMGhostLinkage:
1115 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1117 case LLVMCommonLinkage:
1118 GV->setLinkage(GlobalValue::CommonLinkage);
1123 const char *LLVMGetSection(LLVMValueRef Global) {
1124 return unwrap<GlobalValue>(Global)->getSection().c_str();
1127 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1128 unwrap<GlobalValue>(Global)->setSection(Section);
1131 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1132 return static_cast<LLVMVisibility>(
1133 unwrap<GlobalValue>(Global)->getVisibility());
1136 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1137 unwrap<GlobalValue>(Global)
1138 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1141 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1142 return unwrap<GlobalValue>(Global)->getAlignment();
1145 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1146 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1149 /*--.. Operations on global variables ......................................--*/
1151 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1152 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1153 GlobalValue::ExternalLinkage, 0, Name));
1156 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1158 unsigned AddressSpace) {
1159 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1160 GlobalValue::ExternalLinkage, 0, Name, false,
1164 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1165 return wrap(unwrap(M)->getNamedGlobal(Name));
1168 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1169 Module *Mod = unwrap(M);
1170 Module::global_iterator I = Mod->global_begin();
1171 if (I == Mod->global_end())
1176 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1177 Module *Mod = unwrap(M);
1178 Module::global_iterator I = Mod->global_end();
1179 if (I == Mod->global_begin())
1184 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1185 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1186 Module::global_iterator I = GV;
1187 if (++I == GV->getParent()->global_end())
1192 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1193 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1194 Module::global_iterator I = GV;
1195 if (I == GV->getParent()->global_begin())
1200 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1201 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1204 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1205 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1206 if ( !GV->hasInitializer() )
1208 return wrap(GV->getInitializer());
1211 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1212 unwrap<GlobalVariable>(GlobalVar)
1213 ->setInitializer(unwrap<Constant>(ConstantVal));
1216 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1217 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1220 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1221 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1224 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1225 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1228 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1229 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1232 /*--.. Operations on aliases ......................................--*/
1234 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1236 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1237 unwrap<Constant>(Aliasee), unwrap (M)));
1240 /*--.. Operations on functions .............................................--*/
1242 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1243 LLVMTypeRef FunctionTy) {
1244 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1245 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1248 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1249 return wrap(unwrap(M)->getFunction(Name));
1252 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1253 Module *Mod = unwrap(M);
1254 Module::iterator I = Mod->begin();
1255 if (I == Mod->end())
1260 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1261 Module *Mod = unwrap(M);
1262 Module::iterator I = Mod->end();
1263 if (I == Mod->begin())
1268 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1269 Function *Func = unwrap<Function>(Fn);
1270 Module::iterator I = Func;
1271 if (++I == Func->getParent()->end())
1276 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1277 Function *Func = unwrap<Function>(Fn);
1278 Module::iterator I = Func;
1279 if (I == Func->getParent()->begin())
1284 void LLVMDeleteFunction(LLVMValueRef Fn) {
1285 unwrap<Function>(Fn)->eraseFromParent();
1288 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1289 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1290 return F->getIntrinsicID();
1294 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1295 return unwrap<Function>(Fn)->getCallingConv();
1298 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1299 return unwrap<Function>(Fn)->setCallingConv(
1300 static_cast<CallingConv::ID>(CC));
1303 const char *LLVMGetGC(LLVMValueRef Fn) {
1304 Function *F = unwrap<Function>(Fn);
1305 return F->hasGC()? F->getGC() : 0;
1308 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1309 Function *F = unwrap<Function>(Fn);
1316 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1317 Function *Func = unwrap<Function>(Fn);
1318 const AttrListPtr PAL = Func->getAttributes();
1319 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1320 Func->setAttributes(PALnew);
1323 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1324 Function *Func = unwrap<Function>(Fn);
1325 const AttrListPtr PAL = Func->getAttributes();
1326 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1327 Func->setAttributes(PALnew);
1330 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1331 Function *Func = unwrap<Function>(Fn);
1332 const AttrListPtr PAL = Func->getAttributes();
1333 Attributes attr = PAL.getFnAttributes();
1334 return (LLVMAttribute)attr;
1337 /*--.. Operations on parameters ............................................--*/
1339 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1340 // This function is strictly redundant to
1341 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1342 return unwrap<Function>(FnRef)->arg_size();
1345 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1346 Function *Fn = unwrap<Function>(FnRef);
1347 for (Function::arg_iterator I = Fn->arg_begin(),
1348 E = Fn->arg_end(); I != E; I++)
1349 *ParamRefs++ = wrap(I);
1352 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1353 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1359 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1360 return wrap(unwrap<Argument>(V)->getParent());
1363 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1364 Function *Func = unwrap<Function>(Fn);
1365 Function::arg_iterator I = Func->arg_begin();
1366 if (I == Func->arg_end())
1371 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1372 Function *Func = unwrap<Function>(Fn);
1373 Function::arg_iterator I = Func->arg_end();
1374 if (I == Func->arg_begin())
1379 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1380 Argument *A = unwrap<Argument>(Arg);
1381 Function::arg_iterator I = A;
1382 if (++I == A->getParent()->arg_end())
1387 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1388 Argument *A = unwrap<Argument>(Arg);
1389 Function::arg_iterator I = A;
1390 if (I == A->getParent()->arg_begin())
1395 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1396 unwrap<Argument>(Arg)->addAttr(PA);
1399 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1400 unwrap<Argument>(Arg)->removeAttr(PA);
1403 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1404 Argument *A = unwrap<Argument>(Arg);
1405 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1407 return (LLVMAttribute)attr;
1411 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1412 unwrap<Argument>(Arg)->addAttr(
1413 Attribute::constructAlignmentFromInt(align));
1416 /*--.. Operations on basic blocks ..........................................--*/
1418 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1419 return wrap(static_cast<Value*>(unwrap(BB)));
1422 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1423 return isa<BasicBlock>(unwrap(Val));
1426 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1427 return wrap(unwrap<BasicBlock>(Val));
1430 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1431 return wrap(unwrap(BB)->getParent());
1434 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1435 return unwrap<Function>(FnRef)->size();
1438 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1439 Function *Fn = unwrap<Function>(FnRef);
1440 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1441 *BasicBlocksRefs++ = wrap(I);
1444 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1445 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1448 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1449 Function *Func = unwrap<Function>(Fn);
1450 Function::iterator I = Func->begin();
1451 if (I == Func->end())
1456 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1457 Function *Func = unwrap<Function>(Fn);
1458 Function::iterator I = Func->end();
1459 if (I == Func->begin())
1464 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1465 BasicBlock *Block = unwrap(BB);
1466 Function::iterator I = Block;
1467 if (++I == Block->getParent()->end())
1472 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1473 BasicBlock *Block = unwrap(BB);
1474 Function::iterator I = Block;
1475 if (I == Block->getParent()->begin())
1480 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1483 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1486 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1487 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1490 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1491 LLVMBasicBlockRef BBRef,
1493 BasicBlock *BB = unwrap(BBRef);
1494 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1497 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1499 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1502 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1503 unwrap(BBRef)->eraseFromParent();
1506 /*--.. Operations on instructions ..........................................--*/
1508 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1509 return wrap(unwrap<Instruction>(Inst)->getParent());
1512 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1513 BasicBlock *Block = unwrap(BB);
1514 BasicBlock::iterator I = Block->begin();
1515 if (I == Block->end())
1520 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1521 BasicBlock *Block = unwrap(BB);
1522 BasicBlock::iterator I = Block->end();
1523 if (I == Block->begin())
1528 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1529 Instruction *Instr = unwrap<Instruction>(Inst);
1530 BasicBlock::iterator I = Instr;
1531 if (++I == Instr->getParent()->end())
1536 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1537 Instruction *Instr = unwrap<Instruction>(Inst);
1538 BasicBlock::iterator I = Instr;
1539 if (I == Instr->getParent()->begin())
1544 /*--.. Call and invoke instructions ........................................--*/
1546 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1547 Value *V = unwrap(Instr);
1548 if (CallInst *CI = dyn_cast<CallInst>(V))
1549 return CI->getCallingConv();
1550 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1551 return II->getCallingConv();
1552 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1556 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1557 Value *V = unwrap(Instr);
1558 if (CallInst *CI = dyn_cast<CallInst>(V))
1559 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1560 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1561 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1562 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1565 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1567 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1569 Call.getAttributes().addAttr(index, PA));
1572 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1574 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1576 Call.getAttributes().removeAttr(index, PA));
1579 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1581 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1583 Call.getAttributes().addAttr(index,
1584 Attribute::constructAlignmentFromInt(align)));
1587 /*--.. Operations on call instructions (only) ..............................--*/
1589 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1590 return unwrap<CallInst>(Call)->isTailCall();
1593 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1594 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1597 /*--.. Operations on phi nodes .............................................--*/
1599 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1600 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1601 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1602 for (unsigned I = 0; I != Count; ++I)
1603 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1606 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1607 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1610 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1611 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1614 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1615 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1619 /*===-- Instruction builders ----------------------------------------------===*/
1621 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1622 return wrap(new IRBuilder<>(*unwrap(C)));
1625 LLVMBuilderRef LLVMCreateBuilder(void) {
1626 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1629 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1630 LLVMValueRef Instr) {
1631 BasicBlock *BB = unwrap(Block);
1632 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1633 unwrap(Builder)->SetInsertPoint(BB, I);
1636 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1637 Instruction *I = unwrap<Instruction>(Instr);
1638 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1641 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1642 BasicBlock *BB = unwrap(Block);
1643 unwrap(Builder)->SetInsertPoint(BB);
1646 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1647 return wrap(unwrap(Builder)->GetInsertBlock());
1650 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1651 unwrap(Builder)->ClearInsertionPoint ();
1654 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1655 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1658 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1660 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1663 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1664 delete unwrap(Builder);
1667 /*--.. Metadata builders ...................................................--*/
1669 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1670 unwrap(Builder)->SetCurrentDebugLocation(L? unwrap<MDNode>(L) : NULL);
1673 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1674 return wrap(unwrap(Builder)->getCurrentDebugLocation());
1677 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1678 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1682 /*--.. Instruction builders ................................................--*/
1684 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1685 return wrap(unwrap(B)->CreateRetVoid());
1688 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1689 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1692 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1694 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1697 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1698 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1701 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1702 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1703 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1706 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1707 LLVMBasicBlockRef Else, unsigned NumCases) {
1708 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1711 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1712 unsigned NumDests) {
1713 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1716 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1717 LLVMValueRef *Args, unsigned NumArgs,
1718 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1720 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1721 unwrap(Args), unwrap(Args) + NumArgs,
1725 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1726 return wrap(unwrap(B)->CreateUnwind());
1729 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1730 return wrap(unwrap(B)->CreateUnreachable());
1733 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1734 LLVMBasicBlockRef Dest) {
1735 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1738 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1739 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1742 /*--.. Arithmetic ..........................................................--*/
1744 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1746 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1749 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1751 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1754 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1756 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1759 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1761 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1764 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1766 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1769 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1771 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1774 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1776 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1779 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1781 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1784 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1786 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1789 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1791 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1794 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1796 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1799 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1801 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1804 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1806 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1809 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1811 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1814 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1815 LLVMValueRef RHS, const char *Name) {
1816 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1819 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1821 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1824 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1826 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1829 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1831 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1834 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1836 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1839 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1841 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1844 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1846 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1849 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1851 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1854 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1856 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1859 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1861 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1864 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1866 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1869 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1870 LLVMValueRef LHS, LLVMValueRef RHS,
1872 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1873 unwrap(RHS), Name));
1876 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1877 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1880 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1882 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1885 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1887 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1890 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1891 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1894 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1895 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1898 /*--.. Memory ..............................................................--*/
1900 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1902 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1903 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1904 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1905 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1906 ITy, unwrap(Ty), AllocSize,
1908 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1911 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1912 LLVMValueRef Val, const char *Name) {
1913 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1914 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1915 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1916 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1917 ITy, unwrap(Ty), AllocSize,
1918 unwrap(Val), 0, "");
1919 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1922 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1924 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1927 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1928 LLVMValueRef Val, const char *Name) {
1929 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1932 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1933 return wrap(unwrap(B)->Insert(
1934 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1938 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1940 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1943 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1944 LLVMValueRef PointerVal) {
1945 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1948 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1949 LLVMValueRef *Indices, unsigned NumIndices,
1951 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1952 unwrap(Indices) + NumIndices, Name));
1955 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1956 LLVMValueRef *Indices, unsigned NumIndices,
1958 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1959 unwrap(Indices) + NumIndices, Name));
1962 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1963 unsigned Idx, const char *Name) {
1964 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1967 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1969 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1972 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1974 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1977 /*--.. Casts ...............................................................--*/
1979 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1980 LLVMTypeRef DestTy, const char *Name) {
1981 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1984 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1985 LLVMTypeRef DestTy, const char *Name) {
1986 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1989 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1990 LLVMTypeRef DestTy, const char *Name) {
1991 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1994 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1995 LLVMTypeRef DestTy, const char *Name) {
1996 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1999 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2000 LLVMTypeRef DestTy, const char *Name) {
2001 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2004 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2005 LLVMTypeRef DestTy, const char *Name) {
2006 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2009 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2010 LLVMTypeRef DestTy, const char *Name) {
2011 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2014 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2015 LLVMTypeRef DestTy, const char *Name) {
2016 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2019 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2020 LLVMTypeRef DestTy, const char *Name) {
2021 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2024 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2025 LLVMTypeRef DestTy, const char *Name) {
2026 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2029 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2030 LLVMTypeRef DestTy, const char *Name) {
2031 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2034 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2035 LLVMTypeRef DestTy, const char *Name) {
2036 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2039 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2040 LLVMTypeRef DestTy, const char *Name) {
2041 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2045 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2046 LLVMTypeRef DestTy, const char *Name) {
2047 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2051 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2052 LLVMTypeRef DestTy, const char *Name) {
2053 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2057 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2058 LLVMTypeRef DestTy, const char *Name) {
2059 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2060 unwrap(DestTy), Name));
2063 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2064 LLVMTypeRef DestTy, const char *Name) {
2065 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2068 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2069 LLVMTypeRef DestTy, const char *Name) {
2070 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2071 /*isSigned*/true, Name));
2074 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2075 LLVMTypeRef DestTy, const char *Name) {
2076 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2079 /*--.. Comparisons .........................................................--*/
2081 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2082 LLVMValueRef LHS, LLVMValueRef RHS,
2084 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2085 unwrap(LHS), unwrap(RHS), Name));
2088 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2089 LLVMValueRef LHS, LLVMValueRef RHS,
2091 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2092 unwrap(LHS), unwrap(RHS), Name));
2095 /*--.. Miscellaneous instructions ..........................................--*/
2097 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2098 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2101 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2102 LLVMValueRef *Args, unsigned NumArgs,
2104 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2105 unwrap(Args) + NumArgs, Name));
2108 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2109 LLVMValueRef Then, LLVMValueRef Else,
2111 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2115 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2116 LLVMTypeRef Ty, const char *Name) {
2117 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2120 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2121 LLVMValueRef Index, const char *Name) {
2122 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2126 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2127 LLVMValueRef EltVal, LLVMValueRef Index,
2129 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2130 unwrap(Index), Name));
2133 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2134 LLVMValueRef V2, LLVMValueRef Mask,
2136 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2137 unwrap(Mask), Name));
2140 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2141 unsigned Index, const char *Name) {
2142 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2145 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2146 LLVMValueRef EltVal, unsigned Index,
2148 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2152 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2154 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2157 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2159 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2162 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2163 LLVMValueRef RHS, const char *Name) {
2164 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2168 /*===-- Module providers --------------------------------------------------===*/
2170 LLVMModuleProviderRef
2171 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2172 return reinterpret_cast<LLVMModuleProviderRef>(M);
2175 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2180 /*===-- Memory buffers ----------------------------------------------------===*/
2182 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2184 LLVMMemoryBufferRef *OutMemBuf,
2185 char **OutMessage) {
2188 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2189 *OutMemBuf = wrap(MB);
2193 *OutMessage = strdup(Error.c_str());
2197 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2198 char **OutMessage) {
2199 MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2200 if (!MB->getBufferSize()) {
2202 *OutMessage = strdup("stdin is empty.");
2206 *OutMemBuf = wrap(MB);
2210 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2211 delete unwrap(MemBuf);