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 common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements 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/PassManager.h"
26 #include "llvm/Support/CallSite.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/system_error.h"
38 void llvm::initializeCore(PassRegistry &Registry) {
39 initializeDominatorTreePass(Registry);
40 initializeDominanceFrontierPass(Registry);
41 initializePrintModulePassPass(Registry);
42 initializePrintFunctionPassPass(Registry);
43 initializeVerifierPass(Registry);
44 initializePreVerifierPass(Registry);
47 void LLVMInitializeCore(LLVMPassRegistryRef R) {
48 initializeCore(*unwrap(R));
51 /*===-- Error handling ----------------------------------------------------===*/
53 void LLVMDisposeMessage(char *Message) {
58 /*===-- Operations on contexts --------------------------------------------===*/
60 LLVMContextRef LLVMContextCreate() {
61 return wrap(new LLVMContext());
64 LLVMContextRef LLVMGetGlobalContext() {
65 return wrap(&getGlobalContext());
68 void LLVMContextDispose(LLVMContextRef C) {
72 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
74 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
77 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
78 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
82 /*===-- Operations on modules ---------------------------------------------===*/
84 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
85 return wrap(new Module(ModuleID, getGlobalContext()));
88 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
90 return wrap(new Module(ModuleID, *unwrap(C)));
93 void LLVMDisposeModule(LLVMModuleRef M) {
97 /*--.. Data layout .........................................................--*/
98 const char * LLVMGetDataLayout(LLVMModuleRef M) {
99 return unwrap(M)->getDataLayout().c_str();
102 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
103 unwrap(M)->setDataLayout(Triple);
106 /*--.. Target triple .......................................................--*/
107 const char * LLVMGetTarget(LLVMModuleRef M) {
108 return unwrap(M)->getTargetTriple().c_str();
111 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
112 unwrap(M)->setTargetTriple(Triple);
115 /*--.. Type names ..........................................................--*/
116 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
117 return unwrap(M)->addTypeName(Name, unwrap(Ty));
120 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
121 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
123 TypeSymbolTable::iterator I = TST.find(Name);
128 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
129 return wrap(unwrap(M)->getTypeByName(Name));
132 void LLVMDumpModule(LLVMModuleRef M) {
136 /*--.. Operations on inline assembler ......................................--*/
137 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
138 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
142 /*--.. Operations on module contexts ......................................--*/
143 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
144 return wrap(&unwrap(M)->getContext());
148 /*===-- Operations on types -----------------------------------------------===*/
150 /*--.. Operations on all types (mostly) ....................................--*/
152 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
153 switch (unwrap(Ty)->getTypeID()) {
155 assert(false && "Unhandled TypeID.");
157 return LLVMVoidTypeKind;
158 case Type::FloatTyID:
159 return LLVMFloatTypeKind;
160 case Type::DoubleTyID:
161 return LLVMDoubleTypeKind;
162 case Type::X86_FP80TyID:
163 return LLVMX86_FP80TypeKind;
164 case Type::FP128TyID:
165 return LLVMFP128TypeKind;
166 case Type::PPC_FP128TyID:
167 return LLVMPPC_FP128TypeKind;
168 case Type::LabelTyID:
169 return LLVMLabelTypeKind;
170 case Type::MetadataTyID:
171 return LLVMMetadataTypeKind;
172 case Type::IntegerTyID:
173 return LLVMIntegerTypeKind;
174 case Type::FunctionTyID:
175 return LLVMFunctionTypeKind;
176 case Type::StructTyID:
177 return LLVMStructTypeKind;
178 case Type::ArrayTyID:
179 return LLVMArrayTypeKind;
180 case Type::PointerTyID:
181 return LLVMPointerTypeKind;
182 case Type::OpaqueTyID:
183 return LLVMOpaqueTypeKind;
184 case Type::VectorTyID:
185 return LLVMVectorTypeKind;
186 case Type::X86_MMXTyID:
187 return LLVMX86_MMXTypeKind;
191 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
192 return wrap(&unwrap(Ty)->getContext());
195 /*--.. Operations on integer types .........................................--*/
197 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
198 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
200 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
201 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
203 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
204 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
206 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
207 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
209 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
210 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
212 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
213 return wrap(IntegerType::get(*unwrap(C), NumBits));
216 LLVMTypeRef LLVMInt1Type(void) {
217 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
219 LLVMTypeRef LLVMInt8Type(void) {
220 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
222 LLVMTypeRef LLVMInt16Type(void) {
223 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
225 LLVMTypeRef LLVMInt32Type(void) {
226 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
228 LLVMTypeRef LLVMInt64Type(void) {
229 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
231 LLVMTypeRef LLVMIntType(unsigned NumBits) {
232 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
235 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
236 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
239 /*--.. Operations on real types ............................................--*/
241 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
242 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
244 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
245 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
247 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
248 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
250 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
251 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
253 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
254 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
256 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
257 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
260 LLVMTypeRef LLVMFloatType(void) {
261 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
263 LLVMTypeRef LLVMDoubleType(void) {
264 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
266 LLVMTypeRef LLVMX86FP80Type(void) {
267 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
269 LLVMTypeRef LLVMFP128Type(void) {
270 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
272 LLVMTypeRef LLVMPPCFP128Type(void) {
273 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
275 LLVMTypeRef LLVMX86MMXType(void) {
276 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
279 /*--.. Operations on function types ........................................--*/
281 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
282 LLVMTypeRef *ParamTypes, unsigned ParamCount,
284 std::vector<const Type*> Tys;
285 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
286 Tys.push_back(unwrap(*I));
288 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
291 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
292 return unwrap<FunctionType>(FunctionTy)->isVarArg();
295 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
296 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
299 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
300 return unwrap<FunctionType>(FunctionTy)->getNumParams();
303 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
304 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
305 for (FunctionType::param_iterator I = Ty->param_begin(),
306 E = Ty->param_end(); I != E; ++I)
310 /*--.. Operations on struct types ..........................................--*/
312 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
313 unsigned ElementCount, LLVMBool Packed) {
314 std::vector<const Type*> Tys;
315 for (LLVMTypeRef *I = ElementTypes,
316 *E = ElementTypes + ElementCount; I != E; ++I)
317 Tys.push_back(unwrap(*I));
319 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
322 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
323 unsigned ElementCount, LLVMBool Packed) {
324 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
325 ElementCount, Packed);
329 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
330 return unwrap<StructType>(StructTy)->getNumElements();
333 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
334 StructType *Ty = unwrap<StructType>(StructTy);
335 for (FunctionType::param_iterator I = Ty->element_begin(),
336 E = Ty->element_end(); I != E; ++I)
340 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
341 return unwrap<StructType>(StructTy)->isPacked();
344 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
346 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
347 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
350 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
351 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
354 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
355 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
358 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
359 return wrap(unwrap<SequentialType>(Ty)->getElementType());
362 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
363 return unwrap<ArrayType>(ArrayTy)->getNumElements();
366 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
367 return unwrap<PointerType>(PointerTy)->getAddressSpace();
370 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
371 return unwrap<VectorType>(VectorTy)->getNumElements();
374 /*--.. Operations on other types ...........................................--*/
376 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
377 return wrap(Type::getVoidTy(*unwrap(C)));
379 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
380 return wrap(Type::getLabelTy(*unwrap(C)));
382 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
383 return wrap(OpaqueType::get(*unwrap(C)));
386 LLVMTypeRef LLVMVoidType(void) {
387 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
389 LLVMTypeRef LLVMLabelType(void) {
390 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
392 LLVMTypeRef LLVMOpaqueType(void) {
393 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
396 /*--.. Operations on type handles ..........................................--*/
398 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
399 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
402 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
403 delete unwrap(TypeHandle);
406 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
407 return wrap(unwrap(TypeHandle)->get());
410 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
411 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
415 /*===-- Operations on values ----------------------------------------------===*/
417 /*--.. Operations on all values ............................................--*/
419 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
420 return wrap(unwrap(Val)->getType());
423 const char *LLVMGetValueName(LLVMValueRef Val) {
424 return unwrap(Val)->getName().data();
427 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
428 unwrap(Val)->setName(Name);
431 void LLVMDumpValue(LLVMValueRef Val) {
435 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
436 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
439 int LLVMHasMetadata(LLVMValueRef Inst) {
440 return unwrap<Instruction>(Inst)->hasMetadata();
443 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
444 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
447 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
448 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
451 /*--.. Conversion functions ................................................--*/
453 #define LLVM_DEFINE_VALUE_CAST(name) \
454 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
455 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
458 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
460 /*--.. Operations on Uses ..................................................--*/
461 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
462 Value *V = unwrap(Val);
463 Value::use_iterator I = V->use_begin();
464 if (I == V->use_end())
466 return wrap(&(I.getUse()));
469 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
470 Use *Next = unwrap(U)->getNext();
476 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
477 return wrap(unwrap(U)->getUser());
480 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
481 return wrap(unwrap(U)->get());
484 /*--.. Operations on Users .................................................--*/
485 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
486 return wrap(unwrap<User>(Val)->getOperand(Index));
489 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
490 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
493 int LLVMGetNumOperands(LLVMValueRef Val) {
494 return unwrap<User>(Val)->getNumOperands();
497 /*--.. Operations on constants of any type .................................--*/
499 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
500 return wrap(Constant::getNullValue(unwrap(Ty)));
503 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
504 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
507 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
508 return wrap(UndefValue::get(unwrap(Ty)));
511 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
512 return isa<Constant>(unwrap(Ty));
515 LLVMBool LLVMIsNull(LLVMValueRef Val) {
516 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
517 return C->isNullValue();
521 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
522 return isa<UndefValue>(unwrap(Val));
525 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
527 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
530 /*--.. Operations on metadata nodes ........................................--*/
532 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
534 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
537 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
538 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
541 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
543 return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
546 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
547 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
550 /*--.. Operations on scalar constants ......................................--*/
552 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
553 LLVMBool SignExtend) {
554 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
557 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
559 const uint64_t Words[]) {
560 IntegerType *Ty = unwrap<IntegerType>(IntTy);
561 return wrap(ConstantInt::get(Ty->getContext(),
562 APInt(Ty->getBitWidth(), NumWords, Words)));
565 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
567 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
571 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
572 unsigned SLen, uint8_t Radix) {
573 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
577 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
578 return wrap(ConstantFP::get(unwrap(RealTy), N));
581 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
582 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
585 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
587 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
590 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
591 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
594 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
595 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
598 /*--.. Operations on composite constants ...................................--*/
600 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
602 LLVMBool DontNullTerminate) {
603 /* Inverted the sense of AddNull because ', 0)' is a
604 better mnemonic for null termination than ', 1)'. */
605 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
606 DontNullTerminate == 0));
608 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
609 LLVMValueRef *ConstantVals,
610 unsigned Count, LLVMBool Packed) {
611 return wrap(ConstantStruct::get(*unwrap(C),
612 unwrap<Constant>(ConstantVals, Count),
613 Count, Packed != 0));
616 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
617 LLVMBool DontNullTerminate) {
618 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
621 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
622 LLVMValueRef *ConstantVals, unsigned Length) {
623 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
624 unwrap<Constant>(ConstantVals, Length),
627 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
629 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
632 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
633 return wrap(ConstantVector::get(
634 unwrap<Constant>(ScalarConstantVals, Size), Size));
636 /*--.. Constant expressions ................................................--*/
638 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
639 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
642 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
643 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
646 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
647 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
650 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
651 return wrap(ConstantExpr::getNeg(
652 unwrap<Constant>(ConstantVal)));
655 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
656 return wrap(ConstantExpr::getNSWNeg(
657 unwrap<Constant>(ConstantVal)));
660 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
661 return wrap(ConstantExpr::getNUWNeg(
662 unwrap<Constant>(ConstantVal)));
666 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
667 return wrap(ConstantExpr::getFNeg(
668 unwrap<Constant>(ConstantVal)));
671 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
672 return wrap(ConstantExpr::getNot(
673 unwrap<Constant>(ConstantVal)));
676 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
677 return wrap(ConstantExpr::getAdd(
678 unwrap<Constant>(LHSConstant),
679 unwrap<Constant>(RHSConstant)));
682 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
683 LLVMValueRef RHSConstant) {
684 return wrap(ConstantExpr::getNSWAdd(
685 unwrap<Constant>(LHSConstant),
686 unwrap<Constant>(RHSConstant)));
689 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
690 LLVMValueRef RHSConstant) {
691 return wrap(ConstantExpr::getNUWAdd(
692 unwrap<Constant>(LHSConstant),
693 unwrap<Constant>(RHSConstant)));
696 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
697 return wrap(ConstantExpr::getFAdd(
698 unwrap<Constant>(LHSConstant),
699 unwrap<Constant>(RHSConstant)));
702 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
703 return wrap(ConstantExpr::getSub(
704 unwrap<Constant>(LHSConstant),
705 unwrap<Constant>(RHSConstant)));
708 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
709 LLVMValueRef RHSConstant) {
710 return wrap(ConstantExpr::getNSWSub(
711 unwrap<Constant>(LHSConstant),
712 unwrap<Constant>(RHSConstant)));
715 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
716 LLVMValueRef RHSConstant) {
717 return wrap(ConstantExpr::getNUWSub(
718 unwrap<Constant>(LHSConstant),
719 unwrap<Constant>(RHSConstant)));
722 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
723 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
724 unwrap<Constant>(RHSConstant)));
727 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
728 return wrap(ConstantExpr::getMul(
729 unwrap<Constant>(LHSConstant),
730 unwrap<Constant>(RHSConstant)));
733 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
734 LLVMValueRef RHSConstant) {
735 return wrap(ConstantExpr::getNSWMul(
736 unwrap<Constant>(LHSConstant),
737 unwrap<Constant>(RHSConstant)));
740 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
741 LLVMValueRef RHSConstant) {
742 return wrap(ConstantExpr::getNUWMul(
743 unwrap<Constant>(LHSConstant),
744 unwrap<Constant>(RHSConstant)));
747 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
748 return wrap(ConstantExpr::getFMul(
749 unwrap<Constant>(LHSConstant),
750 unwrap<Constant>(RHSConstant)));
753 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754 return wrap(ConstantExpr::getUDiv(
755 unwrap<Constant>(LHSConstant),
756 unwrap<Constant>(RHSConstant)));
759 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
760 return wrap(ConstantExpr::getSDiv(
761 unwrap<Constant>(LHSConstant),
762 unwrap<Constant>(RHSConstant)));
765 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
766 LLVMValueRef RHSConstant) {
767 return wrap(ConstantExpr::getExactSDiv(
768 unwrap<Constant>(LHSConstant),
769 unwrap<Constant>(RHSConstant)));
772 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
773 return wrap(ConstantExpr::getFDiv(
774 unwrap<Constant>(LHSConstant),
775 unwrap<Constant>(RHSConstant)));
778 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
779 return wrap(ConstantExpr::getURem(
780 unwrap<Constant>(LHSConstant),
781 unwrap<Constant>(RHSConstant)));
784 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
785 return wrap(ConstantExpr::getSRem(
786 unwrap<Constant>(LHSConstant),
787 unwrap<Constant>(RHSConstant)));
790 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
791 return wrap(ConstantExpr::getFRem(
792 unwrap<Constant>(LHSConstant),
793 unwrap<Constant>(RHSConstant)));
796 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
797 return wrap(ConstantExpr::getAnd(
798 unwrap<Constant>(LHSConstant),
799 unwrap<Constant>(RHSConstant)));
802 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
803 return wrap(ConstantExpr::getOr(
804 unwrap<Constant>(LHSConstant),
805 unwrap<Constant>(RHSConstant)));
808 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
809 return wrap(ConstantExpr::getXor(
810 unwrap<Constant>(LHSConstant),
811 unwrap<Constant>(RHSConstant)));
814 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
815 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
816 return wrap(ConstantExpr::getICmp(Predicate,
817 unwrap<Constant>(LHSConstant),
818 unwrap<Constant>(RHSConstant)));
821 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
822 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
823 return wrap(ConstantExpr::getFCmp(Predicate,
824 unwrap<Constant>(LHSConstant),
825 unwrap<Constant>(RHSConstant)));
828 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
829 return wrap(ConstantExpr::getShl(
830 unwrap<Constant>(LHSConstant),
831 unwrap<Constant>(RHSConstant)));
834 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
835 return wrap(ConstantExpr::getLShr(
836 unwrap<Constant>(LHSConstant),
837 unwrap<Constant>(RHSConstant)));
840 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
841 return wrap(ConstantExpr::getAShr(
842 unwrap<Constant>(LHSConstant),
843 unwrap<Constant>(RHSConstant)));
846 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
847 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
848 return wrap(ConstantExpr::getGetElementPtr(
849 unwrap<Constant>(ConstantVal),
850 unwrap<Constant>(ConstantIndices,
855 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
856 LLVMValueRef *ConstantIndices,
857 unsigned NumIndices) {
858 Constant* Val = unwrap<Constant>(ConstantVal);
859 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
860 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
863 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864 return wrap(ConstantExpr::getTrunc(
865 unwrap<Constant>(ConstantVal),
869 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
870 return wrap(ConstantExpr::getSExt(
871 unwrap<Constant>(ConstantVal),
875 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
876 return wrap(ConstantExpr::getZExt(
877 unwrap<Constant>(ConstantVal),
881 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
882 return wrap(ConstantExpr::getFPTrunc(
883 unwrap<Constant>(ConstantVal),
887 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
888 return wrap(ConstantExpr::getFPExtend(
889 unwrap<Constant>(ConstantVal),
893 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
894 return wrap(ConstantExpr::getUIToFP(
895 unwrap<Constant>(ConstantVal),
899 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
900 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
904 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
905 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
909 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
910 return wrap(ConstantExpr::getFPToSI(
911 unwrap<Constant>(ConstantVal),
915 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
916 return wrap(ConstantExpr::getPtrToInt(
917 unwrap<Constant>(ConstantVal),
921 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
922 return wrap(ConstantExpr::getIntToPtr(
923 unwrap<Constant>(ConstantVal),
927 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
928 return wrap(ConstantExpr::getBitCast(
929 unwrap<Constant>(ConstantVal),
933 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
934 LLVMTypeRef ToType) {
935 return wrap(ConstantExpr::getZExtOrBitCast(
936 unwrap<Constant>(ConstantVal),
940 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
941 LLVMTypeRef ToType) {
942 return wrap(ConstantExpr::getSExtOrBitCast(
943 unwrap<Constant>(ConstantVal),
947 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
948 LLVMTypeRef ToType) {
949 return wrap(ConstantExpr::getTruncOrBitCast(
950 unwrap<Constant>(ConstantVal),
954 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
955 LLVMTypeRef ToType) {
956 return wrap(ConstantExpr::getPointerCast(
957 unwrap<Constant>(ConstantVal),
961 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
963 return wrap(ConstantExpr::getIntegerCast(
964 unwrap<Constant>(ConstantVal),
969 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
970 return wrap(ConstantExpr::getFPCast(
971 unwrap<Constant>(ConstantVal),
975 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
976 LLVMValueRef ConstantIfTrue,
977 LLVMValueRef ConstantIfFalse) {
978 return wrap(ConstantExpr::getSelect(
979 unwrap<Constant>(ConstantCondition),
980 unwrap<Constant>(ConstantIfTrue),
981 unwrap<Constant>(ConstantIfFalse)));
984 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
985 LLVMValueRef IndexConstant) {
986 return wrap(ConstantExpr::getExtractElement(
987 unwrap<Constant>(VectorConstant),
988 unwrap<Constant>(IndexConstant)));
991 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
992 LLVMValueRef ElementValueConstant,
993 LLVMValueRef IndexConstant) {
994 return wrap(ConstantExpr::getInsertElement(
995 unwrap<Constant>(VectorConstant),
996 unwrap<Constant>(ElementValueConstant),
997 unwrap<Constant>(IndexConstant)));
1000 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1001 LLVMValueRef VectorBConstant,
1002 LLVMValueRef MaskConstant) {
1003 return wrap(ConstantExpr::getShuffleVector(
1004 unwrap<Constant>(VectorAConstant),
1005 unwrap<Constant>(VectorBConstant),
1006 unwrap<Constant>(MaskConstant)));
1009 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1011 return wrap(ConstantExpr::getExtractValue(
1012 unwrap<Constant>(AggConstant),
1016 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1017 LLVMValueRef ElementValueConstant,
1018 unsigned *IdxList, unsigned NumIdx) {
1019 return wrap(ConstantExpr::getInsertValue(
1020 unwrap<Constant>(AggConstant),
1021 unwrap<Constant>(ElementValueConstant),
1025 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1026 const char *Constraints,
1027 LLVMBool HasSideEffects,
1028 LLVMBool IsAlignStack) {
1029 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1030 Constraints, HasSideEffects, IsAlignStack));
1033 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1034 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1037 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1039 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1040 return wrap(unwrap<GlobalValue>(Global)->getParent());
1043 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1044 return unwrap<GlobalValue>(Global)->isDeclaration();
1047 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1048 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1050 assert(false && "Unhandled Linkage Type.");
1051 case GlobalValue::ExternalLinkage:
1052 return LLVMExternalLinkage;
1053 case GlobalValue::AvailableExternallyLinkage:
1054 return LLVMAvailableExternallyLinkage;
1055 case GlobalValue::LinkOnceAnyLinkage:
1056 return LLVMLinkOnceAnyLinkage;
1057 case GlobalValue::LinkOnceODRLinkage:
1058 return LLVMLinkOnceODRLinkage;
1059 case GlobalValue::WeakAnyLinkage:
1060 return LLVMWeakAnyLinkage;
1061 case GlobalValue::WeakODRLinkage:
1062 return LLVMWeakODRLinkage;
1063 case GlobalValue::AppendingLinkage:
1064 return LLVMAppendingLinkage;
1065 case GlobalValue::InternalLinkage:
1066 return LLVMInternalLinkage;
1067 case GlobalValue::PrivateLinkage:
1068 return LLVMPrivateLinkage;
1069 case GlobalValue::LinkerPrivateLinkage:
1070 return LLVMLinkerPrivateLinkage;
1071 case GlobalValue::LinkerPrivateWeakLinkage:
1072 return LLVMLinkerPrivateWeakLinkage;
1073 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1074 return LLVMLinkerPrivateWeakDefAutoLinkage;
1075 case GlobalValue::DLLImportLinkage:
1076 return LLVMDLLImportLinkage;
1077 case GlobalValue::DLLExportLinkage:
1078 return LLVMDLLExportLinkage;
1079 case GlobalValue::ExternalWeakLinkage:
1080 return LLVMExternalWeakLinkage;
1081 case GlobalValue::CommonLinkage:
1082 return LLVMCommonLinkage;
1085 // Should never get here.
1086 return static_cast<LLVMLinkage>(0);
1089 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1090 GlobalValue *GV = unwrap<GlobalValue>(Global);
1094 assert(false && "Unhandled Linkage Type.");
1095 case LLVMExternalLinkage:
1096 GV->setLinkage(GlobalValue::ExternalLinkage);
1098 case LLVMAvailableExternallyLinkage:
1099 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1101 case LLVMLinkOnceAnyLinkage:
1102 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1104 case LLVMLinkOnceODRLinkage:
1105 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1107 case LLVMWeakAnyLinkage:
1108 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1110 case LLVMWeakODRLinkage:
1111 GV->setLinkage(GlobalValue::WeakODRLinkage);
1113 case LLVMAppendingLinkage:
1114 GV->setLinkage(GlobalValue::AppendingLinkage);
1116 case LLVMInternalLinkage:
1117 GV->setLinkage(GlobalValue::InternalLinkage);
1119 case LLVMPrivateLinkage:
1120 GV->setLinkage(GlobalValue::PrivateLinkage);
1122 case LLVMLinkerPrivateLinkage:
1123 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1125 case LLVMLinkerPrivateWeakLinkage:
1126 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1128 case LLVMLinkerPrivateWeakDefAutoLinkage:
1129 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1131 case LLVMDLLImportLinkage:
1132 GV->setLinkage(GlobalValue::DLLImportLinkage);
1134 case LLVMDLLExportLinkage:
1135 GV->setLinkage(GlobalValue::DLLExportLinkage);
1137 case LLVMExternalWeakLinkage:
1138 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1140 case LLVMGhostLinkage:
1142 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1144 case LLVMCommonLinkage:
1145 GV->setLinkage(GlobalValue::CommonLinkage);
1150 const char *LLVMGetSection(LLVMValueRef Global) {
1151 return unwrap<GlobalValue>(Global)->getSection().c_str();
1154 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1155 unwrap<GlobalValue>(Global)->setSection(Section);
1158 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1159 return static_cast<LLVMVisibility>(
1160 unwrap<GlobalValue>(Global)->getVisibility());
1163 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1164 unwrap<GlobalValue>(Global)
1165 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1168 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1169 return unwrap<GlobalValue>(Global)->getAlignment();
1172 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1173 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1176 /*--.. Operations on global variables ......................................--*/
1178 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1179 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1180 GlobalValue::ExternalLinkage, 0, Name));
1183 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1185 unsigned AddressSpace) {
1186 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1187 GlobalValue::ExternalLinkage, 0, Name, 0,
1188 false, AddressSpace));
1191 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1192 return wrap(unwrap(M)->getNamedGlobal(Name));
1195 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1196 Module *Mod = unwrap(M);
1197 Module::global_iterator I = Mod->global_begin();
1198 if (I == Mod->global_end())
1203 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1204 Module *Mod = unwrap(M);
1205 Module::global_iterator I = Mod->global_end();
1206 if (I == Mod->global_begin())
1211 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1212 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1213 Module::global_iterator I = GV;
1214 if (++I == GV->getParent()->global_end())
1219 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1220 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1221 Module::global_iterator I = GV;
1222 if (I == GV->getParent()->global_begin())
1227 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1228 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1231 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1232 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1233 if ( !GV->hasInitializer() )
1235 return wrap(GV->getInitializer());
1238 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1239 unwrap<GlobalVariable>(GlobalVar)
1240 ->setInitializer(unwrap<Constant>(ConstantVal));
1243 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1244 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1247 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1248 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1251 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1252 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1255 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1256 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1259 /*--.. Operations on aliases ......................................--*/
1261 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1263 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1264 unwrap<Constant>(Aliasee), unwrap (M)));
1267 /*--.. Operations on functions .............................................--*/
1269 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1270 LLVMTypeRef FunctionTy) {
1271 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1272 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1275 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1276 return wrap(unwrap(M)->getFunction(Name));
1279 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1280 Module *Mod = unwrap(M);
1281 Module::iterator I = Mod->begin();
1282 if (I == Mod->end())
1287 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1288 Module *Mod = unwrap(M);
1289 Module::iterator I = Mod->end();
1290 if (I == Mod->begin())
1295 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1296 Function *Func = unwrap<Function>(Fn);
1297 Module::iterator I = Func;
1298 if (++I == Func->getParent()->end())
1303 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1304 Function *Func = unwrap<Function>(Fn);
1305 Module::iterator I = Func;
1306 if (I == Func->getParent()->begin())
1311 void LLVMDeleteFunction(LLVMValueRef Fn) {
1312 unwrap<Function>(Fn)->eraseFromParent();
1315 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1316 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1317 return F->getIntrinsicID();
1321 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1322 return unwrap<Function>(Fn)->getCallingConv();
1325 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1326 return unwrap<Function>(Fn)->setCallingConv(
1327 static_cast<CallingConv::ID>(CC));
1330 const char *LLVMGetGC(LLVMValueRef Fn) {
1331 Function *F = unwrap<Function>(Fn);
1332 return F->hasGC()? F->getGC() : 0;
1335 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1336 Function *F = unwrap<Function>(Fn);
1343 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1344 Function *Func = unwrap<Function>(Fn);
1345 const AttrListPtr PAL = Func->getAttributes();
1346 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1347 Func->setAttributes(PALnew);
1350 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1351 Function *Func = unwrap<Function>(Fn);
1352 const AttrListPtr PAL = Func->getAttributes();
1353 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1354 Func->setAttributes(PALnew);
1357 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1358 Function *Func = unwrap<Function>(Fn);
1359 const AttrListPtr PAL = Func->getAttributes();
1360 Attributes attr = PAL.getFnAttributes();
1361 return (LLVMAttribute)attr;
1364 /*--.. Operations on parameters ............................................--*/
1366 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1367 // This function is strictly redundant to
1368 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1369 return unwrap<Function>(FnRef)->arg_size();
1372 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1373 Function *Fn = unwrap<Function>(FnRef);
1374 for (Function::arg_iterator I = Fn->arg_begin(),
1375 E = Fn->arg_end(); I != E; I++)
1376 *ParamRefs++ = wrap(I);
1379 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1380 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1386 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1387 return wrap(unwrap<Argument>(V)->getParent());
1390 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1391 Function *Func = unwrap<Function>(Fn);
1392 Function::arg_iterator I = Func->arg_begin();
1393 if (I == Func->arg_end())
1398 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1399 Function *Func = unwrap<Function>(Fn);
1400 Function::arg_iterator I = Func->arg_end();
1401 if (I == Func->arg_begin())
1406 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1407 Argument *A = unwrap<Argument>(Arg);
1408 Function::arg_iterator I = A;
1409 if (++I == A->getParent()->arg_end())
1414 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1415 Argument *A = unwrap<Argument>(Arg);
1416 Function::arg_iterator I = A;
1417 if (I == A->getParent()->arg_begin())
1422 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1423 unwrap<Argument>(Arg)->addAttr(PA);
1426 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1427 unwrap<Argument>(Arg)->removeAttr(PA);
1430 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1431 Argument *A = unwrap<Argument>(Arg);
1432 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1434 return (LLVMAttribute)attr;
1438 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1439 unwrap<Argument>(Arg)->addAttr(
1440 Attribute::constructAlignmentFromInt(align));
1443 /*--.. Operations on basic blocks ..........................................--*/
1445 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1446 return wrap(static_cast<Value*>(unwrap(BB)));
1449 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1450 return isa<BasicBlock>(unwrap(Val));
1453 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1454 return wrap(unwrap<BasicBlock>(Val));
1457 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1458 return wrap(unwrap(BB)->getParent());
1461 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1462 return unwrap<Function>(FnRef)->size();
1465 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1466 Function *Fn = unwrap<Function>(FnRef);
1467 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1468 *BasicBlocksRefs++ = wrap(I);
1471 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1472 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1475 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1476 Function *Func = unwrap<Function>(Fn);
1477 Function::iterator I = Func->begin();
1478 if (I == Func->end())
1483 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1484 Function *Func = unwrap<Function>(Fn);
1485 Function::iterator I = Func->end();
1486 if (I == Func->begin())
1491 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1492 BasicBlock *Block = unwrap(BB);
1493 Function::iterator I = Block;
1494 if (++I == Block->getParent()->end())
1499 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1500 BasicBlock *Block = unwrap(BB);
1501 Function::iterator I = Block;
1502 if (I == Block->getParent()->begin())
1507 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1510 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1513 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1514 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1517 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1518 LLVMBasicBlockRef BBRef,
1520 BasicBlock *BB = unwrap(BBRef);
1521 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1524 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1526 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1529 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1530 unwrap(BBRef)->eraseFromParent();
1533 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1534 unwrap(BB)->moveBefore(unwrap(MovePos));
1537 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1538 unwrap(BB)->moveAfter(unwrap(MovePos));
1541 /*--.. Operations on instructions ..........................................--*/
1543 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1544 return wrap(unwrap<Instruction>(Inst)->getParent());
1547 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1548 BasicBlock *Block = unwrap(BB);
1549 BasicBlock::iterator I = Block->begin();
1550 if (I == Block->end())
1555 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1556 BasicBlock *Block = unwrap(BB);
1557 BasicBlock::iterator I = Block->end();
1558 if (I == Block->begin())
1563 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1564 Instruction *Instr = unwrap<Instruction>(Inst);
1565 BasicBlock::iterator I = Instr;
1566 if (++I == Instr->getParent()->end())
1571 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1572 Instruction *Instr = unwrap<Instruction>(Inst);
1573 BasicBlock::iterator I = Instr;
1574 if (I == Instr->getParent()->begin())
1579 /*--.. Call and invoke instructions ........................................--*/
1581 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1582 Value *V = unwrap(Instr);
1583 if (CallInst *CI = dyn_cast<CallInst>(V))
1584 return CI->getCallingConv();
1585 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1586 return II->getCallingConv();
1587 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1591 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1592 Value *V = unwrap(Instr);
1593 if (CallInst *CI = dyn_cast<CallInst>(V))
1594 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1595 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1596 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1597 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1600 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1602 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1604 Call.getAttributes().addAttr(index, PA));
1607 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1609 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1611 Call.getAttributes().removeAttr(index, PA));
1614 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1616 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1618 Call.getAttributes().addAttr(index,
1619 Attribute::constructAlignmentFromInt(align)));
1622 /*--.. Operations on call instructions (only) ..............................--*/
1624 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1625 return unwrap<CallInst>(Call)->isTailCall();
1628 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1629 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1632 /*--.. Operations on phi nodes .............................................--*/
1634 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1635 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1636 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1637 for (unsigned I = 0; I != Count; ++I)
1638 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1641 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1642 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1645 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1646 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1649 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1650 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1654 /*===-- Instruction builders ----------------------------------------------===*/
1656 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1657 return wrap(new IRBuilder<>(*unwrap(C)));
1660 LLVMBuilderRef LLVMCreateBuilder(void) {
1661 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1664 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1665 LLVMValueRef Instr) {
1666 BasicBlock *BB = unwrap(Block);
1667 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1668 unwrap(Builder)->SetInsertPoint(BB, I);
1671 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1672 Instruction *I = unwrap<Instruction>(Instr);
1673 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1676 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1677 BasicBlock *BB = unwrap(Block);
1678 unwrap(Builder)->SetInsertPoint(BB);
1681 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1682 return wrap(unwrap(Builder)->GetInsertBlock());
1685 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1686 unwrap(Builder)->ClearInsertionPoint();
1689 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1690 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1693 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1695 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1698 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1699 delete unwrap(Builder);
1702 /*--.. Metadata builders ...................................................--*/
1704 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1705 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1706 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1709 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1710 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1711 .getAsMDNode(unwrap(Builder)->getContext()));
1714 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1715 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1719 /*--.. Instruction builders ................................................--*/
1721 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1722 return wrap(unwrap(B)->CreateRetVoid());
1725 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1726 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1729 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1731 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1734 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1735 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1738 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1739 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1740 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1743 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1744 LLVMBasicBlockRef Else, unsigned NumCases) {
1745 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1748 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1749 unsigned NumDests) {
1750 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1753 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1754 LLVMValueRef *Args, unsigned NumArgs,
1755 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1757 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1758 unwrap(Args), unwrap(Args) + NumArgs,
1762 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1763 return wrap(unwrap(B)->CreateUnwind());
1766 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1767 return wrap(unwrap(B)->CreateUnreachable());
1770 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1771 LLVMBasicBlockRef Dest) {
1772 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1775 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1776 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1779 /*--.. Arithmetic ..........................................................--*/
1781 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1783 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1786 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1788 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1791 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1793 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1796 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1798 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1801 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1803 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1806 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1808 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1811 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1813 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1816 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1818 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1821 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1823 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1826 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1828 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1831 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1833 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1836 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1838 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1841 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1843 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1846 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1848 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1851 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1852 LLVMValueRef RHS, const char *Name) {
1853 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1856 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1858 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1861 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1863 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1866 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1868 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1871 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1873 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1876 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1878 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1881 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1883 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1886 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1888 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1891 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1893 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1896 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1898 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1901 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1903 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1906 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1907 LLVMValueRef LHS, LLVMValueRef RHS,
1909 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1910 unwrap(RHS), Name));
1913 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1914 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1917 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1919 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1922 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1924 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1927 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1928 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1931 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1932 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1935 /*--.. Memory ..............................................................--*/
1937 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1939 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1940 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1941 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1942 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1943 ITy, unwrap(Ty), AllocSize,
1945 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1948 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1949 LLVMValueRef Val, const char *Name) {
1950 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1951 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1952 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1953 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1954 ITy, unwrap(Ty), AllocSize,
1955 unwrap(Val), 0, "");
1956 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1959 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1961 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1964 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1965 LLVMValueRef Val, const char *Name) {
1966 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1969 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1970 return wrap(unwrap(B)->Insert(
1971 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1975 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1977 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1980 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1981 LLVMValueRef PointerVal) {
1982 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1985 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1986 LLVMValueRef *Indices, unsigned NumIndices,
1988 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1989 unwrap(Indices) + NumIndices, Name));
1992 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1993 LLVMValueRef *Indices, unsigned NumIndices,
1995 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1996 unwrap(Indices) + NumIndices, Name));
1999 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2000 unsigned Idx, const char *Name) {
2001 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2004 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2006 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2009 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2011 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2014 /*--.. Casts ...............................................................--*/
2016 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2017 LLVMTypeRef DestTy, const char *Name) {
2018 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2021 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2022 LLVMTypeRef DestTy, const char *Name) {
2023 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2026 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2027 LLVMTypeRef DestTy, const char *Name) {
2028 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2031 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2032 LLVMTypeRef DestTy, const char *Name) {
2033 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2036 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2037 LLVMTypeRef DestTy, const char *Name) {
2038 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2041 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2042 LLVMTypeRef DestTy, const char *Name) {
2043 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2046 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2047 LLVMTypeRef DestTy, const char *Name) {
2048 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2051 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2052 LLVMTypeRef DestTy, const char *Name) {
2053 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2056 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2057 LLVMTypeRef DestTy, const char *Name) {
2058 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2061 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2062 LLVMTypeRef DestTy, const char *Name) {
2063 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2066 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2067 LLVMTypeRef DestTy, const char *Name) {
2068 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2071 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2072 LLVMTypeRef DestTy, const char *Name) {
2073 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2076 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2077 LLVMTypeRef DestTy, const char *Name) {
2078 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2082 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2083 LLVMTypeRef DestTy, const char *Name) {
2084 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2088 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2089 LLVMTypeRef DestTy, const char *Name) {
2090 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2094 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2095 LLVMTypeRef DestTy, const char *Name) {
2096 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2097 unwrap(DestTy), Name));
2100 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2101 LLVMTypeRef DestTy, const char *Name) {
2102 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2105 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2106 LLVMTypeRef DestTy, const char *Name) {
2107 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2108 /*isSigned*/true, Name));
2111 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2112 LLVMTypeRef DestTy, const char *Name) {
2113 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2116 /*--.. Comparisons .........................................................--*/
2118 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2119 LLVMValueRef LHS, LLVMValueRef RHS,
2121 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2122 unwrap(LHS), unwrap(RHS), Name));
2125 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2126 LLVMValueRef LHS, LLVMValueRef RHS,
2128 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2129 unwrap(LHS), unwrap(RHS), Name));
2132 /*--.. Miscellaneous instructions ..........................................--*/
2134 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2135 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2138 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2139 LLVMValueRef *Args, unsigned NumArgs,
2141 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2142 unwrap(Args) + NumArgs, Name));
2145 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2146 LLVMValueRef Then, LLVMValueRef Else,
2148 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2152 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2153 LLVMTypeRef Ty, const char *Name) {
2154 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2157 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2158 LLVMValueRef Index, const char *Name) {
2159 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2163 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2164 LLVMValueRef EltVal, LLVMValueRef Index,
2166 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2167 unwrap(Index), Name));
2170 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2171 LLVMValueRef V2, LLVMValueRef Mask,
2173 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2174 unwrap(Mask), Name));
2177 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2178 unsigned Index, const char *Name) {
2179 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2182 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2183 LLVMValueRef EltVal, unsigned Index,
2185 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2189 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2191 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2194 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2196 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2199 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2200 LLVMValueRef RHS, const char *Name) {
2201 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2205 /*===-- Module providers --------------------------------------------------===*/
2207 LLVMModuleProviderRef
2208 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2209 return reinterpret_cast<LLVMModuleProviderRef>(M);
2212 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2217 /*===-- Memory buffers ----------------------------------------------------===*/
2219 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2221 LLVMMemoryBufferRef *OutMemBuf,
2222 char **OutMessage) {
2224 OwningPtr<MemoryBuffer> MB;
2226 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2227 *OutMemBuf = wrap(MB.take());
2231 *OutMessage = strdup(ec.message().c_str());
2235 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2236 char **OutMessage) {
2237 OwningPtr<MemoryBuffer> MB;
2239 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2240 *OutMemBuf = wrap(MB.take());
2244 *OutMessage = strdup(ec.message().c_str());
2248 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2249 delete unwrap(MemBuf);
2252 /*===-- Pass Registry -----------------------------------------------------===*/
2254 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2255 return wrap(PassRegistry::getPassRegistry());
2258 /*===-- Pass Manager ------------------------------------------------------===*/
2260 LLVMPassManagerRef LLVMCreatePassManager() {
2261 return wrap(new PassManager());
2264 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2265 return wrap(new FunctionPassManager(unwrap(M)));
2268 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2269 return LLVMCreateFunctionPassManagerForModule(
2270 reinterpret_cast<LLVMModuleRef>(P));
2273 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2274 return unwrap<PassManager>(PM)->run(*unwrap(M));
2277 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2278 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2281 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2282 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2285 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2286 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2289 void LLVMDisposePassManager(LLVMPassManagerRef PM) {