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,
331 unsigned ElementCount, int Packed) {
332 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
336 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
337 return unwrap<UnionType>(UnionTy)->getNumElements();
340 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
341 UnionType *Ty = unwrap<UnionType>(UnionTy);
342 for (FunctionType::param_iterator I = Ty->element_begin(),
343 E = Ty->element_end(); I != E; ++I)
347 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
349 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
350 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
353 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
354 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
357 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
358 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
361 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
362 return wrap(unwrap<SequentialType>(Ty)->getElementType());
365 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
366 return unwrap<ArrayType>(ArrayTy)->getNumElements();
369 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
370 return unwrap<PointerType>(PointerTy)->getAddressSpace();
373 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
374 return unwrap<VectorType>(VectorTy)->getNumElements();
377 /*--.. Operations on other types ...........................................--*/
379 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
380 return wrap(Type::getVoidTy(*unwrap(C)));
382 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
383 return wrap(Type::getLabelTy(*unwrap(C)));
385 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
386 return wrap(OpaqueType::get(*unwrap(C)));
389 LLVMTypeRef LLVMVoidType(void) {
390 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
392 LLVMTypeRef LLVMLabelType(void) {
393 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
395 LLVMTypeRef LLVMOpaqueType(void) {
396 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
399 /*--.. Operations on type handles ..........................................--*/
401 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
402 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
405 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
406 delete unwrap(TypeHandle);
409 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
410 return wrap(unwrap(TypeHandle)->get());
413 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
414 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
418 /*===-- Operations on values ----------------------------------------------===*/
420 /*--.. Operations on all values ............................................--*/
422 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
423 return wrap(unwrap(Val)->getType());
426 const char *LLVMGetValueName(LLVMValueRef Val) {
427 return unwrap(Val)->getName().data();
430 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
431 unwrap(Val)->setName(Name);
434 void LLVMDumpValue(LLVMValueRef Val) {
438 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
439 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
442 int LLVMHasMetadata(LLVMValueRef Inst) {
443 return unwrap<Instruction>(Inst)->hasMetadata();
446 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
447 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
450 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
451 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
454 /*--.. Conversion functions ................................................--*/
456 #define LLVM_DEFINE_VALUE_CAST(name) \
457 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
458 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
461 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
463 /*--.. Operations on Uses ..................................................--*/
464 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
465 Value *V = unwrap(Val);
466 Value::use_iterator I = V->use_begin();
467 if (I == V->use_end())
469 return wrap(&(I.getUse()));
472 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
473 Use *Next = unwrap(U)->getNext();
479 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
480 return wrap(unwrap(U)->getUser());
483 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
484 return wrap(unwrap(U)->get());
487 /*--.. Operations on Users .................................................--*/
488 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
489 return wrap(unwrap<User>(Val)->getOperand(Index));
492 /*--.. Operations on constants of any type .................................--*/
494 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
495 return wrap(Constant::getNullValue(unwrap(Ty)));
498 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
499 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
502 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
503 return wrap(UndefValue::get(unwrap(Ty)));
506 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
507 return isa<Constant>(unwrap(Ty));
510 LLVMBool LLVMIsNull(LLVMValueRef Val) {
511 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
512 return C->isNullValue();
516 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
517 return isa<UndefValue>(unwrap(Val));
520 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
522 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
525 /*--.. Operations on metadata nodes ........................................--*/
527 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
529 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
532 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
533 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
536 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
538 return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
541 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
542 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
545 /*--.. Operations on scalar constants ......................................--*/
547 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
548 LLVMBool SignExtend) {
549 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
552 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
554 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
558 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
559 unsigned SLen, uint8_t Radix) {
560 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
564 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
565 return wrap(ConstantFP::get(unwrap(RealTy), N));
568 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
569 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
572 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
574 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
577 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
578 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
581 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
582 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
585 /*--.. Operations on composite constants ...................................--*/
587 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
589 LLVMBool DontNullTerminate) {
590 /* Inverted the sense of AddNull because ', 0)' is a
591 better mnemonic for null termination than ', 1)'. */
592 return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
593 DontNullTerminate == 0));
595 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
596 LLVMValueRef *ConstantVals,
597 unsigned Count, LLVMBool Packed) {
598 return wrap(ConstantStruct::get(*unwrap(C),
599 unwrap<Constant>(ConstantVals, Count),
600 Count, Packed != 0));
603 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
604 LLVMBool DontNullTerminate) {
605 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
608 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
609 LLVMValueRef *ConstantVals, unsigned Length) {
610 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
611 unwrap<Constant>(ConstantVals, Length),
614 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
616 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
619 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
620 return wrap(ConstantVector::get(
621 unwrap<Constant>(ScalarConstantVals, Size), Size));
623 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
624 return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
627 /*--.. Constant expressions ................................................--*/
629 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
630 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
633 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
634 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
637 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
638 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
641 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
642 return wrap(ConstantExpr::getNeg(
643 unwrap<Constant>(ConstantVal)));
646 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
647 return wrap(ConstantExpr::getNSWNeg(
648 unwrap<Constant>(ConstantVal)));
651 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
652 return wrap(ConstantExpr::getNUWNeg(
653 unwrap<Constant>(ConstantVal)));
657 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
658 return wrap(ConstantExpr::getFNeg(
659 unwrap<Constant>(ConstantVal)));
662 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
663 return wrap(ConstantExpr::getNot(
664 unwrap<Constant>(ConstantVal)));
667 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
668 return wrap(ConstantExpr::getAdd(
669 unwrap<Constant>(LHSConstant),
670 unwrap<Constant>(RHSConstant)));
673 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
674 LLVMValueRef RHSConstant) {
675 return wrap(ConstantExpr::getNSWAdd(
676 unwrap<Constant>(LHSConstant),
677 unwrap<Constant>(RHSConstant)));
680 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
681 LLVMValueRef RHSConstant) {
682 return wrap(ConstantExpr::getNUWAdd(
683 unwrap<Constant>(LHSConstant),
684 unwrap<Constant>(RHSConstant)));
687 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
688 return wrap(ConstantExpr::getFAdd(
689 unwrap<Constant>(LHSConstant),
690 unwrap<Constant>(RHSConstant)));
693 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
694 return wrap(ConstantExpr::getSub(
695 unwrap<Constant>(LHSConstant),
696 unwrap<Constant>(RHSConstant)));
699 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
700 LLVMValueRef RHSConstant) {
701 return wrap(ConstantExpr::getNSWSub(
702 unwrap<Constant>(LHSConstant),
703 unwrap<Constant>(RHSConstant)));
706 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
707 LLVMValueRef RHSConstant) {
708 return wrap(ConstantExpr::getNUWSub(
709 unwrap<Constant>(LHSConstant),
710 unwrap<Constant>(RHSConstant)));
713 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
714 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
715 unwrap<Constant>(RHSConstant)));
718 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
719 return wrap(ConstantExpr::getMul(
720 unwrap<Constant>(LHSConstant),
721 unwrap<Constant>(RHSConstant)));
724 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
725 LLVMValueRef RHSConstant) {
726 return wrap(ConstantExpr::getNSWMul(
727 unwrap<Constant>(LHSConstant),
728 unwrap<Constant>(RHSConstant)));
731 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
732 LLVMValueRef RHSConstant) {
733 return wrap(ConstantExpr::getNUWMul(
734 unwrap<Constant>(LHSConstant),
735 unwrap<Constant>(RHSConstant)));
738 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
739 return wrap(ConstantExpr::getFMul(
740 unwrap<Constant>(LHSConstant),
741 unwrap<Constant>(RHSConstant)));
744 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
745 return wrap(ConstantExpr::getUDiv(
746 unwrap<Constant>(LHSConstant),
747 unwrap<Constant>(RHSConstant)));
750 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
751 return wrap(ConstantExpr::getSDiv(
752 unwrap<Constant>(LHSConstant),
753 unwrap<Constant>(RHSConstant)));
756 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
757 LLVMValueRef RHSConstant) {
758 return wrap(ConstantExpr::getExactSDiv(
759 unwrap<Constant>(LHSConstant),
760 unwrap<Constant>(RHSConstant)));
763 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
764 return wrap(ConstantExpr::getFDiv(
765 unwrap<Constant>(LHSConstant),
766 unwrap<Constant>(RHSConstant)));
769 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
770 return wrap(ConstantExpr::getURem(
771 unwrap<Constant>(LHSConstant),
772 unwrap<Constant>(RHSConstant)));
775 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
776 return wrap(ConstantExpr::getSRem(
777 unwrap<Constant>(LHSConstant),
778 unwrap<Constant>(RHSConstant)));
781 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
782 return wrap(ConstantExpr::getFRem(
783 unwrap<Constant>(LHSConstant),
784 unwrap<Constant>(RHSConstant)));
787 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
788 return wrap(ConstantExpr::getAnd(
789 unwrap<Constant>(LHSConstant),
790 unwrap<Constant>(RHSConstant)));
793 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
794 return wrap(ConstantExpr::getOr(
795 unwrap<Constant>(LHSConstant),
796 unwrap<Constant>(RHSConstant)));
799 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
800 return wrap(ConstantExpr::getXor(
801 unwrap<Constant>(LHSConstant),
802 unwrap<Constant>(RHSConstant)));
805 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
806 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
807 return wrap(ConstantExpr::getICmp(Predicate,
808 unwrap<Constant>(LHSConstant),
809 unwrap<Constant>(RHSConstant)));
812 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
813 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
814 return wrap(ConstantExpr::getFCmp(Predicate,
815 unwrap<Constant>(LHSConstant),
816 unwrap<Constant>(RHSConstant)));
819 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
820 return wrap(ConstantExpr::getShl(
821 unwrap<Constant>(LHSConstant),
822 unwrap<Constant>(RHSConstant)));
825 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
826 return wrap(ConstantExpr::getLShr(
827 unwrap<Constant>(LHSConstant),
828 unwrap<Constant>(RHSConstant)));
831 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
832 return wrap(ConstantExpr::getAShr(
833 unwrap<Constant>(LHSConstant),
834 unwrap<Constant>(RHSConstant)));
837 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
838 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
839 return wrap(ConstantExpr::getGetElementPtr(
840 unwrap<Constant>(ConstantVal),
841 unwrap<Constant>(ConstantIndices,
846 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
847 LLVMValueRef *ConstantIndices,
848 unsigned NumIndices) {
849 Constant* Val = unwrap<Constant>(ConstantVal);
850 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
851 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
854 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
855 return wrap(ConstantExpr::getTrunc(
856 unwrap<Constant>(ConstantVal),
860 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
861 return wrap(ConstantExpr::getSExt(
862 unwrap<Constant>(ConstantVal),
866 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
867 return wrap(ConstantExpr::getZExt(
868 unwrap<Constant>(ConstantVal),
872 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
873 return wrap(ConstantExpr::getFPTrunc(
874 unwrap<Constant>(ConstantVal),
878 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
879 return wrap(ConstantExpr::getFPExtend(
880 unwrap<Constant>(ConstantVal),
884 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
885 return wrap(ConstantExpr::getUIToFP(
886 unwrap<Constant>(ConstantVal),
890 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
891 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
895 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
896 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
900 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
901 return wrap(ConstantExpr::getFPToSI(
902 unwrap<Constant>(ConstantVal),
906 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
907 return wrap(ConstantExpr::getPtrToInt(
908 unwrap<Constant>(ConstantVal),
912 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
913 return wrap(ConstantExpr::getIntToPtr(
914 unwrap<Constant>(ConstantVal),
918 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
919 return wrap(ConstantExpr::getBitCast(
920 unwrap<Constant>(ConstantVal),
924 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
925 LLVMTypeRef ToType) {
926 return wrap(ConstantExpr::getZExtOrBitCast(
927 unwrap<Constant>(ConstantVal),
931 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
932 LLVMTypeRef ToType) {
933 return wrap(ConstantExpr::getSExtOrBitCast(
934 unwrap<Constant>(ConstantVal),
938 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
939 LLVMTypeRef ToType) {
940 return wrap(ConstantExpr::getTruncOrBitCast(
941 unwrap<Constant>(ConstantVal),
945 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
946 LLVMTypeRef ToType) {
947 return wrap(ConstantExpr::getPointerCast(
948 unwrap<Constant>(ConstantVal),
952 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
954 return wrap(ConstantExpr::getIntegerCast(
955 unwrap<Constant>(ConstantVal),
960 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
961 return wrap(ConstantExpr::getFPCast(
962 unwrap<Constant>(ConstantVal),
966 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
967 LLVMValueRef ConstantIfTrue,
968 LLVMValueRef ConstantIfFalse) {
969 return wrap(ConstantExpr::getSelect(
970 unwrap<Constant>(ConstantCondition),
971 unwrap<Constant>(ConstantIfTrue),
972 unwrap<Constant>(ConstantIfFalse)));
975 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
976 LLVMValueRef IndexConstant) {
977 return wrap(ConstantExpr::getExtractElement(
978 unwrap<Constant>(VectorConstant),
979 unwrap<Constant>(IndexConstant)));
982 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
983 LLVMValueRef ElementValueConstant,
984 LLVMValueRef IndexConstant) {
985 return wrap(ConstantExpr::getInsertElement(
986 unwrap<Constant>(VectorConstant),
987 unwrap<Constant>(ElementValueConstant),
988 unwrap<Constant>(IndexConstant)));
991 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
992 LLVMValueRef VectorBConstant,
993 LLVMValueRef MaskConstant) {
994 return wrap(ConstantExpr::getShuffleVector(
995 unwrap<Constant>(VectorAConstant),
996 unwrap<Constant>(VectorBConstant),
997 unwrap<Constant>(MaskConstant)));
1000 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1002 return wrap(ConstantExpr::getExtractValue(
1003 unwrap<Constant>(AggConstant),
1007 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1008 LLVMValueRef ElementValueConstant,
1009 unsigned *IdxList, unsigned NumIdx) {
1010 return wrap(ConstantExpr::getInsertValue(
1011 unwrap<Constant>(AggConstant),
1012 unwrap<Constant>(ElementValueConstant),
1016 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1017 const char *Constraints,
1018 LLVMBool HasSideEffects,
1019 LLVMBool IsAlignStack) {
1020 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1021 Constraints, HasSideEffects, IsAlignStack));
1024 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1025 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1028 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1030 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1031 return wrap(unwrap<GlobalValue>(Global)->getParent());
1034 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1035 return unwrap<GlobalValue>(Global)->isDeclaration();
1038 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1039 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1041 assert(false && "Unhandled Linkage Type.");
1042 case GlobalValue::ExternalLinkage:
1043 return LLVMExternalLinkage;
1044 case GlobalValue::AvailableExternallyLinkage:
1045 return LLVMAvailableExternallyLinkage;
1046 case GlobalValue::LinkOnceAnyLinkage:
1047 return LLVMLinkOnceAnyLinkage;
1048 case GlobalValue::LinkOnceODRLinkage:
1049 return LLVMLinkOnceODRLinkage;
1050 case GlobalValue::WeakAnyLinkage:
1051 return LLVMWeakAnyLinkage;
1052 case GlobalValue::WeakODRLinkage:
1053 return LLVMWeakODRLinkage;
1054 case GlobalValue::AppendingLinkage:
1055 return LLVMAppendingLinkage;
1056 case GlobalValue::InternalLinkage:
1057 return LLVMInternalLinkage;
1058 case GlobalValue::PrivateLinkage:
1059 return LLVMPrivateLinkage;
1060 case GlobalValue::LinkerPrivateLinkage:
1061 return LLVMLinkerPrivateLinkage;
1062 case GlobalValue::DLLImportLinkage:
1063 return LLVMDLLImportLinkage;
1064 case GlobalValue::DLLExportLinkage:
1065 return LLVMDLLExportLinkage;
1066 case GlobalValue::ExternalWeakLinkage:
1067 return LLVMExternalWeakLinkage;
1068 case GlobalValue::CommonLinkage:
1069 return LLVMCommonLinkage;
1072 // Should never get here.
1073 return static_cast<LLVMLinkage>(0);
1076 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1077 GlobalValue *GV = unwrap<GlobalValue>(Global);
1081 assert(false && "Unhandled Linkage Type.");
1082 case LLVMExternalLinkage:
1083 GV->setLinkage(GlobalValue::ExternalLinkage);
1085 case LLVMAvailableExternallyLinkage:
1086 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1088 case LLVMLinkOnceAnyLinkage:
1089 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1091 case LLVMLinkOnceODRLinkage:
1092 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1094 case LLVMWeakAnyLinkage:
1095 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1097 case LLVMWeakODRLinkage:
1098 GV->setLinkage(GlobalValue::WeakODRLinkage);
1100 case LLVMAppendingLinkage:
1101 GV->setLinkage(GlobalValue::AppendingLinkage);
1103 case LLVMInternalLinkage:
1104 GV->setLinkage(GlobalValue::InternalLinkage);
1106 case LLVMPrivateLinkage:
1107 GV->setLinkage(GlobalValue::PrivateLinkage);
1109 case LLVMLinkerPrivateLinkage:
1110 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1112 case LLVMDLLImportLinkage:
1113 GV->setLinkage(GlobalValue::DLLImportLinkage);
1115 case LLVMDLLExportLinkage:
1116 GV->setLinkage(GlobalValue::DLLExportLinkage);
1118 case LLVMExternalWeakLinkage:
1119 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1121 case LLVMGhostLinkage:
1123 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1125 case LLVMCommonLinkage:
1126 GV->setLinkage(GlobalValue::CommonLinkage);
1131 const char *LLVMGetSection(LLVMValueRef Global) {
1132 return unwrap<GlobalValue>(Global)->getSection().c_str();
1135 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1136 unwrap<GlobalValue>(Global)->setSection(Section);
1139 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1140 return static_cast<LLVMVisibility>(
1141 unwrap<GlobalValue>(Global)->getVisibility());
1144 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1145 unwrap<GlobalValue>(Global)
1146 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1149 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1150 return unwrap<GlobalValue>(Global)->getAlignment();
1153 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1154 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1157 /*--.. Operations on global variables ......................................--*/
1159 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1160 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1161 GlobalValue::ExternalLinkage, 0, Name));
1164 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1166 unsigned AddressSpace) {
1167 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1168 GlobalValue::ExternalLinkage, 0, Name, 0,
1169 false, AddressSpace));
1172 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1173 return wrap(unwrap(M)->getNamedGlobal(Name));
1176 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1177 Module *Mod = unwrap(M);
1178 Module::global_iterator I = Mod->global_begin();
1179 if (I == Mod->global_end())
1184 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1185 Module *Mod = unwrap(M);
1186 Module::global_iterator I = Mod->global_end();
1187 if (I == Mod->global_begin())
1192 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1193 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1194 Module::global_iterator I = GV;
1195 if (++I == GV->getParent()->global_end())
1200 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1201 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1202 Module::global_iterator I = GV;
1203 if (I == GV->getParent()->global_begin())
1208 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1209 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1212 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1213 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1214 if ( !GV->hasInitializer() )
1216 return wrap(GV->getInitializer());
1219 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1220 unwrap<GlobalVariable>(GlobalVar)
1221 ->setInitializer(unwrap<Constant>(ConstantVal));
1224 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1225 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1228 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1229 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1232 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1233 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1236 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1237 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1240 /*--.. Operations on aliases ......................................--*/
1242 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1244 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1245 unwrap<Constant>(Aliasee), unwrap (M)));
1248 /*--.. Operations on functions .............................................--*/
1250 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1251 LLVMTypeRef FunctionTy) {
1252 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1253 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1256 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1257 return wrap(unwrap(M)->getFunction(Name));
1260 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1261 Module *Mod = unwrap(M);
1262 Module::iterator I = Mod->begin();
1263 if (I == Mod->end())
1268 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1269 Module *Mod = unwrap(M);
1270 Module::iterator I = Mod->end();
1271 if (I == Mod->begin())
1276 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1277 Function *Func = unwrap<Function>(Fn);
1278 Module::iterator I = Func;
1279 if (++I == Func->getParent()->end())
1284 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1285 Function *Func = unwrap<Function>(Fn);
1286 Module::iterator I = Func;
1287 if (I == Func->getParent()->begin())
1292 void LLVMDeleteFunction(LLVMValueRef Fn) {
1293 unwrap<Function>(Fn)->eraseFromParent();
1296 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1297 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1298 return F->getIntrinsicID();
1302 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1303 return unwrap<Function>(Fn)->getCallingConv();
1306 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1307 return unwrap<Function>(Fn)->setCallingConv(
1308 static_cast<CallingConv::ID>(CC));
1311 const char *LLVMGetGC(LLVMValueRef Fn) {
1312 Function *F = unwrap<Function>(Fn);
1313 return F->hasGC()? F->getGC() : 0;
1316 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1317 Function *F = unwrap<Function>(Fn);
1324 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1325 Function *Func = unwrap<Function>(Fn);
1326 const AttrListPtr PAL = Func->getAttributes();
1327 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1328 Func->setAttributes(PALnew);
1331 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1332 Function *Func = unwrap<Function>(Fn);
1333 const AttrListPtr PAL = Func->getAttributes();
1334 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1335 Func->setAttributes(PALnew);
1338 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1339 Function *Func = unwrap<Function>(Fn);
1340 const AttrListPtr PAL = Func->getAttributes();
1341 Attributes attr = PAL.getFnAttributes();
1342 return (LLVMAttribute)attr;
1345 /*--.. Operations on parameters ............................................--*/
1347 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1348 // This function is strictly redundant to
1349 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1350 return unwrap<Function>(FnRef)->arg_size();
1353 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1354 Function *Fn = unwrap<Function>(FnRef);
1355 for (Function::arg_iterator I = Fn->arg_begin(),
1356 E = Fn->arg_end(); I != E; I++)
1357 *ParamRefs++ = wrap(I);
1360 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1361 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1367 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1368 return wrap(unwrap<Argument>(V)->getParent());
1371 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1372 Function *Func = unwrap<Function>(Fn);
1373 Function::arg_iterator I = Func->arg_begin();
1374 if (I == Func->arg_end())
1379 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1380 Function *Func = unwrap<Function>(Fn);
1381 Function::arg_iterator I = Func->arg_end();
1382 if (I == Func->arg_begin())
1387 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1388 Argument *A = unwrap<Argument>(Arg);
1389 Function::arg_iterator I = A;
1390 if (++I == A->getParent()->arg_end())
1395 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1396 Argument *A = unwrap<Argument>(Arg);
1397 Function::arg_iterator I = A;
1398 if (I == A->getParent()->arg_begin())
1403 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1404 unwrap<Argument>(Arg)->addAttr(PA);
1407 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1408 unwrap<Argument>(Arg)->removeAttr(PA);
1411 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1412 Argument *A = unwrap<Argument>(Arg);
1413 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1415 return (LLVMAttribute)attr;
1419 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1420 unwrap<Argument>(Arg)->addAttr(
1421 Attribute::constructAlignmentFromInt(align));
1424 /*--.. Operations on basic blocks ..........................................--*/
1426 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1427 return wrap(static_cast<Value*>(unwrap(BB)));
1430 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1431 return isa<BasicBlock>(unwrap(Val));
1434 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1435 return wrap(unwrap<BasicBlock>(Val));
1438 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1439 return wrap(unwrap(BB)->getParent());
1442 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1443 return unwrap<Function>(FnRef)->size();
1446 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1447 Function *Fn = unwrap<Function>(FnRef);
1448 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1449 *BasicBlocksRefs++ = wrap(I);
1452 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1453 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1456 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1457 Function *Func = unwrap<Function>(Fn);
1458 Function::iterator I = Func->begin();
1459 if (I == Func->end())
1464 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1465 Function *Func = unwrap<Function>(Fn);
1466 Function::iterator I = Func->end();
1467 if (I == Func->begin())
1472 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1473 BasicBlock *Block = unwrap(BB);
1474 Function::iterator I = Block;
1475 if (++I == Block->getParent()->end())
1480 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1481 BasicBlock *Block = unwrap(BB);
1482 Function::iterator I = Block;
1483 if (I == Block->getParent()->begin())
1488 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1491 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1494 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1495 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1498 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1499 LLVMBasicBlockRef BBRef,
1501 BasicBlock *BB = unwrap(BBRef);
1502 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1505 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1507 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1510 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1511 unwrap(BBRef)->eraseFromParent();
1514 /*--.. Operations on instructions ..........................................--*/
1516 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1517 return wrap(unwrap<Instruction>(Inst)->getParent());
1520 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1521 BasicBlock *Block = unwrap(BB);
1522 BasicBlock::iterator I = Block->begin();
1523 if (I == Block->end())
1528 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1529 BasicBlock *Block = unwrap(BB);
1530 BasicBlock::iterator I = Block->end();
1531 if (I == Block->begin())
1536 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1537 Instruction *Instr = unwrap<Instruction>(Inst);
1538 BasicBlock::iterator I = Instr;
1539 if (++I == Instr->getParent()->end())
1544 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1545 Instruction *Instr = unwrap<Instruction>(Inst);
1546 BasicBlock::iterator I = Instr;
1547 if (I == Instr->getParent()->begin())
1552 /*--.. Call and invoke instructions ........................................--*/
1554 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1555 Value *V = unwrap(Instr);
1556 if (CallInst *CI = dyn_cast<CallInst>(V))
1557 return CI->getCallingConv();
1558 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1559 return II->getCallingConv();
1560 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1564 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1565 Value *V = unwrap(Instr);
1566 if (CallInst *CI = dyn_cast<CallInst>(V))
1567 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1568 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1569 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1570 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1573 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1575 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1577 Call.getAttributes().addAttr(index, PA));
1580 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1582 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1584 Call.getAttributes().removeAttr(index, PA));
1587 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1589 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1591 Call.getAttributes().addAttr(index,
1592 Attribute::constructAlignmentFromInt(align)));
1595 /*--.. Operations on call instructions (only) ..............................--*/
1597 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1598 return unwrap<CallInst>(Call)->isTailCall();
1601 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1602 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1605 /*--.. Operations on phi nodes .............................................--*/
1607 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1608 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1609 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1610 for (unsigned I = 0; I != Count; ++I)
1611 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1614 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1615 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1618 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1619 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1622 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1623 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1627 /*===-- Instruction builders ----------------------------------------------===*/
1629 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1630 return wrap(new IRBuilder<>(*unwrap(C)));
1633 LLVMBuilderRef LLVMCreateBuilder(void) {
1634 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1637 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1638 LLVMValueRef Instr) {
1639 BasicBlock *BB = unwrap(Block);
1640 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1641 unwrap(Builder)->SetInsertPoint(BB, I);
1644 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1645 Instruction *I = unwrap<Instruction>(Instr);
1646 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1649 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1650 BasicBlock *BB = unwrap(Block);
1651 unwrap(Builder)->SetInsertPoint(BB);
1654 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1655 return wrap(unwrap(Builder)->GetInsertBlock());
1658 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1659 unwrap(Builder)->ClearInsertionPoint();
1662 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1663 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1666 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1668 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1671 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1672 delete unwrap(Builder);
1675 /*--.. Metadata builders ...................................................--*/
1677 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1678 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1679 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1682 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1683 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1684 .getAsMDNode(unwrap(Builder)->getContext()));
1687 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1688 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1692 /*--.. Instruction builders ................................................--*/
1694 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1695 return wrap(unwrap(B)->CreateRetVoid());
1698 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1699 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1702 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1704 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1707 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1708 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1711 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1712 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1713 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1716 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1717 LLVMBasicBlockRef Else, unsigned NumCases) {
1718 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1721 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1722 unsigned NumDests) {
1723 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1726 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1727 LLVMValueRef *Args, unsigned NumArgs,
1728 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1730 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1731 unwrap(Args), unwrap(Args) + NumArgs,
1735 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1736 return wrap(unwrap(B)->CreateUnwind());
1739 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1740 return wrap(unwrap(B)->CreateUnreachable());
1743 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1744 LLVMBasicBlockRef Dest) {
1745 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1748 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1749 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1752 /*--.. Arithmetic ..........................................................--*/
1754 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1756 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1759 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1761 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1764 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1766 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1769 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1771 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1774 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1776 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1779 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1781 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1784 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1786 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1789 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1791 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1794 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1796 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1799 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1801 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1804 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1806 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1809 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1811 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1814 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1816 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1819 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1821 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1824 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1825 LLVMValueRef RHS, const char *Name) {
1826 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1829 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1831 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1834 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1836 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1839 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1841 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1844 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1846 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1849 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1851 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1854 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1856 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1859 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1861 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1864 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1866 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1869 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1871 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1874 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1876 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1879 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1880 LLVMValueRef LHS, LLVMValueRef RHS,
1882 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1883 unwrap(RHS), Name));
1886 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1887 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1890 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1892 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1895 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1897 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1900 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1901 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1904 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1905 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1908 /*--.. Memory ..............................................................--*/
1910 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1912 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1913 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1914 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1915 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1916 ITy, unwrap(Ty), AllocSize,
1918 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1921 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1922 LLVMValueRef Val, const char *Name) {
1923 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1924 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1925 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1926 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1927 ITy, unwrap(Ty), AllocSize,
1928 unwrap(Val), 0, "");
1929 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1932 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1934 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1937 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1938 LLVMValueRef Val, const char *Name) {
1939 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1942 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1943 return wrap(unwrap(B)->Insert(
1944 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1948 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1950 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1953 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1954 LLVMValueRef PointerVal) {
1955 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1958 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1959 LLVMValueRef *Indices, unsigned NumIndices,
1961 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1962 unwrap(Indices) + NumIndices, Name));
1965 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1966 LLVMValueRef *Indices, unsigned NumIndices,
1968 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1969 unwrap(Indices) + NumIndices, Name));
1972 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1973 unsigned Idx, const char *Name) {
1974 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1977 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1979 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1982 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1984 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1987 /*--.. Casts ...............................................................--*/
1989 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1990 LLVMTypeRef DestTy, const char *Name) {
1991 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1994 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1995 LLVMTypeRef DestTy, const char *Name) {
1996 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1999 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2000 LLVMTypeRef DestTy, const char *Name) {
2001 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2004 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2005 LLVMTypeRef DestTy, const char *Name) {
2006 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2009 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2010 LLVMTypeRef DestTy, const char *Name) {
2011 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2014 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2015 LLVMTypeRef DestTy, const char *Name) {
2016 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2019 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2020 LLVMTypeRef DestTy, const char *Name) {
2021 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2024 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2025 LLVMTypeRef DestTy, const char *Name) {
2026 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2029 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2030 LLVMTypeRef DestTy, const char *Name) {
2031 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2034 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2035 LLVMTypeRef DestTy, const char *Name) {
2036 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2039 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2040 LLVMTypeRef DestTy, const char *Name) {
2041 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2044 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2045 LLVMTypeRef DestTy, const char *Name) {
2046 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2049 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2050 LLVMTypeRef DestTy, const char *Name) {
2051 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2055 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2056 LLVMTypeRef DestTy, const char *Name) {
2057 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2061 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2062 LLVMTypeRef DestTy, const char *Name) {
2063 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2067 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2068 LLVMTypeRef DestTy, const char *Name) {
2069 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2070 unwrap(DestTy), Name));
2073 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2074 LLVMTypeRef DestTy, const char *Name) {
2075 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2078 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2079 LLVMTypeRef DestTy, const char *Name) {
2080 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2081 /*isSigned*/true, Name));
2084 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2085 LLVMTypeRef DestTy, const char *Name) {
2086 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2089 /*--.. Comparisons .........................................................--*/
2091 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2092 LLVMValueRef LHS, LLVMValueRef RHS,
2094 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2095 unwrap(LHS), unwrap(RHS), Name));
2098 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2099 LLVMValueRef LHS, LLVMValueRef RHS,
2101 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2102 unwrap(LHS), unwrap(RHS), Name));
2105 /*--.. Miscellaneous instructions ..........................................--*/
2107 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2108 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2111 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2112 LLVMValueRef *Args, unsigned NumArgs,
2114 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2115 unwrap(Args) + NumArgs, Name));
2118 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2119 LLVMValueRef Then, LLVMValueRef Else,
2121 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2125 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2126 LLVMTypeRef Ty, const char *Name) {
2127 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2130 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2131 LLVMValueRef Index, const char *Name) {
2132 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2136 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2137 LLVMValueRef EltVal, LLVMValueRef Index,
2139 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2140 unwrap(Index), Name));
2143 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2144 LLVMValueRef V2, LLVMValueRef Mask,
2146 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2147 unwrap(Mask), Name));
2150 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2151 unsigned Index, const char *Name) {
2152 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2155 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2156 LLVMValueRef EltVal, unsigned Index,
2158 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2162 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2164 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2167 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2169 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2172 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2173 LLVMValueRef RHS, const char *Name) {
2174 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2178 /*===-- Module providers --------------------------------------------------===*/
2180 LLVMModuleProviderRef
2181 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2182 return reinterpret_cast<LLVMModuleProviderRef>(M);
2185 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2190 /*===-- Memory buffers ----------------------------------------------------===*/
2192 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2194 LLVMMemoryBufferRef *OutMemBuf,
2195 char **OutMessage) {
2198 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2199 *OutMemBuf = wrap(MB);
2203 *OutMessage = strdup(Error.c_str());
2207 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2208 char **OutMessage) {
2209 MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2210 if (!MB->getBufferSize()) {
2212 *OutMessage = strdup("stdin is empty.");
2216 *OutMemBuf = wrap(MB);
2220 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2221 delete unwrap(MemBuf);