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 return wrap(unwrap<User>(Val)->getOperand(Index));
462 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
463 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
466 int LLVMGetNumOperands(LLVMValueRef Val) {
467 return unwrap<User>(Val)->getNumOperands();
470 /*--.. Operations on constants of any type .................................--*/
472 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
473 return wrap(Constant::getNullValue(unwrap(Ty)));
476 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
477 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
480 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
481 return wrap(UndefValue::get(unwrap(Ty)));
484 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
485 return isa<Constant>(unwrap(Ty));
488 LLVMBool LLVMIsNull(LLVMValueRef Val) {
489 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
490 return C->isNullValue();
494 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
495 return isa<UndefValue>(unwrap(Val));
498 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
500 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
503 /*--.. Operations on metadata nodes ........................................--*/
505 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
507 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
510 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
511 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
514 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
516 return wrap(MDNode::get(*unwrap(C),
517 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
520 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
521 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
524 /*--.. Operations on scalar constants ......................................--*/
526 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
527 LLVMBool SignExtend) {
528 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
531 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
533 const uint64_t Words[]) {
534 IntegerType *Ty = unwrap<IntegerType>(IntTy);
535 return wrap(ConstantInt::get(Ty->getContext(),
536 APInt(Ty->getBitWidth(),
537 makeArrayRef(Words, NumWords))));
540 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
542 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
546 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
547 unsigned SLen, uint8_t Radix) {
548 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
552 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
553 return wrap(ConstantFP::get(unwrap(RealTy), N));
556 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
557 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
560 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
562 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
565 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
566 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
569 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
570 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
573 /*--.. Operations on composite constants ...................................--*/
575 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
577 LLVMBool DontNullTerminate) {
578 /* Inverted the sense of AddNull because ', 0)' is a
579 better mnemonic for null termination than ', 1)'. */
580 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
581 DontNullTerminate == 0));
583 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
584 LLVMValueRef *ConstantVals,
585 unsigned Count, LLVMBool Packed) {
586 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
587 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
591 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
592 LLVMBool DontNullTerminate) {
593 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
596 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
597 LLVMValueRef *ConstantVals, unsigned Length) {
598 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
599 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
601 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
603 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
607 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
608 LLVMValueRef *ConstantVals,
610 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
611 StructType *Ty = cast<StructType>(unwrap(StructTy));
613 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
616 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
617 return wrap(ConstantVector::get(makeArrayRef(
618 unwrap<Constant>(ScalarConstantVals, Size), Size)));
620 /*--.. Constant expressions ................................................--*/
622 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
623 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
626 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
627 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
630 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
631 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
634 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
635 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
638 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
639 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
642 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
643 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
647 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
648 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
651 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
652 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
655 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
656 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
657 unwrap<Constant>(RHSConstant)));
660 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
661 LLVMValueRef RHSConstant) {
662 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
663 unwrap<Constant>(RHSConstant)));
666 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
667 LLVMValueRef RHSConstant) {
668 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
669 unwrap<Constant>(RHSConstant)));
672 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
673 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
674 unwrap<Constant>(RHSConstant)));
677 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
678 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
679 unwrap<Constant>(RHSConstant)));
682 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
683 LLVMValueRef RHSConstant) {
684 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
685 unwrap<Constant>(RHSConstant)));
688 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
689 LLVMValueRef RHSConstant) {
690 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
691 unwrap<Constant>(RHSConstant)));
694 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
695 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
696 unwrap<Constant>(RHSConstant)));
699 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
700 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
701 unwrap<Constant>(RHSConstant)));
704 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
705 LLVMValueRef RHSConstant) {
706 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
707 unwrap<Constant>(RHSConstant)));
710 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
711 LLVMValueRef RHSConstant) {
712 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
713 unwrap<Constant>(RHSConstant)));
716 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
717 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
718 unwrap<Constant>(RHSConstant)));
721 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
722 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
723 unwrap<Constant>(RHSConstant)));
726 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
728 unwrap<Constant>(RHSConstant)));
731 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
732 LLVMValueRef RHSConstant) {
733 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
734 unwrap<Constant>(RHSConstant)));
737 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
738 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
739 unwrap<Constant>(RHSConstant)));
742 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
743 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
744 unwrap<Constant>(RHSConstant)));
747 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
748 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
749 unwrap<Constant>(RHSConstant)));
752 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
753 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
754 unwrap<Constant>(RHSConstant)));
757 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
759 unwrap<Constant>(RHSConstant)));
762 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
763 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
764 unwrap<Constant>(RHSConstant)));
767 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
768 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
769 unwrap<Constant>(RHSConstant)));
772 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
773 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
774 return wrap(ConstantExpr::getICmp(Predicate,
775 unwrap<Constant>(LHSConstant),
776 unwrap<Constant>(RHSConstant)));
779 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
780 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
781 return wrap(ConstantExpr::getFCmp(Predicate,
782 unwrap<Constant>(LHSConstant),
783 unwrap<Constant>(RHSConstant)));
786 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
787 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
788 unwrap<Constant>(RHSConstant)));
791 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
792 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
793 unwrap<Constant>(RHSConstant)));
796 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
797 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
798 unwrap<Constant>(RHSConstant)));
801 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
802 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
803 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
805 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
809 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
810 LLVMValueRef *ConstantIndices,
811 unsigned NumIndices) {
812 Constant* Val = unwrap<Constant>(ConstantVal);
813 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
815 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
818 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
819 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
823 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
824 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
828 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
829 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
833 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
834 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
838 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
839 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
843 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
844 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
848 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
849 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
853 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
854 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
858 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
859 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
863 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
868 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
869 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
873 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
874 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
878 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
879 LLVMTypeRef ToType) {
880 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
884 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
885 LLVMTypeRef ToType) {
886 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
890 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
891 LLVMTypeRef ToType) {
892 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
896 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
897 LLVMTypeRef ToType) {
898 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
902 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
904 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
905 unwrap(ToType), isSigned));
908 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
909 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
913 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
914 LLVMValueRef ConstantIfTrue,
915 LLVMValueRef ConstantIfFalse) {
916 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
917 unwrap<Constant>(ConstantIfTrue),
918 unwrap<Constant>(ConstantIfFalse)));
921 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
922 LLVMValueRef IndexConstant) {
923 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
924 unwrap<Constant>(IndexConstant)));
927 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
928 LLVMValueRef ElementValueConstant,
929 LLVMValueRef IndexConstant) {
930 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
931 unwrap<Constant>(ElementValueConstant),
932 unwrap<Constant>(IndexConstant)));
935 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
936 LLVMValueRef VectorBConstant,
937 LLVMValueRef MaskConstant) {
938 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
939 unwrap<Constant>(VectorBConstant),
940 unwrap<Constant>(MaskConstant)));
943 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
945 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
946 makeArrayRef(IdxList, NumIdx)));
949 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
950 LLVMValueRef ElementValueConstant,
951 unsigned *IdxList, unsigned NumIdx) {
952 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
953 unwrap<Constant>(ElementValueConstant),
954 makeArrayRef(IdxList, NumIdx)));
957 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
958 const char *Constraints,
959 LLVMBool HasSideEffects,
960 LLVMBool IsAlignStack) {
961 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
962 Constraints, HasSideEffects, IsAlignStack));
965 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
966 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
969 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
971 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
972 return wrap(unwrap<GlobalValue>(Global)->getParent());
975 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
976 return unwrap<GlobalValue>(Global)->isDeclaration();
979 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
980 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
982 assert(false && "Unhandled Linkage Type.");
983 case GlobalValue::ExternalLinkage:
984 return LLVMExternalLinkage;
985 case GlobalValue::AvailableExternallyLinkage:
986 return LLVMAvailableExternallyLinkage;
987 case GlobalValue::LinkOnceAnyLinkage:
988 return LLVMLinkOnceAnyLinkage;
989 case GlobalValue::LinkOnceODRLinkage:
990 return LLVMLinkOnceODRLinkage;
991 case GlobalValue::WeakAnyLinkage:
992 return LLVMWeakAnyLinkage;
993 case GlobalValue::WeakODRLinkage:
994 return LLVMWeakODRLinkage;
995 case GlobalValue::AppendingLinkage:
996 return LLVMAppendingLinkage;
997 case GlobalValue::InternalLinkage:
998 return LLVMInternalLinkage;
999 case GlobalValue::PrivateLinkage:
1000 return LLVMPrivateLinkage;
1001 case GlobalValue::LinkerPrivateLinkage:
1002 return LLVMLinkerPrivateLinkage;
1003 case GlobalValue::LinkerPrivateWeakLinkage:
1004 return LLVMLinkerPrivateWeakLinkage;
1005 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1006 return LLVMLinkerPrivateWeakDefAutoLinkage;
1007 case GlobalValue::DLLImportLinkage:
1008 return LLVMDLLImportLinkage;
1009 case GlobalValue::DLLExportLinkage:
1010 return LLVMDLLExportLinkage;
1011 case GlobalValue::ExternalWeakLinkage:
1012 return LLVMExternalWeakLinkage;
1013 case GlobalValue::CommonLinkage:
1014 return LLVMCommonLinkage;
1017 // Should never get here.
1018 return static_cast<LLVMLinkage>(0);
1021 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1022 GlobalValue *GV = unwrap<GlobalValue>(Global);
1026 assert(false && "Unhandled Linkage Type.");
1027 case LLVMExternalLinkage:
1028 GV->setLinkage(GlobalValue::ExternalLinkage);
1030 case LLVMAvailableExternallyLinkage:
1031 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1033 case LLVMLinkOnceAnyLinkage:
1034 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1036 case LLVMLinkOnceODRLinkage:
1037 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1039 case LLVMWeakAnyLinkage:
1040 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1042 case LLVMWeakODRLinkage:
1043 GV->setLinkage(GlobalValue::WeakODRLinkage);
1045 case LLVMAppendingLinkage:
1046 GV->setLinkage(GlobalValue::AppendingLinkage);
1048 case LLVMInternalLinkage:
1049 GV->setLinkage(GlobalValue::InternalLinkage);
1051 case LLVMPrivateLinkage:
1052 GV->setLinkage(GlobalValue::PrivateLinkage);
1054 case LLVMLinkerPrivateLinkage:
1055 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1057 case LLVMLinkerPrivateWeakLinkage:
1058 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1060 case LLVMLinkerPrivateWeakDefAutoLinkage:
1061 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1063 case LLVMDLLImportLinkage:
1064 GV->setLinkage(GlobalValue::DLLImportLinkage);
1066 case LLVMDLLExportLinkage:
1067 GV->setLinkage(GlobalValue::DLLExportLinkage);
1069 case LLVMExternalWeakLinkage:
1070 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1072 case LLVMGhostLinkage:
1074 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1076 case LLVMCommonLinkage:
1077 GV->setLinkage(GlobalValue::CommonLinkage);
1082 const char *LLVMGetSection(LLVMValueRef Global) {
1083 return unwrap<GlobalValue>(Global)->getSection().c_str();
1086 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1087 unwrap<GlobalValue>(Global)->setSection(Section);
1090 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1091 return static_cast<LLVMVisibility>(
1092 unwrap<GlobalValue>(Global)->getVisibility());
1095 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1096 unwrap<GlobalValue>(Global)
1097 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1100 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1101 return unwrap<GlobalValue>(Global)->getAlignment();
1104 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1105 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1108 /*--.. Operations on global variables ......................................--*/
1110 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1111 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1112 GlobalValue::ExternalLinkage, 0, Name));
1115 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1117 unsigned AddressSpace) {
1118 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1119 GlobalValue::ExternalLinkage, 0, Name, 0,
1120 false, AddressSpace));
1123 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1124 return wrap(unwrap(M)->getNamedGlobal(Name));
1127 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1128 Module *Mod = unwrap(M);
1129 Module::global_iterator I = Mod->global_begin();
1130 if (I == Mod->global_end())
1135 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1136 Module *Mod = unwrap(M);
1137 Module::global_iterator I = Mod->global_end();
1138 if (I == Mod->global_begin())
1143 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1144 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1145 Module::global_iterator I = GV;
1146 if (++I == GV->getParent()->global_end())
1151 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1152 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1153 Module::global_iterator I = GV;
1154 if (I == GV->getParent()->global_begin())
1159 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1160 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1163 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1164 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1165 if ( !GV->hasInitializer() )
1167 return wrap(GV->getInitializer());
1170 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1171 unwrap<GlobalVariable>(GlobalVar)
1172 ->setInitializer(unwrap<Constant>(ConstantVal));
1175 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1176 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1179 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1180 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1183 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1184 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1187 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1188 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1191 /*--.. Operations on aliases ......................................--*/
1193 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1195 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1196 unwrap<Constant>(Aliasee), unwrap (M)));
1199 /*--.. Operations on functions .............................................--*/
1201 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1202 LLVMTypeRef FunctionTy) {
1203 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1204 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1207 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1208 return wrap(unwrap(M)->getFunction(Name));
1211 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1212 Module *Mod = unwrap(M);
1213 Module::iterator I = Mod->begin();
1214 if (I == Mod->end())
1219 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1220 Module *Mod = unwrap(M);
1221 Module::iterator I = Mod->end();
1222 if (I == Mod->begin())
1227 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1228 Function *Func = unwrap<Function>(Fn);
1229 Module::iterator I = Func;
1230 if (++I == Func->getParent()->end())
1235 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1236 Function *Func = unwrap<Function>(Fn);
1237 Module::iterator I = Func;
1238 if (I == Func->getParent()->begin())
1243 void LLVMDeleteFunction(LLVMValueRef Fn) {
1244 unwrap<Function>(Fn)->eraseFromParent();
1247 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1248 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1249 return F->getIntrinsicID();
1253 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1254 return unwrap<Function>(Fn)->getCallingConv();
1257 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1258 return unwrap<Function>(Fn)->setCallingConv(
1259 static_cast<CallingConv::ID>(CC));
1262 const char *LLVMGetGC(LLVMValueRef Fn) {
1263 Function *F = unwrap<Function>(Fn);
1264 return F->hasGC()? F->getGC() : 0;
1267 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1268 Function *F = unwrap<Function>(Fn);
1275 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1276 Function *Func = unwrap<Function>(Fn);
1277 const AttrListPtr PAL = Func->getAttributes();
1278 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1279 Func->setAttributes(PALnew);
1282 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1283 Function *Func = unwrap<Function>(Fn);
1284 const AttrListPtr PAL = Func->getAttributes();
1285 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1286 Func->setAttributes(PALnew);
1289 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1290 Function *Func = unwrap<Function>(Fn);
1291 const AttrListPtr PAL = Func->getAttributes();
1292 Attributes attr = PAL.getFnAttributes();
1293 return (LLVMAttribute)attr;
1296 /*--.. Operations on parameters ............................................--*/
1298 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1299 // This function is strictly redundant to
1300 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1301 return unwrap<Function>(FnRef)->arg_size();
1304 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1305 Function *Fn = unwrap<Function>(FnRef);
1306 for (Function::arg_iterator I = Fn->arg_begin(),
1307 E = Fn->arg_end(); I != E; I++)
1308 *ParamRefs++ = wrap(I);
1311 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1312 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1318 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1319 return wrap(unwrap<Argument>(V)->getParent());
1322 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1323 Function *Func = unwrap<Function>(Fn);
1324 Function::arg_iterator I = Func->arg_begin();
1325 if (I == Func->arg_end())
1330 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1331 Function *Func = unwrap<Function>(Fn);
1332 Function::arg_iterator I = Func->arg_end();
1333 if (I == Func->arg_begin())
1338 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1339 Argument *A = unwrap<Argument>(Arg);
1340 Function::arg_iterator I = A;
1341 if (++I == A->getParent()->arg_end())
1346 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1347 Argument *A = unwrap<Argument>(Arg);
1348 Function::arg_iterator I = A;
1349 if (I == A->getParent()->arg_begin())
1354 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1355 unwrap<Argument>(Arg)->addAttr(PA);
1358 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1359 unwrap<Argument>(Arg)->removeAttr(PA);
1362 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1363 Argument *A = unwrap<Argument>(Arg);
1364 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1366 return (LLVMAttribute)attr;
1370 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1371 unwrap<Argument>(Arg)->addAttr(
1372 Attribute::constructAlignmentFromInt(align));
1375 /*--.. Operations on basic blocks ..........................................--*/
1377 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1378 return wrap(static_cast<Value*>(unwrap(BB)));
1381 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1382 return isa<BasicBlock>(unwrap(Val));
1385 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1386 return wrap(unwrap<BasicBlock>(Val));
1389 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1390 return wrap(unwrap(BB)->getParent());
1393 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1394 return wrap(unwrap(BB)->getTerminator());
1397 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1398 return unwrap<Function>(FnRef)->size();
1401 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1402 Function *Fn = unwrap<Function>(FnRef);
1403 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1404 *BasicBlocksRefs++ = wrap(I);
1407 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1408 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1411 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1412 Function *Func = unwrap<Function>(Fn);
1413 Function::iterator I = Func->begin();
1414 if (I == Func->end())
1419 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1420 Function *Func = unwrap<Function>(Fn);
1421 Function::iterator I = Func->end();
1422 if (I == Func->begin())
1427 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1428 BasicBlock *Block = unwrap(BB);
1429 Function::iterator I = Block;
1430 if (++I == Block->getParent()->end())
1435 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1436 BasicBlock *Block = unwrap(BB);
1437 Function::iterator I = Block;
1438 if (I == Block->getParent()->begin())
1443 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1446 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1449 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1450 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1453 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1454 LLVMBasicBlockRef BBRef,
1456 BasicBlock *BB = unwrap(BBRef);
1457 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1460 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1462 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1465 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1466 unwrap(BBRef)->eraseFromParent();
1469 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1470 unwrap(BBRef)->removeFromParent();
1473 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1474 unwrap(BB)->moveBefore(unwrap(MovePos));
1477 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1478 unwrap(BB)->moveAfter(unwrap(MovePos));
1481 /*--.. Operations on instructions ..........................................--*/
1483 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1484 return wrap(unwrap<Instruction>(Inst)->getParent());
1487 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1488 BasicBlock *Block = unwrap(BB);
1489 BasicBlock::iterator I = Block->begin();
1490 if (I == Block->end())
1495 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1496 BasicBlock *Block = unwrap(BB);
1497 BasicBlock::iterator I = Block->end();
1498 if (I == Block->begin())
1503 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1504 Instruction *Instr = unwrap<Instruction>(Inst);
1505 BasicBlock::iterator I = Instr;
1506 if (++I == Instr->getParent()->end())
1511 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1512 Instruction *Instr = unwrap<Instruction>(Inst);
1513 BasicBlock::iterator I = Instr;
1514 if (I == Instr->getParent()->begin())
1519 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1520 unwrap<Instruction>(Inst)->eraseFromParent();
1523 /*--.. Call and invoke instructions ........................................--*/
1525 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1526 Value *V = unwrap(Instr);
1527 if (CallInst *CI = dyn_cast<CallInst>(V))
1528 return CI->getCallingConv();
1529 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1530 return II->getCallingConv();
1531 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1535 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1536 Value *V = unwrap(Instr);
1537 if (CallInst *CI = dyn_cast<CallInst>(V))
1538 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1539 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1540 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1541 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1544 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1546 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1548 Call.getAttributes().addAttr(index, PA));
1551 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1553 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1555 Call.getAttributes().removeAttr(index, PA));
1558 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1560 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1562 Call.getAttributes().addAttr(index,
1563 Attribute::constructAlignmentFromInt(align)));
1566 /*--.. Operations on call instructions (only) ..............................--*/
1568 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1569 return unwrap<CallInst>(Call)->isTailCall();
1572 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1573 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1576 /*--.. Operations on switch instructions (only) ............................--*/
1578 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1579 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1582 /*--.. Operations on phi nodes .............................................--*/
1584 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1585 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1586 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1587 for (unsigned I = 0; I != Count; ++I)
1588 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1591 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1592 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1595 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1596 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1599 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1600 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1604 /*===-- Instruction builders ----------------------------------------------===*/
1606 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1607 return wrap(new IRBuilder<>(*unwrap(C)));
1610 LLVMBuilderRef LLVMCreateBuilder(void) {
1611 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1614 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1615 LLVMValueRef Instr) {
1616 BasicBlock *BB = unwrap(Block);
1617 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1618 unwrap(Builder)->SetInsertPoint(BB, I);
1621 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1622 Instruction *I = unwrap<Instruction>(Instr);
1623 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1626 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1627 BasicBlock *BB = unwrap(Block);
1628 unwrap(Builder)->SetInsertPoint(BB);
1631 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1632 return wrap(unwrap(Builder)->GetInsertBlock());
1635 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1636 unwrap(Builder)->ClearInsertionPoint();
1639 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1640 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1643 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1645 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1648 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1649 delete unwrap(Builder);
1652 /*--.. Metadata builders ...................................................--*/
1654 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1655 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1656 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1659 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1660 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1661 .getAsMDNode(unwrap(Builder)->getContext()));
1664 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1665 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1669 /*--.. Instruction builders ................................................--*/
1671 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1672 return wrap(unwrap(B)->CreateRetVoid());
1675 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1676 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1679 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1681 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1684 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1685 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1688 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1689 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1690 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1693 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1694 LLVMBasicBlockRef Else, unsigned NumCases) {
1695 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1698 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1699 unsigned NumDests) {
1700 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1703 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1704 LLVMValueRef *Args, unsigned NumArgs,
1705 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1707 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1708 makeArrayRef(unwrap(Args), NumArgs),
1712 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1713 LLVMValueRef PersFn, unsigned NumClauses,
1715 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1716 cast<Function>(unwrap(PersFn)),
1720 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1721 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1724 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1725 return wrap(unwrap(B)->CreateUnreachable());
1728 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1729 LLVMBasicBlockRef Dest) {
1730 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1733 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1734 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1737 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1738 unwrap<LandingPadInst>(LandingPad)->
1739 addClause(cast<Constant>(unwrap(ClauseVal)));
1742 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1743 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1746 /*--.. Arithmetic ..........................................................--*/
1748 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1750 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1753 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1755 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1758 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1760 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1763 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1765 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1768 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1770 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1773 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1775 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1778 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1780 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1783 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1785 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1788 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1790 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1793 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1795 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1798 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1800 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1803 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1805 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1808 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1810 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1813 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1815 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1818 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1819 LLVMValueRef RHS, const char *Name) {
1820 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1823 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1825 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1828 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1830 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1833 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1835 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1838 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1840 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1843 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1845 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1848 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1850 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1853 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1855 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1858 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1860 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1863 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1865 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1868 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1870 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1873 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1874 LLVMValueRef LHS, LLVMValueRef RHS,
1876 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1877 unwrap(RHS), Name));
1880 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1881 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1884 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1886 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1889 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1891 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1894 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1895 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1898 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1899 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1902 /*--.. Memory ..............................................................--*/
1904 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1906 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1907 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1908 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1909 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1910 ITy, unwrap(Ty), AllocSize,
1912 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1915 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1916 LLVMValueRef Val, const char *Name) {
1917 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1918 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1919 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1920 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1921 ITy, unwrap(Ty), AllocSize,
1922 unwrap(Val), 0, "");
1923 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1926 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1928 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1931 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1932 LLVMValueRef Val, const char *Name) {
1933 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1936 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1937 return wrap(unwrap(B)->Insert(
1938 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1942 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1944 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1947 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1948 LLVMValueRef PointerVal) {
1949 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1952 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1953 LLVMValueRef *Indices, unsigned NumIndices,
1955 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1956 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
1959 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1960 LLVMValueRef *Indices, unsigned NumIndices,
1962 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1963 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
1966 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1967 unsigned Idx, const char *Name) {
1968 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1971 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1973 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1976 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1978 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1981 /*--.. Casts ...............................................................--*/
1983 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1984 LLVMTypeRef DestTy, const char *Name) {
1985 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1988 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1989 LLVMTypeRef DestTy, const char *Name) {
1990 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1993 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1994 LLVMTypeRef DestTy, const char *Name) {
1995 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1998 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1999 LLVMTypeRef DestTy, const char *Name) {
2000 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2003 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2004 LLVMTypeRef DestTy, const char *Name) {
2005 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2008 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2009 LLVMTypeRef DestTy, const char *Name) {
2010 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2013 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2014 LLVMTypeRef DestTy, const char *Name) {
2015 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2018 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2019 LLVMTypeRef DestTy, const char *Name) {
2020 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2023 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2024 LLVMTypeRef DestTy, const char *Name) {
2025 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2028 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2029 LLVMTypeRef DestTy, const char *Name) {
2030 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2033 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2034 LLVMTypeRef DestTy, const char *Name) {
2035 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2038 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2039 LLVMTypeRef DestTy, const char *Name) {
2040 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2043 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2044 LLVMTypeRef DestTy, const char *Name) {
2045 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2049 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2050 LLVMTypeRef DestTy, const char *Name) {
2051 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2055 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2056 LLVMTypeRef DestTy, const char *Name) {
2057 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2061 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2062 LLVMTypeRef DestTy, const char *Name) {
2063 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2064 unwrap(DestTy), Name));
2067 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2068 LLVMTypeRef DestTy, const char *Name) {
2069 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2072 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2073 LLVMTypeRef DestTy, const char *Name) {
2074 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2075 /*isSigned*/true, Name));
2078 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2079 LLVMTypeRef DestTy, const char *Name) {
2080 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2083 /*--.. Comparisons .........................................................--*/
2085 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2086 LLVMValueRef LHS, LLVMValueRef RHS,
2088 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2089 unwrap(LHS), unwrap(RHS), Name));
2092 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2093 LLVMValueRef LHS, LLVMValueRef RHS,
2095 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2096 unwrap(LHS), unwrap(RHS), Name));
2099 /*--.. Miscellaneous instructions ..........................................--*/
2101 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2102 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2105 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2106 LLVMValueRef *Args, unsigned NumArgs,
2108 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2109 makeArrayRef(unwrap(Args), NumArgs),
2113 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2114 LLVMValueRef Then, LLVMValueRef Else,
2116 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2120 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2121 LLVMTypeRef Ty, const char *Name) {
2122 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2125 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2126 LLVMValueRef Index, const char *Name) {
2127 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2131 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2132 LLVMValueRef EltVal, LLVMValueRef Index,
2134 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2135 unwrap(Index), Name));
2138 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2139 LLVMValueRef V2, LLVMValueRef Mask,
2141 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2142 unwrap(Mask), Name));
2145 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2146 unsigned Index, const char *Name) {
2147 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2150 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2151 LLVMValueRef EltVal, unsigned Index,
2153 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2157 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2159 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2162 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2164 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2167 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2168 LLVMValueRef RHS, const char *Name) {
2169 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2173 /*===-- Module providers --------------------------------------------------===*/
2175 LLVMModuleProviderRef
2176 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2177 return reinterpret_cast<LLVMModuleProviderRef>(M);
2180 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2185 /*===-- Memory buffers ----------------------------------------------------===*/
2187 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2189 LLVMMemoryBufferRef *OutMemBuf,
2190 char **OutMessage) {
2192 OwningPtr<MemoryBuffer> MB;
2194 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2195 *OutMemBuf = wrap(MB.take());
2199 *OutMessage = strdup(ec.message().c_str());
2203 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2204 char **OutMessage) {
2205 OwningPtr<MemoryBuffer> MB;
2207 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2208 *OutMemBuf = wrap(MB.take());
2212 *OutMessage = strdup(ec.message().c_str());
2216 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2217 delete unwrap(MemBuf);
2220 /*===-- Pass Registry -----------------------------------------------------===*/
2222 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2223 return wrap(PassRegistry::getPassRegistry());
2226 /*===-- Pass Manager ------------------------------------------------------===*/
2228 LLVMPassManagerRef LLVMCreatePassManager() {
2229 return wrap(new PassManager());
2232 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2233 return wrap(new FunctionPassManager(unwrap(M)));
2236 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2237 return LLVMCreateFunctionPassManagerForModule(
2238 reinterpret_cast<LLVMModuleRef>(P));
2241 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2242 return unwrap<PassManager>(PM)->run(*unwrap(M));
2245 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2246 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2249 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2250 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2253 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2254 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2257 void LLVMDisposePassManager(LLVMPassManagerRef PM) {