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 std::vector<Type*> Tys;
264 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
265 Tys.push_back(unwrap(*I));
267 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
270 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
271 return unwrap<FunctionType>(FunctionTy)->isVarArg();
274 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
275 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
278 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
279 return unwrap<FunctionType>(FunctionTy)->getNumParams();
282 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
283 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
284 for (FunctionType::param_iterator I = Ty->param_begin(),
285 E = Ty->param_end(); I != E; ++I)
289 /*--.. Operations on struct types ..........................................--*/
291 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
292 unsigned ElementCount, LLVMBool Packed) {
293 std::vector<Type*> Tys;
294 for (LLVMTypeRef *I = ElementTypes,
295 *E = ElementTypes + ElementCount; I != E; ++I)
296 Tys.push_back(unwrap(*I));
298 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
301 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
302 unsigned ElementCount, LLVMBool Packed) {
303 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
304 ElementCount, Packed);
307 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
309 return wrap(StructType::createNamed(*unwrap(C), Name));
312 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
313 unsigned ElementCount, LLVMBool Packed) {
314 std::vector<Type*> Tys;
315 for (LLVMTypeRef *I = ElementTypes,
316 *E = ElementTypes + ElementCount; I != E; ++I)
317 Tys.push_back(unwrap(*I));
318 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
321 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
322 return unwrap<StructType>(StructTy)->getNumElements();
325 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
326 StructType *Ty = unwrap<StructType>(StructTy);
327 for (StructType::element_iterator I = Ty->element_begin(),
328 E = Ty->element_end(); I != E; ++I)
332 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
333 return unwrap<StructType>(StructTy)->isPacked();
336 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
338 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
339 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
342 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
343 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
346 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
347 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
350 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
351 return wrap(unwrap<SequentialType>(Ty)->getElementType());
354 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
355 return unwrap<ArrayType>(ArrayTy)->getNumElements();
358 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
359 return unwrap<PointerType>(PointerTy)->getAddressSpace();
362 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
363 return unwrap<VectorType>(VectorTy)->getNumElements();
366 /*--.. Operations on other types ...........................................--*/
368 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
369 return wrap(Type::getVoidTy(*unwrap(C)));
371 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
372 return wrap(Type::getLabelTy(*unwrap(C)));
375 LLVMTypeRef LLVMVoidType(void) {
376 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
378 LLVMTypeRef LLVMLabelType(void) {
379 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
382 /*===-- Operations on values ----------------------------------------------===*/
384 /*--.. Operations on all values ............................................--*/
386 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
387 return wrap(unwrap(Val)->getType());
390 const char *LLVMGetValueName(LLVMValueRef Val) {
391 return unwrap(Val)->getName().data();
394 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
395 unwrap(Val)->setName(Name);
398 void LLVMDumpValue(LLVMValueRef Val) {
402 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
403 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
406 int LLVMHasMetadata(LLVMValueRef Inst) {
407 return unwrap<Instruction>(Inst)->hasMetadata();
410 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
411 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
414 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
415 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
418 /*--.. Conversion functions ................................................--*/
420 #define LLVM_DEFINE_VALUE_CAST(name) \
421 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
422 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
425 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
427 /*--.. Operations on Uses ..................................................--*/
428 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
429 Value *V = unwrap(Val);
430 Value::use_iterator I = V->use_begin();
431 if (I == V->use_end())
433 return wrap(&(I.getUse()));
436 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
437 Use *Next = unwrap(U)->getNext();
443 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
444 return wrap(unwrap(U)->getUser());
447 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
448 return wrap(unwrap(U)->get());
451 /*--.. Operations on Users .................................................--*/
452 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
453 return wrap(unwrap<User>(Val)->getOperand(Index));
456 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
457 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
460 int LLVMGetNumOperands(LLVMValueRef Val) {
461 return unwrap<User>(Val)->getNumOperands();
464 /*--.. Operations on constants of any type .................................--*/
466 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
467 return wrap(Constant::getNullValue(unwrap(Ty)));
470 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
471 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
474 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
475 return wrap(UndefValue::get(unwrap(Ty)));
478 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
479 return isa<Constant>(unwrap(Ty));
482 LLVMBool LLVMIsNull(LLVMValueRef Val) {
483 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
484 return C->isNullValue();
488 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
489 return isa<UndefValue>(unwrap(Val));
492 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
494 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
497 /*--.. Operations on metadata nodes ........................................--*/
499 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
501 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
504 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
505 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
508 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
510 return wrap(MDNode::get(*unwrap(C),
511 ArrayRef<Value*>(unwrap<Value>(Vals, Count), Count)));
514 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
515 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
518 /*--.. Operations on scalar constants ......................................--*/
520 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
521 LLVMBool SignExtend) {
522 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
525 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
527 const uint64_t Words[]) {
528 IntegerType *Ty = unwrap<IntegerType>(IntTy);
529 return wrap(ConstantInt::get(Ty->getContext(),
530 APInt(Ty->getBitWidth(), NumWords, Words)));
533 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
535 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
539 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
540 unsigned SLen, uint8_t Radix) {
541 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
545 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
546 return wrap(ConstantFP::get(unwrap(RealTy), N));
549 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
550 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
553 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
555 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
558 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
559 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
562 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
563 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
566 /*--.. Operations on composite constants ...................................--*/
568 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
570 LLVMBool DontNullTerminate) {
571 /* Inverted the sense of AddNull because ', 0)' is a
572 better mnemonic for null termination than ', 1)'. */
573 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
574 DontNullTerminate == 0));
576 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
577 LLVMValueRef *ConstantVals,
578 unsigned Count, LLVMBool Packed) {
579 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
580 return wrap(ConstantStruct::getAnon(*unwrap(C),
581 ArrayRef<Constant*>(Elements, Count),
585 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
586 LLVMBool DontNullTerminate) {
587 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
590 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
591 LLVMValueRef *ConstantVals, unsigned Length) {
592 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
593 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
595 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
597 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
600 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
601 return wrap(ConstantVector::get(ArrayRef<Constant*>(
602 unwrap<Constant>(ScalarConstantVals, Size), Size)));
604 /*--.. Constant expressions ................................................--*/
606 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
607 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
610 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
611 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
614 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
615 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
618 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
619 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
622 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
623 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
626 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
627 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
631 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
632 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
635 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
636 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
639 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
640 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
641 unwrap<Constant>(RHSConstant)));
644 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
645 LLVMValueRef RHSConstant) {
646 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
647 unwrap<Constant>(RHSConstant)));
650 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
651 LLVMValueRef RHSConstant) {
652 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
653 unwrap<Constant>(RHSConstant)));
656 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
657 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
658 unwrap<Constant>(RHSConstant)));
661 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
662 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
663 unwrap<Constant>(RHSConstant)));
666 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
667 LLVMValueRef RHSConstant) {
668 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
669 unwrap<Constant>(RHSConstant)));
672 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
673 LLVMValueRef RHSConstant) {
674 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
675 unwrap<Constant>(RHSConstant)));
678 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
679 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
680 unwrap<Constant>(RHSConstant)));
683 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
684 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
685 unwrap<Constant>(RHSConstant)));
688 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
689 LLVMValueRef RHSConstant) {
690 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
691 unwrap<Constant>(RHSConstant)));
694 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
695 LLVMValueRef RHSConstant) {
696 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
697 unwrap<Constant>(RHSConstant)));
700 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
701 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
702 unwrap<Constant>(RHSConstant)));
705 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
706 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
707 unwrap<Constant>(RHSConstant)));
710 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
711 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
712 unwrap<Constant>(RHSConstant)));
715 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
716 LLVMValueRef RHSConstant) {
717 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
718 unwrap<Constant>(RHSConstant)));
721 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
722 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
723 unwrap<Constant>(RHSConstant)));
726 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
728 unwrap<Constant>(RHSConstant)));
731 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
732 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
733 unwrap<Constant>(RHSConstant)));
736 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
737 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
738 unwrap<Constant>(RHSConstant)));
741 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
742 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
743 unwrap<Constant>(RHSConstant)));
746 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
747 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
748 unwrap<Constant>(RHSConstant)));
751 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
752 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
753 unwrap<Constant>(RHSConstant)));
756 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
757 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758 return wrap(ConstantExpr::getICmp(Predicate,
759 unwrap<Constant>(LHSConstant),
760 unwrap<Constant>(RHSConstant)));
763 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
764 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
765 return wrap(ConstantExpr::getFCmp(Predicate,
766 unwrap<Constant>(LHSConstant),
767 unwrap<Constant>(RHSConstant)));
770 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
771 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
772 unwrap<Constant>(RHSConstant)));
775 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
776 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
777 unwrap<Constant>(RHSConstant)));
780 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
781 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
782 unwrap<Constant>(RHSConstant)));
785 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
786 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
787 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
788 unwrap<Constant>(ConstantIndices,
793 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
794 LLVMValueRef *ConstantIndices,
795 unsigned NumIndices) {
796 Constant* Val = unwrap<Constant>(ConstantVal);
797 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
798 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
801 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
802 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
806 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
807 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
811 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
812 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
816 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
817 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
821 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
822 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
826 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
827 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
831 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
832 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
836 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
837 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
841 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
842 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
846 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
847 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
851 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
852 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
856 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
857 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
861 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
862 LLVMTypeRef ToType) {
863 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
867 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
868 LLVMTypeRef ToType) {
869 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
873 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
874 LLVMTypeRef ToType) {
875 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
879 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
880 LLVMTypeRef ToType) {
881 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
885 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
887 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
888 unwrap(ToType), isSigned));
891 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
892 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
896 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
897 LLVMValueRef ConstantIfTrue,
898 LLVMValueRef ConstantIfFalse) {
899 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
900 unwrap<Constant>(ConstantIfTrue),
901 unwrap<Constant>(ConstantIfFalse)));
904 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
905 LLVMValueRef IndexConstant) {
906 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
907 unwrap<Constant>(IndexConstant)));
910 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
911 LLVMValueRef ElementValueConstant,
912 LLVMValueRef IndexConstant) {
913 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
914 unwrap<Constant>(ElementValueConstant),
915 unwrap<Constant>(IndexConstant)));
918 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
919 LLVMValueRef VectorBConstant,
920 LLVMValueRef MaskConstant) {
921 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
922 unwrap<Constant>(VectorBConstant),
923 unwrap<Constant>(MaskConstant)));
926 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
928 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
929 ArrayRef<unsigned>(IdxList,
933 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
934 LLVMValueRef ElementValueConstant,
935 unsigned *IdxList, unsigned NumIdx) {
936 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
937 unwrap<Constant>(ElementValueConstant),
938 ArrayRef<unsigned>(IdxList,
942 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
943 const char *Constraints,
944 LLVMBool HasSideEffects,
945 LLVMBool IsAlignStack) {
946 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
947 Constraints, HasSideEffects, IsAlignStack));
950 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
951 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
954 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
956 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
957 return wrap(unwrap<GlobalValue>(Global)->getParent());
960 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
961 return unwrap<GlobalValue>(Global)->isDeclaration();
964 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
965 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
967 assert(false && "Unhandled Linkage Type.");
968 case GlobalValue::ExternalLinkage:
969 return LLVMExternalLinkage;
970 case GlobalValue::AvailableExternallyLinkage:
971 return LLVMAvailableExternallyLinkage;
972 case GlobalValue::LinkOnceAnyLinkage:
973 return LLVMLinkOnceAnyLinkage;
974 case GlobalValue::LinkOnceODRLinkage:
975 return LLVMLinkOnceODRLinkage;
976 case GlobalValue::WeakAnyLinkage:
977 return LLVMWeakAnyLinkage;
978 case GlobalValue::WeakODRLinkage:
979 return LLVMWeakODRLinkage;
980 case GlobalValue::AppendingLinkage:
981 return LLVMAppendingLinkage;
982 case GlobalValue::InternalLinkage:
983 return LLVMInternalLinkage;
984 case GlobalValue::PrivateLinkage:
985 return LLVMPrivateLinkage;
986 case GlobalValue::LinkerPrivateLinkage:
987 return LLVMLinkerPrivateLinkage;
988 case GlobalValue::LinkerPrivateWeakLinkage:
989 return LLVMLinkerPrivateWeakLinkage;
990 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
991 return LLVMLinkerPrivateWeakDefAutoLinkage;
992 case GlobalValue::DLLImportLinkage:
993 return LLVMDLLImportLinkage;
994 case GlobalValue::DLLExportLinkage:
995 return LLVMDLLExportLinkage;
996 case GlobalValue::ExternalWeakLinkage:
997 return LLVMExternalWeakLinkage;
998 case GlobalValue::CommonLinkage:
999 return LLVMCommonLinkage;
1002 // Should never get here.
1003 return static_cast<LLVMLinkage>(0);
1006 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1007 GlobalValue *GV = unwrap<GlobalValue>(Global);
1011 assert(false && "Unhandled Linkage Type.");
1012 case LLVMExternalLinkage:
1013 GV->setLinkage(GlobalValue::ExternalLinkage);
1015 case LLVMAvailableExternallyLinkage:
1016 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1018 case LLVMLinkOnceAnyLinkage:
1019 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1021 case LLVMLinkOnceODRLinkage:
1022 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1024 case LLVMWeakAnyLinkage:
1025 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1027 case LLVMWeakODRLinkage:
1028 GV->setLinkage(GlobalValue::WeakODRLinkage);
1030 case LLVMAppendingLinkage:
1031 GV->setLinkage(GlobalValue::AppendingLinkage);
1033 case LLVMInternalLinkage:
1034 GV->setLinkage(GlobalValue::InternalLinkage);
1036 case LLVMPrivateLinkage:
1037 GV->setLinkage(GlobalValue::PrivateLinkage);
1039 case LLVMLinkerPrivateLinkage:
1040 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1042 case LLVMLinkerPrivateWeakLinkage:
1043 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1045 case LLVMLinkerPrivateWeakDefAutoLinkage:
1046 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1048 case LLVMDLLImportLinkage:
1049 GV->setLinkage(GlobalValue::DLLImportLinkage);
1051 case LLVMDLLExportLinkage:
1052 GV->setLinkage(GlobalValue::DLLExportLinkage);
1054 case LLVMExternalWeakLinkage:
1055 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1057 case LLVMGhostLinkage:
1059 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1061 case LLVMCommonLinkage:
1062 GV->setLinkage(GlobalValue::CommonLinkage);
1067 const char *LLVMGetSection(LLVMValueRef Global) {
1068 return unwrap<GlobalValue>(Global)->getSection().c_str();
1071 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1072 unwrap<GlobalValue>(Global)->setSection(Section);
1075 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1076 return static_cast<LLVMVisibility>(
1077 unwrap<GlobalValue>(Global)->getVisibility());
1080 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1081 unwrap<GlobalValue>(Global)
1082 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1085 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1086 return unwrap<GlobalValue>(Global)->getAlignment();
1089 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1090 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1093 /*--.. Operations on global variables ......................................--*/
1095 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1096 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1097 GlobalValue::ExternalLinkage, 0, Name));
1100 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1102 unsigned AddressSpace) {
1103 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1104 GlobalValue::ExternalLinkage, 0, Name, 0,
1105 false, AddressSpace));
1108 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1109 return wrap(unwrap(M)->getNamedGlobal(Name));
1112 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1113 Module *Mod = unwrap(M);
1114 Module::global_iterator I = Mod->global_begin();
1115 if (I == Mod->global_end())
1120 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1121 Module *Mod = unwrap(M);
1122 Module::global_iterator I = Mod->global_end();
1123 if (I == Mod->global_begin())
1128 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1129 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1130 Module::global_iterator I = GV;
1131 if (++I == GV->getParent()->global_end())
1136 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1137 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1138 Module::global_iterator I = GV;
1139 if (I == GV->getParent()->global_begin())
1144 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1145 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1148 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1149 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1150 if ( !GV->hasInitializer() )
1152 return wrap(GV->getInitializer());
1155 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1156 unwrap<GlobalVariable>(GlobalVar)
1157 ->setInitializer(unwrap<Constant>(ConstantVal));
1160 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1161 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1164 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1165 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1168 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1169 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1172 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1173 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1176 /*--.. Operations on aliases ......................................--*/
1178 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1180 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1181 unwrap<Constant>(Aliasee), unwrap (M)));
1184 /*--.. Operations on functions .............................................--*/
1186 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1187 LLVMTypeRef FunctionTy) {
1188 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1189 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1192 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1193 return wrap(unwrap(M)->getFunction(Name));
1196 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1197 Module *Mod = unwrap(M);
1198 Module::iterator I = Mod->begin();
1199 if (I == Mod->end())
1204 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1205 Module *Mod = unwrap(M);
1206 Module::iterator I = Mod->end();
1207 if (I == Mod->begin())
1212 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1213 Function *Func = unwrap<Function>(Fn);
1214 Module::iterator I = Func;
1215 if (++I == Func->getParent()->end())
1220 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1221 Function *Func = unwrap<Function>(Fn);
1222 Module::iterator I = Func;
1223 if (I == Func->getParent()->begin())
1228 void LLVMDeleteFunction(LLVMValueRef Fn) {
1229 unwrap<Function>(Fn)->eraseFromParent();
1232 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1233 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1234 return F->getIntrinsicID();
1238 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1239 return unwrap<Function>(Fn)->getCallingConv();
1242 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1243 return unwrap<Function>(Fn)->setCallingConv(
1244 static_cast<CallingConv::ID>(CC));
1247 const char *LLVMGetGC(LLVMValueRef Fn) {
1248 Function *F = unwrap<Function>(Fn);
1249 return F->hasGC()? F->getGC() : 0;
1252 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1253 Function *F = unwrap<Function>(Fn);
1260 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1261 Function *Func = unwrap<Function>(Fn);
1262 const AttrListPtr PAL = Func->getAttributes();
1263 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1264 Func->setAttributes(PALnew);
1267 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1268 Function *Func = unwrap<Function>(Fn);
1269 const AttrListPtr PAL = Func->getAttributes();
1270 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1271 Func->setAttributes(PALnew);
1274 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1275 Function *Func = unwrap<Function>(Fn);
1276 const AttrListPtr PAL = Func->getAttributes();
1277 Attributes attr = PAL.getFnAttributes();
1278 return (LLVMAttribute)attr;
1281 /*--.. Operations on parameters ............................................--*/
1283 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1284 // This function is strictly redundant to
1285 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1286 return unwrap<Function>(FnRef)->arg_size();
1289 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1290 Function *Fn = unwrap<Function>(FnRef);
1291 for (Function::arg_iterator I = Fn->arg_begin(),
1292 E = Fn->arg_end(); I != E; I++)
1293 *ParamRefs++ = wrap(I);
1296 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1297 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1303 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1304 return wrap(unwrap<Argument>(V)->getParent());
1307 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1308 Function *Func = unwrap<Function>(Fn);
1309 Function::arg_iterator I = Func->arg_begin();
1310 if (I == Func->arg_end())
1315 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1316 Function *Func = unwrap<Function>(Fn);
1317 Function::arg_iterator I = Func->arg_end();
1318 if (I == Func->arg_begin())
1323 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1324 Argument *A = unwrap<Argument>(Arg);
1325 Function::arg_iterator I = A;
1326 if (++I == A->getParent()->arg_end())
1331 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1332 Argument *A = unwrap<Argument>(Arg);
1333 Function::arg_iterator I = A;
1334 if (I == A->getParent()->arg_begin())
1339 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1340 unwrap<Argument>(Arg)->addAttr(PA);
1343 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1344 unwrap<Argument>(Arg)->removeAttr(PA);
1347 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1348 Argument *A = unwrap<Argument>(Arg);
1349 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1351 return (LLVMAttribute)attr;
1355 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1356 unwrap<Argument>(Arg)->addAttr(
1357 Attribute::constructAlignmentFromInt(align));
1360 /*--.. Operations on basic blocks ..........................................--*/
1362 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1363 return wrap(static_cast<Value*>(unwrap(BB)));
1366 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1367 return isa<BasicBlock>(unwrap(Val));
1370 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1371 return wrap(unwrap<BasicBlock>(Val));
1374 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1375 return wrap(unwrap(BB)->getParent());
1378 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1379 return unwrap<Function>(FnRef)->size();
1382 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1383 Function *Fn = unwrap<Function>(FnRef);
1384 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1385 *BasicBlocksRefs++ = wrap(I);
1388 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1389 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1392 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1393 Function *Func = unwrap<Function>(Fn);
1394 Function::iterator I = Func->begin();
1395 if (I == Func->end())
1400 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1401 Function *Func = unwrap<Function>(Fn);
1402 Function::iterator I = Func->end();
1403 if (I == Func->begin())
1408 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1409 BasicBlock *Block = unwrap(BB);
1410 Function::iterator I = Block;
1411 if (++I == Block->getParent()->end())
1416 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1417 BasicBlock *Block = unwrap(BB);
1418 Function::iterator I = Block;
1419 if (I == Block->getParent()->begin())
1424 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1427 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1430 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1431 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1434 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1435 LLVMBasicBlockRef BBRef,
1437 BasicBlock *BB = unwrap(BBRef);
1438 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1441 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1443 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1446 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1447 unwrap(BBRef)->eraseFromParent();
1450 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1451 unwrap(BB)->moveBefore(unwrap(MovePos));
1454 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1455 unwrap(BB)->moveAfter(unwrap(MovePos));
1458 /*--.. Operations on instructions ..........................................--*/
1460 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1461 return wrap(unwrap<Instruction>(Inst)->getParent());
1464 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1465 BasicBlock *Block = unwrap(BB);
1466 BasicBlock::iterator I = Block->begin();
1467 if (I == Block->end())
1472 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1473 BasicBlock *Block = unwrap(BB);
1474 BasicBlock::iterator I = Block->end();
1475 if (I == Block->begin())
1480 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1481 Instruction *Instr = unwrap<Instruction>(Inst);
1482 BasicBlock::iterator I = Instr;
1483 if (++I == Instr->getParent()->end())
1488 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1489 Instruction *Instr = unwrap<Instruction>(Inst);
1490 BasicBlock::iterator I = Instr;
1491 if (I == Instr->getParent()->begin())
1496 /*--.. Call and invoke instructions ........................................--*/
1498 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1499 Value *V = unwrap(Instr);
1500 if (CallInst *CI = dyn_cast<CallInst>(V))
1501 return CI->getCallingConv();
1502 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1503 return II->getCallingConv();
1504 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1508 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1509 Value *V = unwrap(Instr);
1510 if (CallInst *CI = dyn_cast<CallInst>(V))
1511 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1512 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1513 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1514 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1517 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1519 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1521 Call.getAttributes().addAttr(index, PA));
1524 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1526 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1528 Call.getAttributes().removeAttr(index, PA));
1531 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1533 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1535 Call.getAttributes().addAttr(index,
1536 Attribute::constructAlignmentFromInt(align)));
1539 /*--.. Operations on call instructions (only) ..............................--*/
1541 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1542 return unwrap<CallInst>(Call)->isTailCall();
1545 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1546 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1549 /*--.. Operations on phi nodes .............................................--*/
1551 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1552 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1553 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1554 for (unsigned I = 0; I != Count; ++I)
1555 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1558 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1559 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1562 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1563 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1566 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1567 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1571 /*===-- Instruction builders ----------------------------------------------===*/
1573 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1574 return wrap(new IRBuilder<>(*unwrap(C)));
1577 LLVMBuilderRef LLVMCreateBuilder(void) {
1578 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1581 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1582 LLVMValueRef Instr) {
1583 BasicBlock *BB = unwrap(Block);
1584 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1585 unwrap(Builder)->SetInsertPoint(BB, I);
1588 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1589 Instruction *I = unwrap<Instruction>(Instr);
1590 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1593 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1594 BasicBlock *BB = unwrap(Block);
1595 unwrap(Builder)->SetInsertPoint(BB);
1598 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1599 return wrap(unwrap(Builder)->GetInsertBlock());
1602 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1603 unwrap(Builder)->ClearInsertionPoint();
1606 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1607 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1610 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1612 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1615 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1616 delete unwrap(Builder);
1619 /*--.. Metadata builders ...................................................--*/
1621 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1622 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1623 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1626 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1627 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1628 .getAsMDNode(unwrap(Builder)->getContext()));
1631 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1632 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1636 /*--.. Instruction builders ................................................--*/
1638 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1639 return wrap(unwrap(B)->CreateRetVoid());
1642 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1643 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1646 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1648 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1651 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1652 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1655 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1656 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1657 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1660 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1661 LLVMBasicBlockRef Else, unsigned NumCases) {
1662 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1665 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1666 unsigned NumDests) {
1667 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1670 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1671 LLVMValueRef *Args, unsigned NumArgs,
1672 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1674 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1675 unwrap(Args), unwrap(Args) + NumArgs,
1679 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1680 return wrap(unwrap(B)->CreateUnwind());
1683 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1684 return wrap(unwrap(B)->CreateUnreachable());
1687 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1688 LLVMBasicBlockRef Dest) {
1689 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1692 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1693 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1696 /*--.. Arithmetic ..........................................................--*/
1698 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1700 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1703 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1705 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1708 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1710 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1713 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1715 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1718 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1720 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1723 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1725 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1728 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1730 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1733 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1735 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1738 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1740 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1743 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1745 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1748 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1750 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1753 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1755 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1758 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1760 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1763 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1765 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1768 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1769 LLVMValueRef RHS, const char *Name) {
1770 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1773 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1775 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1778 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1780 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1783 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1785 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1788 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1790 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1793 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1795 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1798 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1800 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1803 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1805 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1808 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1810 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1813 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1815 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1818 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1820 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1823 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1824 LLVMValueRef LHS, LLVMValueRef RHS,
1826 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1827 unwrap(RHS), Name));
1830 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1831 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1834 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1836 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1839 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1841 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1844 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1845 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1848 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1849 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1852 /*--.. Memory ..............................................................--*/
1854 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1856 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1857 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1858 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1859 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1860 ITy, unwrap(Ty), AllocSize,
1862 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1865 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1866 LLVMValueRef Val, const char *Name) {
1867 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1868 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1869 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1870 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1871 ITy, unwrap(Ty), AllocSize,
1872 unwrap(Val), 0, "");
1873 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1876 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1878 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1881 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1882 LLVMValueRef Val, const char *Name) {
1883 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1886 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1887 return wrap(unwrap(B)->Insert(
1888 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1892 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1894 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1897 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1898 LLVMValueRef PointerVal) {
1899 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1902 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1903 LLVMValueRef *Indices, unsigned NumIndices,
1905 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1906 unwrap(Indices) + NumIndices, Name));
1909 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1910 LLVMValueRef *Indices, unsigned NumIndices,
1912 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1913 unwrap(Indices) + NumIndices, Name));
1916 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1917 unsigned Idx, const char *Name) {
1918 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1921 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1923 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1926 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1928 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1931 /*--.. Casts ...............................................................--*/
1933 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1934 LLVMTypeRef DestTy, const char *Name) {
1935 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1938 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1939 LLVMTypeRef DestTy, const char *Name) {
1940 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1943 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1944 LLVMTypeRef DestTy, const char *Name) {
1945 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1948 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1949 LLVMTypeRef DestTy, const char *Name) {
1950 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1953 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1954 LLVMTypeRef DestTy, const char *Name) {
1955 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1958 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1959 LLVMTypeRef DestTy, const char *Name) {
1960 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1963 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1964 LLVMTypeRef DestTy, const char *Name) {
1965 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1968 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1969 LLVMTypeRef DestTy, const char *Name) {
1970 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1973 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1974 LLVMTypeRef DestTy, const char *Name) {
1975 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1978 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1979 LLVMTypeRef DestTy, const char *Name) {
1980 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1983 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1984 LLVMTypeRef DestTy, const char *Name) {
1985 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1988 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1989 LLVMTypeRef DestTy, const char *Name) {
1990 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1993 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1994 LLVMTypeRef DestTy, const char *Name) {
1995 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1999 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2000 LLVMTypeRef DestTy, const char *Name) {
2001 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2005 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2006 LLVMTypeRef DestTy, const char *Name) {
2007 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2011 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2012 LLVMTypeRef DestTy, const char *Name) {
2013 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2014 unwrap(DestTy), Name));
2017 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2018 LLVMTypeRef DestTy, const char *Name) {
2019 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2022 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2023 LLVMTypeRef DestTy, const char *Name) {
2024 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2025 /*isSigned*/true, Name));
2028 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2029 LLVMTypeRef DestTy, const char *Name) {
2030 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2033 /*--.. Comparisons .........................................................--*/
2035 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2036 LLVMValueRef LHS, LLVMValueRef RHS,
2038 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2039 unwrap(LHS), unwrap(RHS), Name));
2042 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2043 LLVMValueRef LHS, LLVMValueRef RHS,
2045 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2046 unwrap(LHS), unwrap(RHS), Name));
2049 /*--.. Miscellaneous instructions ..........................................--*/
2051 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2052 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2055 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2056 LLVMValueRef *Args, unsigned NumArgs,
2058 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2059 unwrap(Args) + NumArgs, Name));
2062 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2063 LLVMValueRef Then, LLVMValueRef Else,
2065 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2069 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2070 LLVMTypeRef Ty, const char *Name) {
2071 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2074 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2075 LLVMValueRef Index, const char *Name) {
2076 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2080 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2081 LLVMValueRef EltVal, LLVMValueRef Index,
2083 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2084 unwrap(Index), Name));
2087 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2088 LLVMValueRef V2, LLVMValueRef Mask,
2090 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2091 unwrap(Mask), Name));
2094 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2095 unsigned Index, const char *Name) {
2096 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2099 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2100 LLVMValueRef EltVal, unsigned Index,
2102 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2106 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2108 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2111 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2113 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2116 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2117 LLVMValueRef RHS, const char *Name) {
2118 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2122 /*===-- Module providers --------------------------------------------------===*/
2124 LLVMModuleProviderRef
2125 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2126 return reinterpret_cast<LLVMModuleProviderRef>(M);
2129 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2134 /*===-- Memory buffers ----------------------------------------------------===*/
2136 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2138 LLVMMemoryBufferRef *OutMemBuf,
2139 char **OutMessage) {
2141 OwningPtr<MemoryBuffer> MB;
2143 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2144 *OutMemBuf = wrap(MB.take());
2148 *OutMessage = strdup(ec.message().c_str());
2152 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2153 char **OutMessage) {
2154 OwningPtr<MemoryBuffer> MB;
2156 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2157 *OutMemBuf = wrap(MB.take());
2161 *OutMessage = strdup(ec.message().c_str());
2165 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2166 delete unwrap(MemBuf);
2169 /*===-- Pass Registry -----------------------------------------------------===*/
2171 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2172 return wrap(PassRegistry::getPassRegistry());
2175 /*===-- Pass Manager ------------------------------------------------------===*/
2177 LLVMPassManagerRef LLVMCreatePassManager() {
2178 return wrap(new PassManager());
2181 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2182 return wrap(new FunctionPassManager(unwrap(M)));
2185 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2186 return LLVMCreateFunctionPassManagerForModule(
2187 reinterpret_cast<LLVMModuleRef>(P));
2190 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2191 return unwrap<PassManager>(PM)->run(*unwrap(M));
2194 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2195 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2198 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2199 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2202 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2203 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2206 void LLVMDisposePassManager(LLVMPassManagerRef PM) {