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/InlineAsm.h"
23 #include "llvm/IntrinsicInst.h"
24 #include "llvm/PassManager.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"
30 #include "llvm/Support/system_error.h"
37 void llvm::initializeCore(PassRegistry &Registry) {
38 initializeDominatorTreePass(Registry);
39 initializePrintModulePassPass(Registry);
40 initializePrintFunctionPassPass(Registry);
41 initializeVerifierPass(Registry);
42 initializePreVerifierPass(Registry);
45 void LLVMInitializeCore(LLVMPassRegistryRef R) {
46 initializeCore(*unwrap(R));
49 /*===-- Error handling ----------------------------------------------------===*/
51 void LLVMDisposeMessage(char *Message) {
56 /*===-- Operations on contexts --------------------------------------------===*/
58 LLVMContextRef LLVMContextCreate() {
59 return wrap(new LLVMContext());
62 LLVMContextRef LLVMGetGlobalContext() {
63 return wrap(&getGlobalContext());
66 void LLVMContextDispose(LLVMContextRef C) {
70 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
72 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
75 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
76 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
80 /*===-- Operations on modules ---------------------------------------------===*/
82 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
83 return wrap(new Module(ModuleID, getGlobalContext()));
86 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
88 return wrap(new Module(ModuleID, *unwrap(C)));
91 void LLVMDisposeModule(LLVMModuleRef M) {
95 /*--.. Data layout .........................................................--*/
96 const char * LLVMGetDataLayout(LLVMModuleRef M) {
97 return unwrap(M)->getDataLayout().c_str();
100 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
101 unwrap(M)->setDataLayout(Triple);
104 /*--.. Target triple .......................................................--*/
105 const char * LLVMGetTarget(LLVMModuleRef M) {
106 return unwrap(M)->getTargetTriple().c_str();
109 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
110 unwrap(M)->setTargetTriple(Triple);
113 void LLVMDumpModule(LLVMModuleRef M) {
117 /*--.. Operations on inline assembler ......................................--*/
118 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
119 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
123 /*--.. Operations on module contexts ......................................--*/
124 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
125 return wrap(&unwrap(M)->getContext());
129 /*===-- Operations on types -----------------------------------------------===*/
131 /*--.. Operations on all types (mostly) ....................................--*/
133 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
134 switch (unwrap(Ty)->getTypeID()) {
136 assert(false && "Unhandled TypeID.");
138 return LLVMVoidTypeKind;
139 case Type::FloatTyID:
140 return LLVMFloatTypeKind;
141 case Type::DoubleTyID:
142 return LLVMDoubleTypeKind;
143 case Type::X86_FP80TyID:
144 return LLVMX86_FP80TypeKind;
145 case Type::FP128TyID:
146 return LLVMFP128TypeKind;
147 case Type::PPC_FP128TyID:
148 return LLVMPPC_FP128TypeKind;
149 case Type::LabelTyID:
150 return LLVMLabelTypeKind;
151 case Type::MetadataTyID:
152 return LLVMMetadataTypeKind;
153 case Type::IntegerTyID:
154 return LLVMIntegerTypeKind;
155 case Type::FunctionTyID:
156 return LLVMFunctionTypeKind;
157 case Type::StructTyID:
158 return LLVMStructTypeKind;
159 case Type::ArrayTyID:
160 return LLVMArrayTypeKind;
161 case Type::PointerTyID:
162 return LLVMPointerTypeKind;
163 case Type::VectorTyID:
164 return LLVMVectorTypeKind;
165 case Type::X86_MMXTyID:
166 return LLVMX86_MMXTypeKind;
170 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
171 return wrap(&unwrap(Ty)->getContext());
174 /*--.. Operations on integer types .........................................--*/
176 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
177 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
179 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
180 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
182 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
183 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
185 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
186 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
188 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
189 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
191 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
192 return wrap(IntegerType::get(*unwrap(C), NumBits));
195 LLVMTypeRef LLVMInt1Type(void) {
196 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
198 LLVMTypeRef LLVMInt8Type(void) {
199 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
201 LLVMTypeRef LLVMInt16Type(void) {
202 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
204 LLVMTypeRef LLVMInt32Type(void) {
205 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
207 LLVMTypeRef LLVMInt64Type(void) {
208 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
210 LLVMTypeRef LLVMIntType(unsigned NumBits) {
211 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
214 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
215 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
218 /*--.. Operations on real types ............................................--*/
220 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
221 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
223 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
224 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
226 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
227 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
229 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
230 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
232 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
233 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
235 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
236 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
239 LLVMTypeRef LLVMFloatType(void) {
240 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
242 LLVMTypeRef LLVMDoubleType(void) {
243 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
245 LLVMTypeRef LLVMX86FP80Type(void) {
246 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
248 LLVMTypeRef LLVMFP128Type(void) {
249 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
251 LLVMTypeRef LLVMPPCFP128Type(void) {
252 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
254 LLVMTypeRef LLVMX86MMXType(void) {
255 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
258 /*--.. Operations on function types ........................................--*/
260 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
261 LLVMTypeRef *ParamTypes, unsigned ParamCount,
263 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
264 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
267 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
268 return unwrap<FunctionType>(FunctionTy)->isVarArg();
271 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
272 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
275 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
276 return unwrap<FunctionType>(FunctionTy)->getNumParams();
279 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
280 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
281 for (FunctionType::param_iterator I = Ty->param_begin(),
282 E = Ty->param_end(); I != E; ++I)
286 /*--.. Operations on struct types ..........................................--*/
288 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
289 unsigned ElementCount, LLVMBool Packed) {
290 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
291 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
294 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
295 unsigned ElementCount, LLVMBool Packed) {
296 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
297 ElementCount, Packed);
300 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
302 return wrap(StructType::create(*unwrap(C), Name));
305 const char *LLVMGetStructName(LLVMTypeRef Ty)
307 StructType *Type = unwrap<StructType>(Ty);
308 if (!Type->hasName())
310 return Type->getName().data();
313 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
314 unsigned ElementCount, LLVMBool Packed) {
315 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
316 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
319 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
320 return unwrap<StructType>(StructTy)->getNumElements();
323 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
324 StructType *Ty = unwrap<StructType>(StructTy);
325 for (StructType::element_iterator I = Ty->element_begin(),
326 E = Ty->element_end(); I != E; ++I)
330 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
331 return unwrap<StructType>(StructTy)->isPacked();
334 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
335 return unwrap<StructType>(StructTy)->isOpaque();
338 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
339 return wrap(unwrap(M)->getTypeByName(Name));
342 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
344 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
345 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
348 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
349 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
352 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
353 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
356 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
357 return wrap(unwrap<SequentialType>(Ty)->getElementType());
360 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
361 return unwrap<ArrayType>(ArrayTy)->getNumElements();
364 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
365 return unwrap<PointerType>(PointerTy)->getAddressSpace();
368 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
369 return unwrap<VectorType>(VectorTy)->getNumElements();
372 /*--.. Operations on other types ...........................................--*/
374 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
375 return wrap(Type::getVoidTy(*unwrap(C)));
377 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
378 return wrap(Type::getLabelTy(*unwrap(C)));
381 LLVMTypeRef LLVMVoidType(void) {
382 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
384 LLVMTypeRef LLVMLabelType(void) {
385 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
388 /*===-- Operations on values ----------------------------------------------===*/
390 /*--.. Operations on all values ............................................--*/
392 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
393 return wrap(unwrap(Val)->getType());
396 const char *LLVMGetValueName(LLVMValueRef Val) {
397 return unwrap(Val)->getName().data();
400 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
401 unwrap(Val)->setName(Name);
404 void LLVMDumpValue(LLVMValueRef Val) {
408 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
409 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
412 int LLVMHasMetadata(LLVMValueRef Inst) {
413 return unwrap<Instruction>(Inst)->hasMetadata();
416 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
417 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
420 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
421 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
424 /*--.. Conversion functions ................................................--*/
426 #define LLVM_DEFINE_VALUE_CAST(name) \
427 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
428 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
431 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
433 /*--.. Operations on Uses ..................................................--*/
434 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
435 Value *V = unwrap(Val);
436 Value::use_iterator I = V->use_begin();
437 if (I == V->use_end())
439 return wrap(&(I.getUse()));
442 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
443 Use *Next = unwrap(U)->getNext();
449 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
450 return wrap(unwrap(U)->getUser());
453 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
454 return wrap(unwrap(U)->get());
457 /*--.. Operations on Users .................................................--*/
458 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
459 Value *V = unwrap(Val);
460 if (MDNode *MD = dyn_cast<MDNode>(V))
461 return wrap(MD->getOperand(Index));
462 return wrap(cast<User>(V)->getOperand(Index));
465 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
466 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
469 int LLVMGetNumOperands(LLVMValueRef Val) {
470 Value *V = unwrap(Val);
471 if (MDNode *MD = dyn_cast<MDNode>(V))
472 return MD->getNumOperands();
473 return cast<User>(V)->getNumOperands();
476 /*--.. Operations on constants of any type .................................--*/
478 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
479 return wrap(Constant::getNullValue(unwrap(Ty)));
482 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
483 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
486 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
487 return wrap(UndefValue::get(unwrap(Ty)));
490 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
491 return isa<Constant>(unwrap(Ty));
494 LLVMBool LLVMIsNull(LLVMValueRef Val) {
495 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
496 return C->isNullValue();
500 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
501 return isa<UndefValue>(unwrap(Val));
504 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
506 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
509 /*--.. Operations on metadata nodes ........................................--*/
511 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
513 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
516 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
517 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
520 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
522 return wrap(MDNode::get(*unwrap(C),
523 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
526 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
527 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
530 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
531 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
532 *Len = S->getString().size();
533 return S->getString().data();
539 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
541 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
542 return N->getNumOperands();
547 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
549 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
552 for (unsigned i=0;i<N->getNumOperands();i++)
553 Dest[i] = wrap(N->getOperand(i));
556 /*--.. Operations on scalar constants ......................................--*/
558 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
559 LLVMBool SignExtend) {
560 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
563 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
565 const uint64_t Words[]) {
566 IntegerType *Ty = unwrap<IntegerType>(IntTy);
567 return wrap(ConstantInt::get(Ty->getContext(),
568 APInt(Ty->getBitWidth(),
569 makeArrayRef(Words, NumWords))));
572 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
574 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
578 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
579 unsigned SLen, uint8_t Radix) {
580 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
584 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
585 return wrap(ConstantFP::get(unwrap(RealTy), N));
588 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
589 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
592 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
594 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
597 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
598 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
601 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
602 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
605 /*--.. Operations on composite constants ...................................--*/
607 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
609 LLVMBool DontNullTerminate) {
610 /* Inverted the sense of AddNull because ', 0)' is a
611 better mnemonic for null termination than ', 1)'. */
612 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
613 DontNullTerminate == 0));
615 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
616 LLVMValueRef *ConstantVals,
617 unsigned Count, LLVMBool Packed) {
618 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
619 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
623 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
624 LLVMBool DontNullTerminate) {
625 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
628 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
629 LLVMValueRef *ConstantVals, unsigned Length) {
630 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
631 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
633 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
635 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
639 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
640 LLVMValueRef *ConstantVals,
642 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
643 StructType *Ty = cast<StructType>(unwrap(StructTy));
645 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
648 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
649 return wrap(ConstantVector::get(makeArrayRef(
650 unwrap<Constant>(ScalarConstantVals, Size), Size)));
652 /*--.. Constant expressions ................................................--*/
654 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
655 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
658 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
659 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
662 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
663 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
666 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
667 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
670 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
671 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
674 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
675 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
679 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
680 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
683 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
684 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
687 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
688 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
689 unwrap<Constant>(RHSConstant)));
692 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
693 LLVMValueRef RHSConstant) {
694 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
695 unwrap<Constant>(RHSConstant)));
698 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
699 LLVMValueRef RHSConstant) {
700 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
701 unwrap<Constant>(RHSConstant)));
704 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
705 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
706 unwrap<Constant>(RHSConstant)));
709 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
710 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
711 unwrap<Constant>(RHSConstant)));
714 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
715 LLVMValueRef RHSConstant) {
716 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
717 unwrap<Constant>(RHSConstant)));
720 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
721 LLVMValueRef RHSConstant) {
722 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
723 unwrap<Constant>(RHSConstant)));
726 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
728 unwrap<Constant>(RHSConstant)));
731 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
732 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
733 unwrap<Constant>(RHSConstant)));
736 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
737 LLVMValueRef RHSConstant) {
738 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
739 unwrap<Constant>(RHSConstant)));
742 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
743 LLVMValueRef RHSConstant) {
744 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
745 unwrap<Constant>(RHSConstant)));
748 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
749 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
750 unwrap<Constant>(RHSConstant)));
753 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
755 unwrap<Constant>(RHSConstant)));
758 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
759 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
760 unwrap<Constant>(RHSConstant)));
763 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
764 LLVMValueRef RHSConstant) {
765 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
766 unwrap<Constant>(RHSConstant)));
769 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
770 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
771 unwrap<Constant>(RHSConstant)));
774 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
775 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
776 unwrap<Constant>(RHSConstant)));
779 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
780 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
781 unwrap<Constant>(RHSConstant)));
784 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
785 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
786 unwrap<Constant>(RHSConstant)));
789 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
790 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
791 unwrap<Constant>(RHSConstant)));
794 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
795 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
796 unwrap<Constant>(RHSConstant)));
799 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
800 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
801 unwrap<Constant>(RHSConstant)));
804 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
805 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
806 return wrap(ConstantExpr::getICmp(Predicate,
807 unwrap<Constant>(LHSConstant),
808 unwrap<Constant>(RHSConstant)));
811 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
812 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
813 return wrap(ConstantExpr::getFCmp(Predicate,
814 unwrap<Constant>(LHSConstant),
815 unwrap<Constant>(RHSConstant)));
818 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
819 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
820 unwrap<Constant>(RHSConstant)));
823 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
824 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
825 unwrap<Constant>(RHSConstant)));
828 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
829 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
830 unwrap<Constant>(RHSConstant)));
833 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
834 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
835 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
837 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
841 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
842 LLVMValueRef *ConstantIndices,
843 unsigned NumIndices) {
844 Constant* Val = unwrap<Constant>(ConstantVal);
845 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
847 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
850 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
851 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
855 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
856 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
860 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
861 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
865 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
866 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
870 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
871 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
875 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
876 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
880 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
881 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
885 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
886 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
890 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
891 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
895 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
896 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
900 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
901 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
905 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
906 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
910 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
911 LLVMTypeRef ToType) {
912 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
916 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
917 LLVMTypeRef ToType) {
918 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
922 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
923 LLVMTypeRef ToType) {
924 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
928 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
929 LLVMTypeRef ToType) {
930 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
934 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
936 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
937 unwrap(ToType), isSigned));
940 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
941 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
945 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
946 LLVMValueRef ConstantIfTrue,
947 LLVMValueRef ConstantIfFalse) {
948 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
949 unwrap<Constant>(ConstantIfTrue),
950 unwrap<Constant>(ConstantIfFalse)));
953 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
954 LLVMValueRef IndexConstant) {
955 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
956 unwrap<Constant>(IndexConstant)));
959 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
960 LLVMValueRef ElementValueConstant,
961 LLVMValueRef IndexConstant) {
962 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
963 unwrap<Constant>(ElementValueConstant),
964 unwrap<Constant>(IndexConstant)));
967 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
968 LLVMValueRef VectorBConstant,
969 LLVMValueRef MaskConstant) {
970 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
971 unwrap<Constant>(VectorBConstant),
972 unwrap<Constant>(MaskConstant)));
975 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
977 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
978 makeArrayRef(IdxList, NumIdx)));
981 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
982 LLVMValueRef ElementValueConstant,
983 unsigned *IdxList, unsigned NumIdx) {
984 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
985 unwrap<Constant>(ElementValueConstant),
986 makeArrayRef(IdxList, NumIdx)));
989 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
990 const char *Constraints,
991 LLVMBool HasSideEffects,
992 LLVMBool IsAlignStack) {
993 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
994 Constraints, HasSideEffects, IsAlignStack));
997 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
998 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1001 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1003 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1004 return wrap(unwrap<GlobalValue>(Global)->getParent());
1007 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1008 return unwrap<GlobalValue>(Global)->isDeclaration();
1011 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1012 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1014 assert(false && "Unhandled Linkage Type.");
1015 case GlobalValue::ExternalLinkage:
1016 return LLVMExternalLinkage;
1017 case GlobalValue::AvailableExternallyLinkage:
1018 return LLVMAvailableExternallyLinkage;
1019 case GlobalValue::LinkOnceAnyLinkage:
1020 return LLVMLinkOnceAnyLinkage;
1021 case GlobalValue::LinkOnceODRLinkage:
1022 return LLVMLinkOnceODRLinkage;
1023 case GlobalValue::WeakAnyLinkage:
1024 return LLVMWeakAnyLinkage;
1025 case GlobalValue::WeakODRLinkage:
1026 return LLVMWeakODRLinkage;
1027 case GlobalValue::AppendingLinkage:
1028 return LLVMAppendingLinkage;
1029 case GlobalValue::InternalLinkage:
1030 return LLVMInternalLinkage;
1031 case GlobalValue::PrivateLinkage:
1032 return LLVMPrivateLinkage;
1033 case GlobalValue::LinkerPrivateLinkage:
1034 return LLVMLinkerPrivateLinkage;
1035 case GlobalValue::LinkerPrivateWeakLinkage:
1036 return LLVMLinkerPrivateWeakLinkage;
1037 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1038 return LLVMLinkerPrivateWeakDefAutoLinkage;
1039 case GlobalValue::DLLImportLinkage:
1040 return LLVMDLLImportLinkage;
1041 case GlobalValue::DLLExportLinkage:
1042 return LLVMDLLExportLinkage;
1043 case GlobalValue::ExternalWeakLinkage:
1044 return LLVMExternalWeakLinkage;
1045 case GlobalValue::CommonLinkage:
1046 return LLVMCommonLinkage;
1049 // Should never get here.
1050 return static_cast<LLVMLinkage>(0);
1053 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1054 GlobalValue *GV = unwrap<GlobalValue>(Global);
1058 assert(false && "Unhandled Linkage Type.");
1059 case LLVMExternalLinkage:
1060 GV->setLinkage(GlobalValue::ExternalLinkage);
1062 case LLVMAvailableExternallyLinkage:
1063 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1065 case LLVMLinkOnceAnyLinkage:
1066 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1068 case LLVMLinkOnceODRLinkage:
1069 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1071 case LLVMWeakAnyLinkage:
1072 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1074 case LLVMWeakODRLinkage:
1075 GV->setLinkage(GlobalValue::WeakODRLinkage);
1077 case LLVMAppendingLinkage:
1078 GV->setLinkage(GlobalValue::AppendingLinkage);
1080 case LLVMInternalLinkage:
1081 GV->setLinkage(GlobalValue::InternalLinkage);
1083 case LLVMPrivateLinkage:
1084 GV->setLinkage(GlobalValue::PrivateLinkage);
1086 case LLVMLinkerPrivateLinkage:
1087 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1089 case LLVMLinkerPrivateWeakLinkage:
1090 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1092 case LLVMLinkerPrivateWeakDefAutoLinkage:
1093 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1095 case LLVMDLLImportLinkage:
1096 GV->setLinkage(GlobalValue::DLLImportLinkage);
1098 case LLVMDLLExportLinkage:
1099 GV->setLinkage(GlobalValue::DLLExportLinkage);
1101 case LLVMExternalWeakLinkage:
1102 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1104 case LLVMGhostLinkage:
1106 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1108 case LLVMCommonLinkage:
1109 GV->setLinkage(GlobalValue::CommonLinkage);
1114 const char *LLVMGetSection(LLVMValueRef Global) {
1115 return unwrap<GlobalValue>(Global)->getSection().c_str();
1118 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1119 unwrap<GlobalValue>(Global)->setSection(Section);
1122 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1123 return static_cast<LLVMVisibility>(
1124 unwrap<GlobalValue>(Global)->getVisibility());
1127 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1128 unwrap<GlobalValue>(Global)
1129 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1132 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1133 return unwrap<GlobalValue>(Global)->getAlignment();
1136 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1137 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1140 /*--.. Operations on global variables ......................................--*/
1142 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1143 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1144 GlobalValue::ExternalLinkage, 0, Name));
1147 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1149 unsigned AddressSpace) {
1150 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1151 GlobalValue::ExternalLinkage, 0, Name, 0,
1152 false, AddressSpace));
1155 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1156 return wrap(unwrap(M)->getNamedGlobal(Name));
1159 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1160 Module *Mod = unwrap(M);
1161 Module::global_iterator I = Mod->global_begin();
1162 if (I == Mod->global_end())
1167 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1168 Module *Mod = unwrap(M);
1169 Module::global_iterator I = Mod->global_end();
1170 if (I == Mod->global_begin())
1175 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1176 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1177 Module::global_iterator I = GV;
1178 if (++I == GV->getParent()->global_end())
1183 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1184 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1185 Module::global_iterator I = GV;
1186 if (I == GV->getParent()->global_begin())
1191 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1192 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1195 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1196 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1197 if ( !GV->hasInitializer() )
1199 return wrap(GV->getInitializer());
1202 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1203 unwrap<GlobalVariable>(GlobalVar)
1204 ->setInitializer(unwrap<Constant>(ConstantVal));
1207 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1208 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1211 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1212 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1215 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1216 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1219 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1220 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1223 /*--.. Operations on aliases ......................................--*/
1225 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1227 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1228 unwrap<Constant>(Aliasee), unwrap (M)));
1231 /*--.. Operations on functions .............................................--*/
1233 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1234 LLVMTypeRef FunctionTy) {
1235 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1236 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1239 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1240 return wrap(unwrap(M)->getFunction(Name));
1243 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1244 Module *Mod = unwrap(M);
1245 Module::iterator I = Mod->begin();
1246 if (I == Mod->end())
1251 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1252 Module *Mod = unwrap(M);
1253 Module::iterator I = Mod->end();
1254 if (I == Mod->begin())
1259 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1260 Function *Func = unwrap<Function>(Fn);
1261 Module::iterator I = Func;
1262 if (++I == Func->getParent()->end())
1267 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1268 Function *Func = unwrap<Function>(Fn);
1269 Module::iterator I = Func;
1270 if (I == Func->getParent()->begin())
1275 void LLVMDeleteFunction(LLVMValueRef Fn) {
1276 unwrap<Function>(Fn)->eraseFromParent();
1279 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1280 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1281 return F->getIntrinsicID();
1285 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1286 return unwrap<Function>(Fn)->getCallingConv();
1289 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1290 return unwrap<Function>(Fn)->setCallingConv(
1291 static_cast<CallingConv::ID>(CC));
1294 const char *LLVMGetGC(LLVMValueRef Fn) {
1295 Function *F = unwrap<Function>(Fn);
1296 return F->hasGC()? F->getGC() : 0;
1299 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1300 Function *F = unwrap<Function>(Fn);
1307 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1308 Function *Func = unwrap<Function>(Fn);
1309 const AttrListPtr PAL = Func->getAttributes();
1310 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1311 Func->setAttributes(PALnew);
1314 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1315 Function *Func = unwrap<Function>(Fn);
1316 const AttrListPtr PAL = Func->getAttributes();
1317 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1318 Func->setAttributes(PALnew);
1321 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1322 Function *Func = unwrap<Function>(Fn);
1323 const AttrListPtr PAL = Func->getAttributes();
1324 Attributes attr = PAL.getFnAttributes();
1325 return (LLVMAttribute)attr;
1328 /*--.. Operations on parameters ............................................--*/
1330 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1331 // This function is strictly redundant to
1332 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1333 return unwrap<Function>(FnRef)->arg_size();
1336 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1337 Function *Fn = unwrap<Function>(FnRef);
1338 for (Function::arg_iterator I = Fn->arg_begin(),
1339 E = Fn->arg_end(); I != E; I++)
1340 *ParamRefs++ = wrap(I);
1343 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1344 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1350 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1351 return wrap(unwrap<Argument>(V)->getParent());
1354 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1355 Function *Func = unwrap<Function>(Fn);
1356 Function::arg_iterator I = Func->arg_begin();
1357 if (I == Func->arg_end())
1362 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1363 Function *Func = unwrap<Function>(Fn);
1364 Function::arg_iterator I = Func->arg_end();
1365 if (I == Func->arg_begin())
1370 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1371 Argument *A = unwrap<Argument>(Arg);
1372 Function::arg_iterator I = A;
1373 if (++I == A->getParent()->arg_end())
1378 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1379 Argument *A = unwrap<Argument>(Arg);
1380 Function::arg_iterator I = A;
1381 if (I == A->getParent()->arg_begin())
1386 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1387 unwrap<Argument>(Arg)->addAttr(PA);
1390 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1391 unwrap<Argument>(Arg)->removeAttr(PA);
1394 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1395 Argument *A = unwrap<Argument>(Arg);
1396 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1398 return (LLVMAttribute)attr;
1402 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1403 unwrap<Argument>(Arg)->addAttr(
1404 Attribute::constructAlignmentFromInt(align));
1407 /*--.. Operations on basic blocks ..........................................--*/
1409 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1410 return wrap(static_cast<Value*>(unwrap(BB)));
1413 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1414 return isa<BasicBlock>(unwrap(Val));
1417 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1418 return wrap(unwrap<BasicBlock>(Val));
1421 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1422 return wrap(unwrap(BB)->getParent());
1425 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1426 return wrap(unwrap(BB)->getTerminator());
1429 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1430 return unwrap<Function>(FnRef)->size();
1433 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1434 Function *Fn = unwrap<Function>(FnRef);
1435 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1436 *BasicBlocksRefs++ = wrap(I);
1439 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1440 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1443 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1444 Function *Func = unwrap<Function>(Fn);
1445 Function::iterator I = Func->begin();
1446 if (I == Func->end())
1451 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1452 Function *Func = unwrap<Function>(Fn);
1453 Function::iterator I = Func->end();
1454 if (I == Func->begin())
1459 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1460 BasicBlock *Block = unwrap(BB);
1461 Function::iterator I = Block;
1462 if (++I == Block->getParent()->end())
1467 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1468 BasicBlock *Block = unwrap(BB);
1469 Function::iterator I = Block;
1470 if (I == Block->getParent()->begin())
1475 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1478 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1481 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1482 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1485 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1486 LLVMBasicBlockRef BBRef,
1488 BasicBlock *BB = unwrap(BBRef);
1489 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1492 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1494 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1497 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1498 unwrap(BBRef)->eraseFromParent();
1501 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1502 unwrap(BBRef)->removeFromParent();
1505 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1506 unwrap(BB)->moveBefore(unwrap(MovePos));
1509 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1510 unwrap(BB)->moveAfter(unwrap(MovePos));
1513 /*--.. Operations on instructions ..........................................--*/
1515 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1516 return wrap(unwrap<Instruction>(Inst)->getParent());
1519 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1520 BasicBlock *Block = unwrap(BB);
1521 BasicBlock::iterator I = Block->begin();
1522 if (I == Block->end())
1527 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1528 BasicBlock *Block = unwrap(BB);
1529 BasicBlock::iterator I = Block->end();
1530 if (I == Block->begin())
1535 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1536 Instruction *Instr = unwrap<Instruction>(Inst);
1537 BasicBlock::iterator I = Instr;
1538 if (++I == Instr->getParent()->end())
1543 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1544 Instruction *Instr = unwrap<Instruction>(Inst);
1545 BasicBlock::iterator I = Instr;
1546 if (I == Instr->getParent()->begin())
1551 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1552 unwrap<Instruction>(Inst)->eraseFromParent();
1555 /*--.. Call and invoke instructions ........................................--*/
1557 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1558 Value *V = unwrap(Instr);
1559 if (CallInst *CI = dyn_cast<CallInst>(V))
1560 return CI->getCallingConv();
1561 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1562 return II->getCallingConv();
1563 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1567 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1568 Value *V = unwrap(Instr);
1569 if (CallInst *CI = dyn_cast<CallInst>(V))
1570 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1571 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1572 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1573 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1576 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1578 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1580 Call.getAttributes().addAttr(index, PA));
1583 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1585 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1587 Call.getAttributes().removeAttr(index, PA));
1590 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1592 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1594 Call.getAttributes().addAttr(index,
1595 Attribute::constructAlignmentFromInt(align)));
1598 /*--.. Operations on call instructions (only) ..............................--*/
1600 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1601 return unwrap<CallInst>(Call)->isTailCall();
1604 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1605 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1608 /*--.. Operations on switch instructions (only) ............................--*/
1610 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1611 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1614 /*--.. Operations on phi nodes .............................................--*/
1616 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1617 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1618 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1619 for (unsigned I = 0; I != Count; ++I)
1620 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1623 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1624 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1627 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1628 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1631 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1632 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1636 /*===-- Instruction builders ----------------------------------------------===*/
1638 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1639 return wrap(new IRBuilder<>(*unwrap(C)));
1642 LLVMBuilderRef LLVMCreateBuilder(void) {
1643 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1646 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1647 LLVMValueRef Instr) {
1648 BasicBlock *BB = unwrap(Block);
1649 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1650 unwrap(Builder)->SetInsertPoint(BB, I);
1653 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1654 Instruction *I = unwrap<Instruction>(Instr);
1655 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1658 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1659 BasicBlock *BB = unwrap(Block);
1660 unwrap(Builder)->SetInsertPoint(BB);
1663 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1664 return wrap(unwrap(Builder)->GetInsertBlock());
1667 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1668 unwrap(Builder)->ClearInsertionPoint();
1671 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1672 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1675 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1677 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1680 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1681 delete unwrap(Builder);
1684 /*--.. Metadata builders ...................................................--*/
1686 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1687 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1688 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1691 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1692 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1693 .getAsMDNode(unwrap(Builder)->getContext()));
1696 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1697 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1701 /*--.. Instruction builders ................................................--*/
1703 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1704 return wrap(unwrap(B)->CreateRetVoid());
1707 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1708 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1711 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1713 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1716 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1717 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1720 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1721 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1722 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1725 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1726 LLVMBasicBlockRef Else, unsigned NumCases) {
1727 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1730 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1731 unsigned NumDests) {
1732 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1735 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1736 LLVMValueRef *Args, unsigned NumArgs,
1737 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1739 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1740 makeArrayRef(unwrap(Args), NumArgs),
1744 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1745 LLVMValueRef PersFn, unsigned NumClauses,
1747 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1748 cast<Function>(unwrap(PersFn)),
1752 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1753 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1756 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1757 return wrap(unwrap(B)->CreateUnreachable());
1760 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1761 LLVMBasicBlockRef Dest) {
1762 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1765 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1766 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1769 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1770 unwrap<LandingPadInst>(LandingPad)->
1771 addClause(cast<Constant>(unwrap(ClauseVal)));
1774 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1775 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1778 /*--.. Arithmetic ..........................................................--*/
1780 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1782 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1785 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1787 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1790 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1792 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1795 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1797 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1800 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1802 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1805 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1807 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1810 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1812 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1815 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1817 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1820 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1822 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1825 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1827 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1830 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1832 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1835 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1837 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1840 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1842 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1845 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1847 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1850 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1851 LLVMValueRef RHS, const char *Name) {
1852 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1855 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1857 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1860 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1862 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1865 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1867 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1870 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1872 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1875 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1877 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1880 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1882 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1885 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1887 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1890 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1892 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1895 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1897 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1900 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1902 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1905 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1906 LLVMValueRef LHS, LLVMValueRef RHS,
1908 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1909 unwrap(RHS), Name));
1912 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1913 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1916 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1918 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1921 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1923 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1926 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1927 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1930 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1931 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1934 /*--.. Memory ..............................................................--*/
1936 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1938 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1939 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1940 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1941 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1942 ITy, unwrap(Ty), AllocSize,
1944 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1947 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1948 LLVMValueRef Val, const char *Name) {
1949 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1950 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1951 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1952 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1953 ITy, unwrap(Ty), AllocSize,
1954 unwrap(Val), 0, "");
1955 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1958 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1960 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1963 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1964 LLVMValueRef Val, const char *Name) {
1965 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1968 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1969 return wrap(unwrap(B)->Insert(
1970 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1974 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1976 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1979 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1980 LLVMValueRef PointerVal) {
1981 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1984 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1985 LLVMValueRef *Indices, unsigned NumIndices,
1987 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1988 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
1991 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1992 LLVMValueRef *Indices, unsigned NumIndices,
1994 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1995 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
1998 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1999 unsigned Idx, const char *Name) {
2000 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2003 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2005 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2008 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2010 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2013 /*--.. Casts ...............................................................--*/
2015 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2016 LLVMTypeRef DestTy, const char *Name) {
2017 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2020 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2021 LLVMTypeRef DestTy, const char *Name) {
2022 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2025 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2026 LLVMTypeRef DestTy, const char *Name) {
2027 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2030 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2031 LLVMTypeRef DestTy, const char *Name) {
2032 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2035 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2036 LLVMTypeRef DestTy, const char *Name) {
2037 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2040 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2041 LLVMTypeRef DestTy, const char *Name) {
2042 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2045 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2046 LLVMTypeRef DestTy, const char *Name) {
2047 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2050 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2051 LLVMTypeRef DestTy, const char *Name) {
2052 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2055 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2056 LLVMTypeRef DestTy, const char *Name) {
2057 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2060 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2061 LLVMTypeRef DestTy, const char *Name) {
2062 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2065 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2066 LLVMTypeRef DestTy, const char *Name) {
2067 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2070 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2071 LLVMTypeRef DestTy, const char *Name) {
2072 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2075 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2076 LLVMTypeRef DestTy, const char *Name) {
2077 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2081 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2082 LLVMTypeRef DestTy, const char *Name) {
2083 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2087 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2088 LLVMTypeRef DestTy, const char *Name) {
2089 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2093 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2094 LLVMTypeRef DestTy, const char *Name) {
2095 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2096 unwrap(DestTy), Name));
2099 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2100 LLVMTypeRef DestTy, const char *Name) {
2101 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2104 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2105 LLVMTypeRef DestTy, const char *Name) {
2106 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2107 /*isSigned*/true, Name));
2110 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2111 LLVMTypeRef DestTy, const char *Name) {
2112 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2115 /*--.. Comparisons .........................................................--*/
2117 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2118 LLVMValueRef LHS, LLVMValueRef RHS,
2120 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2121 unwrap(LHS), unwrap(RHS), Name));
2124 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2125 LLVMValueRef LHS, LLVMValueRef RHS,
2127 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2128 unwrap(LHS), unwrap(RHS), Name));
2131 /*--.. Miscellaneous instructions ..........................................--*/
2133 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2134 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2137 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2138 LLVMValueRef *Args, unsigned NumArgs,
2140 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2141 makeArrayRef(unwrap(Args), NumArgs),
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) {