1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CallSite.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/system_error.h"
38 void llvm::initializeCore(PassRegistry &Registry) {
39 initializeDominatorTreePass(Registry);
40 initializePrintModulePassPass(Registry);
41 initializePrintFunctionPassPass(Registry);
42 initializeVerifierPass(Registry);
43 initializePreVerifierPass(Registry);
46 void LLVMInitializeCore(LLVMPassRegistryRef R) {
47 initializeCore(*unwrap(R));
50 /*===-- Error handling ----------------------------------------------------===*/
52 void LLVMDisposeMessage(char *Message) {
57 /*===-- Operations on contexts --------------------------------------------===*/
59 LLVMContextRef LLVMContextCreate() {
60 return wrap(new LLVMContext());
63 LLVMContextRef LLVMGetGlobalContext() {
64 return wrap(&getGlobalContext());
67 void LLVMContextDispose(LLVMContextRef C) {
71 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
73 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
76 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
77 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
81 /*===-- Operations on modules ---------------------------------------------===*/
83 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
84 return wrap(new Module(ModuleID, getGlobalContext()));
87 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
89 return wrap(new Module(ModuleID, *unwrap(C)));
92 void LLVMDisposeModule(LLVMModuleRef M) {
96 /*--.. Data layout .........................................................--*/
97 const char * LLVMGetDataLayout(LLVMModuleRef M) {
98 return unwrap(M)->getDataLayout().c_str();
101 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
102 unwrap(M)->setDataLayout(Triple);
105 /*--.. Target triple .......................................................--*/
106 const char * LLVMGetTarget(LLVMModuleRef M) {
107 return unwrap(M)->getTargetTriple().c_str();
110 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
111 unwrap(M)->setTargetTriple(Triple);
114 /*--.. Type names ..........................................................--*/
115 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
116 return unwrap(M)->addTypeName(Name, unwrap(Ty));
119 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
120 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
122 TypeSymbolTable::iterator I = TST.find(Name);
127 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
128 return wrap(unwrap(M)->getTypeByName(Name));
131 void LLVMDumpModule(LLVMModuleRef M) {
135 /*--.. Operations on inline assembler ......................................--*/
136 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
137 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
141 /*--.. Operations on module contexts ......................................--*/
142 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
143 return wrap(&unwrap(M)->getContext());
147 /*===-- Operations on types -----------------------------------------------===*/
149 /*--.. Operations on all types (mostly) ....................................--*/
151 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
152 switch (unwrap(Ty)->getTypeID()) {
154 assert(false && "Unhandled TypeID.");
156 return LLVMVoidTypeKind;
157 case Type::FloatTyID:
158 return LLVMFloatTypeKind;
159 case Type::DoubleTyID:
160 return LLVMDoubleTypeKind;
161 case Type::X86_FP80TyID:
162 return LLVMX86_FP80TypeKind;
163 case Type::FP128TyID:
164 return LLVMFP128TypeKind;
165 case Type::PPC_FP128TyID:
166 return LLVMPPC_FP128TypeKind;
167 case Type::LabelTyID:
168 return LLVMLabelTypeKind;
169 case Type::MetadataTyID:
170 return LLVMMetadataTypeKind;
171 case Type::IntegerTyID:
172 return LLVMIntegerTypeKind;
173 case Type::FunctionTyID:
174 return LLVMFunctionTypeKind;
175 case Type::StructTyID:
176 return LLVMStructTypeKind;
177 case Type::ArrayTyID:
178 return LLVMArrayTypeKind;
179 case Type::PointerTyID:
180 return LLVMPointerTypeKind;
181 case Type::OpaqueTyID:
182 return LLVMOpaqueTypeKind;
183 case Type::VectorTyID:
184 return LLVMVectorTypeKind;
185 case Type::X86_MMXTyID:
186 return LLVMX86_MMXTypeKind;
190 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
191 return wrap(&unwrap(Ty)->getContext());
194 /*--.. Operations on integer types .........................................--*/
196 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
197 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
199 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
200 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
202 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
203 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
205 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
206 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
208 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
209 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
211 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
212 return wrap(IntegerType::get(*unwrap(C), NumBits));
215 LLVMTypeRef LLVMInt1Type(void) {
216 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
218 LLVMTypeRef LLVMInt8Type(void) {
219 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
221 LLVMTypeRef LLVMInt16Type(void) {
222 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
224 LLVMTypeRef LLVMInt32Type(void) {
225 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
227 LLVMTypeRef LLVMInt64Type(void) {
228 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
230 LLVMTypeRef LLVMIntType(unsigned NumBits) {
231 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
234 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
235 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
238 /*--.. Operations on real types ............................................--*/
240 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
241 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
243 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
244 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
246 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
247 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
249 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
250 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
252 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
253 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
255 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
256 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
259 LLVMTypeRef LLVMFloatType(void) {
260 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
262 LLVMTypeRef LLVMDoubleType(void) {
263 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
265 LLVMTypeRef LLVMX86FP80Type(void) {
266 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
268 LLVMTypeRef LLVMFP128Type(void) {
269 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
271 LLVMTypeRef LLVMPPCFP128Type(void) {
272 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
274 LLVMTypeRef LLVMX86MMXType(void) {
275 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
278 /*--.. Operations on function types ........................................--*/
280 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
281 LLVMTypeRef *ParamTypes, unsigned ParamCount,
283 std::vector<const Type*> Tys;
284 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
285 Tys.push_back(unwrap(*I));
287 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
290 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
291 return unwrap<FunctionType>(FunctionTy)->isVarArg();
294 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
295 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
298 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
299 return unwrap<FunctionType>(FunctionTy)->getNumParams();
302 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
303 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
304 for (FunctionType::param_iterator I = Ty->param_begin(),
305 E = Ty->param_end(); I != E; ++I)
309 /*--.. Operations on struct types ..........................................--*/
311 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
312 unsigned ElementCount, LLVMBool Packed) {
313 std::vector<const Type*> Tys;
314 for (LLVMTypeRef *I = ElementTypes,
315 *E = ElementTypes + ElementCount; I != E; ++I)
316 Tys.push_back(unwrap(*I));
318 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
321 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
322 unsigned ElementCount, LLVMBool Packed) {
323 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
324 ElementCount, Packed);
328 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
329 return unwrap<StructType>(StructTy)->getNumElements();
332 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
333 StructType *Ty = unwrap<StructType>(StructTy);
334 for (FunctionType::param_iterator I = Ty->element_begin(),
335 E = Ty->element_end(); I != E; ++I)
339 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
340 return unwrap<StructType>(StructTy)->isPacked();
343 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
345 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
346 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
349 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
350 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
353 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
354 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
357 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
358 return wrap(unwrap<SequentialType>(Ty)->getElementType());
361 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
362 return unwrap<ArrayType>(ArrayTy)->getNumElements();
365 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
366 return unwrap<PointerType>(PointerTy)->getAddressSpace();
369 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
370 return unwrap<VectorType>(VectorTy)->getNumElements();
373 /*--.. Operations on other types ...........................................--*/
375 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
376 return wrap(Type::getVoidTy(*unwrap(C)));
378 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
379 return wrap(Type::getLabelTy(*unwrap(C)));
381 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
382 return wrap(OpaqueType::get(*unwrap(C)));
385 LLVMTypeRef LLVMVoidType(void) {
386 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
388 LLVMTypeRef LLVMLabelType(void) {
389 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
391 LLVMTypeRef LLVMOpaqueType(void) {
392 return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
395 /*--.. Operations on type handles ..........................................--*/
397 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
398 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
401 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
402 delete unwrap(TypeHandle);
405 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
406 return wrap(unwrap(TypeHandle)->get());
409 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
410 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
414 /*===-- Operations on values ----------------------------------------------===*/
416 /*--.. Operations on all values ............................................--*/
418 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
419 return wrap(unwrap(Val)->getType());
422 const char *LLVMGetValueName(LLVMValueRef Val) {
423 return unwrap(Val)->getName().data();
426 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
427 unwrap(Val)->setName(Name);
430 void LLVMDumpValue(LLVMValueRef Val) {
434 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
435 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
438 int LLVMHasMetadata(LLVMValueRef Inst) {
439 return unwrap<Instruction>(Inst)->hasMetadata();
442 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
443 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
446 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
447 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
450 /*--.. Conversion functions ................................................--*/
452 #define LLVM_DEFINE_VALUE_CAST(name) \
453 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
454 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
457 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
459 /*--.. Operations on Uses ..................................................--*/
460 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
461 Value *V = unwrap(Val);
462 Value::use_iterator I = V->use_begin();
463 if (I == V->use_end())
465 return wrap(&(I.getUse()));
468 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
469 Use *Next = unwrap(U)->getNext();
475 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
476 return wrap(unwrap(U)->getUser());
479 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
480 return wrap(unwrap(U)->get());
483 /*--.. Operations on Users .................................................--*/
484 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
485 return wrap(unwrap<User>(Val)->getOperand(Index));
488 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
489 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
492 int LLVMGetNumOperands(LLVMValueRef Val) {
493 return unwrap<User>(Val)->getNumOperands();
496 /*--.. Operations on constants of any type .................................--*/
498 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
499 return wrap(Constant::getNullValue(unwrap(Ty)));
502 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
503 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
506 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
507 return wrap(UndefValue::get(unwrap(Ty)));
510 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
511 return isa<Constant>(unwrap(Ty));
514 LLVMBool LLVMIsNull(LLVMValueRef Val) {
515 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
516 return C->isNullValue();
520 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
521 return isa<UndefValue>(unwrap(Val));
524 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
526 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
529 /*--.. Operations on metadata nodes ........................................--*/
531 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
533 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
536 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
537 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
540 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
542 return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
545 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
546 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
549 /*--.. Operations on scalar constants ......................................--*/
551 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
552 LLVMBool SignExtend) {
553 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
556 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
558 const uint64_t Words[]) {
559 IntegerType *Ty = unwrap<IntegerType>(IntTy);
560 return wrap(ConstantInt::get(Ty->getContext(),
561 APInt(Ty->getBitWidth(), NumWords, Words)));
564 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
566 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
570 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
571 unsigned SLen, uint8_t Radix) {
572 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
576 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
577 return wrap(ConstantFP::get(unwrap(RealTy), N));
580 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
581 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
584 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
586 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
589 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
590 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
593 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
594 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
597 /*--.. Operations on composite constants ...................................--*/
599 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
601 LLVMBool DontNullTerminate) {
602 /* Inverted the sense of AddNull because ', 0)' is a
603 better mnemonic for null termination than ', 1)'. */
604 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
605 DontNullTerminate == 0));
607 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
608 LLVMValueRef *ConstantVals,
609 unsigned Count, LLVMBool Packed) {
610 return wrap(ConstantStruct::get(*unwrap(C),
611 unwrap<Constant>(ConstantVals, Count),
612 Count, Packed != 0));
615 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
616 LLVMBool DontNullTerminate) {
617 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
620 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
621 LLVMValueRef *ConstantVals, unsigned Length) {
622 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
623 unwrap<Constant>(ConstantVals, Length),
626 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
628 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
631 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
632 return wrap(ConstantVector::get(ArrayRef<Constant*>(
633 unwrap<Constant>(ScalarConstantVals, Size), Size)));
635 /*--.. Constant expressions ................................................--*/
637 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
638 return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
641 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
642 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
645 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
646 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
649 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
650 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
653 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
654 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
657 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
658 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
662 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
663 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
666 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
667 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
670 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
671 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
672 unwrap<Constant>(RHSConstant)));
675 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
676 LLVMValueRef RHSConstant) {
677 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
678 unwrap<Constant>(RHSConstant)));
681 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
682 LLVMValueRef RHSConstant) {
683 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
684 unwrap<Constant>(RHSConstant)));
687 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
688 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
689 unwrap<Constant>(RHSConstant)));
692 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
693 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
694 unwrap<Constant>(RHSConstant)));
697 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
698 LLVMValueRef RHSConstant) {
699 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
700 unwrap<Constant>(RHSConstant)));
703 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
704 LLVMValueRef RHSConstant) {
705 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
706 unwrap<Constant>(RHSConstant)));
709 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
710 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
711 unwrap<Constant>(RHSConstant)));
714 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
715 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
716 unwrap<Constant>(RHSConstant)));
719 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
720 LLVMValueRef RHSConstant) {
721 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
722 unwrap<Constant>(RHSConstant)));
725 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
726 LLVMValueRef RHSConstant) {
727 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
728 unwrap<Constant>(RHSConstant)));
731 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
732 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
733 unwrap<Constant>(RHSConstant)));
736 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
737 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
738 unwrap<Constant>(RHSConstant)));
741 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
742 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
743 unwrap<Constant>(RHSConstant)));
746 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
747 LLVMValueRef RHSConstant) {
748 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
749 unwrap<Constant>(RHSConstant)));
752 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
753 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
754 unwrap<Constant>(RHSConstant)));
757 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
759 unwrap<Constant>(RHSConstant)));
762 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
763 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
764 unwrap<Constant>(RHSConstant)));
767 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
768 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
769 unwrap<Constant>(RHSConstant)));
772 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
773 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
774 unwrap<Constant>(RHSConstant)));
777 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
778 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
779 unwrap<Constant>(RHSConstant)));
782 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
783 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
784 unwrap<Constant>(RHSConstant)));
787 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
788 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
789 return wrap(ConstantExpr::getICmp(Predicate,
790 unwrap<Constant>(LHSConstant),
791 unwrap<Constant>(RHSConstant)));
794 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
795 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
796 return wrap(ConstantExpr::getFCmp(Predicate,
797 unwrap<Constant>(LHSConstant),
798 unwrap<Constant>(RHSConstant)));
801 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
802 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
803 unwrap<Constant>(RHSConstant)));
806 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
807 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
808 unwrap<Constant>(RHSConstant)));
811 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
812 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
813 unwrap<Constant>(RHSConstant)));
816 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
817 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
818 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
819 unwrap<Constant>(ConstantIndices,
824 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
825 LLVMValueRef *ConstantIndices,
826 unsigned NumIndices) {
827 Constant* Val = unwrap<Constant>(ConstantVal);
828 Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
829 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
832 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
833 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
837 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
838 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
842 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
843 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
847 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
848 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
852 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
853 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
857 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
858 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
862 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
863 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
867 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
868 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
872 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
873 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
877 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
878 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
882 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
883 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
887 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
888 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
892 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
893 LLVMTypeRef ToType) {
894 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
898 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
899 LLVMTypeRef ToType) {
900 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
904 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
905 LLVMTypeRef ToType) {
906 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
910 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
911 LLVMTypeRef ToType) {
912 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
916 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
918 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
919 unwrap(ToType), isSigned));
922 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
923 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
927 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
928 LLVMValueRef ConstantIfTrue,
929 LLVMValueRef ConstantIfFalse) {
930 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
931 unwrap<Constant>(ConstantIfTrue),
932 unwrap<Constant>(ConstantIfFalse)));
935 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
936 LLVMValueRef IndexConstant) {
937 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
938 unwrap<Constant>(IndexConstant)));
941 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
942 LLVMValueRef ElementValueConstant,
943 LLVMValueRef IndexConstant) {
944 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
945 unwrap<Constant>(ElementValueConstant),
946 unwrap<Constant>(IndexConstant)));
949 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
950 LLVMValueRef VectorBConstant,
951 LLVMValueRef MaskConstant) {
952 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
953 unwrap<Constant>(VectorBConstant),
954 unwrap<Constant>(MaskConstant)));
957 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
959 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
963 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
964 LLVMValueRef ElementValueConstant,
965 unsigned *IdxList, unsigned NumIdx) {
966 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
967 unwrap<Constant>(ElementValueConstant),
971 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
972 const char *Constraints,
973 LLVMBool HasSideEffects,
974 LLVMBool IsAlignStack) {
975 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
976 Constraints, HasSideEffects, IsAlignStack));
979 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
980 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
983 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
985 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
986 return wrap(unwrap<GlobalValue>(Global)->getParent());
989 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
990 return unwrap<GlobalValue>(Global)->isDeclaration();
993 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
994 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
996 assert(false && "Unhandled Linkage Type.");
997 case GlobalValue::ExternalLinkage:
998 return LLVMExternalLinkage;
999 case GlobalValue::AvailableExternallyLinkage:
1000 return LLVMAvailableExternallyLinkage;
1001 case GlobalValue::LinkOnceAnyLinkage:
1002 return LLVMLinkOnceAnyLinkage;
1003 case GlobalValue::LinkOnceODRLinkage:
1004 return LLVMLinkOnceODRLinkage;
1005 case GlobalValue::WeakAnyLinkage:
1006 return LLVMWeakAnyLinkage;
1007 case GlobalValue::WeakODRLinkage:
1008 return LLVMWeakODRLinkage;
1009 case GlobalValue::AppendingLinkage:
1010 return LLVMAppendingLinkage;
1011 case GlobalValue::InternalLinkage:
1012 return LLVMInternalLinkage;
1013 case GlobalValue::PrivateLinkage:
1014 return LLVMPrivateLinkage;
1015 case GlobalValue::LinkerPrivateLinkage:
1016 return LLVMLinkerPrivateLinkage;
1017 case GlobalValue::LinkerPrivateWeakLinkage:
1018 return LLVMLinkerPrivateWeakLinkage;
1019 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1020 return LLVMLinkerPrivateWeakDefAutoLinkage;
1021 case GlobalValue::DLLImportLinkage:
1022 return LLVMDLLImportLinkage;
1023 case GlobalValue::DLLExportLinkage:
1024 return LLVMDLLExportLinkage;
1025 case GlobalValue::ExternalWeakLinkage:
1026 return LLVMExternalWeakLinkage;
1027 case GlobalValue::CommonLinkage:
1028 return LLVMCommonLinkage;
1031 // Should never get here.
1032 return static_cast<LLVMLinkage>(0);
1035 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1036 GlobalValue *GV = unwrap<GlobalValue>(Global);
1040 assert(false && "Unhandled Linkage Type.");
1041 case LLVMExternalLinkage:
1042 GV->setLinkage(GlobalValue::ExternalLinkage);
1044 case LLVMAvailableExternallyLinkage:
1045 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1047 case LLVMLinkOnceAnyLinkage:
1048 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1050 case LLVMLinkOnceODRLinkage:
1051 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1053 case LLVMWeakAnyLinkage:
1054 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1056 case LLVMWeakODRLinkage:
1057 GV->setLinkage(GlobalValue::WeakODRLinkage);
1059 case LLVMAppendingLinkage:
1060 GV->setLinkage(GlobalValue::AppendingLinkage);
1062 case LLVMInternalLinkage:
1063 GV->setLinkage(GlobalValue::InternalLinkage);
1065 case LLVMPrivateLinkage:
1066 GV->setLinkage(GlobalValue::PrivateLinkage);
1068 case LLVMLinkerPrivateLinkage:
1069 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1071 case LLVMLinkerPrivateWeakLinkage:
1072 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1074 case LLVMLinkerPrivateWeakDefAutoLinkage:
1075 GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1077 case LLVMDLLImportLinkage:
1078 GV->setLinkage(GlobalValue::DLLImportLinkage);
1080 case LLVMDLLExportLinkage:
1081 GV->setLinkage(GlobalValue::DLLExportLinkage);
1083 case LLVMExternalWeakLinkage:
1084 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1086 case LLVMGhostLinkage:
1088 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1090 case LLVMCommonLinkage:
1091 GV->setLinkage(GlobalValue::CommonLinkage);
1096 const char *LLVMGetSection(LLVMValueRef Global) {
1097 return unwrap<GlobalValue>(Global)->getSection().c_str();
1100 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1101 unwrap<GlobalValue>(Global)->setSection(Section);
1104 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1105 return static_cast<LLVMVisibility>(
1106 unwrap<GlobalValue>(Global)->getVisibility());
1109 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1110 unwrap<GlobalValue>(Global)
1111 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1114 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1115 return unwrap<GlobalValue>(Global)->getAlignment();
1118 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1119 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1122 /*--.. Operations on global variables ......................................--*/
1124 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1125 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1126 GlobalValue::ExternalLinkage, 0, Name));
1129 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1131 unsigned AddressSpace) {
1132 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1133 GlobalValue::ExternalLinkage, 0, Name, 0,
1134 false, AddressSpace));
1137 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1138 return wrap(unwrap(M)->getNamedGlobal(Name));
1141 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1142 Module *Mod = unwrap(M);
1143 Module::global_iterator I = Mod->global_begin();
1144 if (I == Mod->global_end())
1149 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1150 Module *Mod = unwrap(M);
1151 Module::global_iterator I = Mod->global_end();
1152 if (I == Mod->global_begin())
1157 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1158 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1159 Module::global_iterator I = GV;
1160 if (++I == GV->getParent()->global_end())
1165 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1166 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1167 Module::global_iterator I = GV;
1168 if (I == GV->getParent()->global_begin())
1173 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1174 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1177 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1178 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1179 if ( !GV->hasInitializer() )
1181 return wrap(GV->getInitializer());
1184 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1185 unwrap<GlobalVariable>(GlobalVar)
1186 ->setInitializer(unwrap<Constant>(ConstantVal));
1189 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1190 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1193 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1194 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1197 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1198 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1201 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1202 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1205 /*--.. Operations on aliases ......................................--*/
1207 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1209 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1210 unwrap<Constant>(Aliasee), unwrap (M)));
1213 /*--.. Operations on functions .............................................--*/
1215 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1216 LLVMTypeRef FunctionTy) {
1217 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1218 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1221 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1222 return wrap(unwrap(M)->getFunction(Name));
1225 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1226 Module *Mod = unwrap(M);
1227 Module::iterator I = Mod->begin();
1228 if (I == Mod->end())
1233 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1234 Module *Mod = unwrap(M);
1235 Module::iterator I = Mod->end();
1236 if (I == Mod->begin())
1241 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1242 Function *Func = unwrap<Function>(Fn);
1243 Module::iterator I = Func;
1244 if (++I == Func->getParent()->end())
1249 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1250 Function *Func = unwrap<Function>(Fn);
1251 Module::iterator I = Func;
1252 if (I == Func->getParent()->begin())
1257 void LLVMDeleteFunction(LLVMValueRef Fn) {
1258 unwrap<Function>(Fn)->eraseFromParent();
1261 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1262 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1263 return F->getIntrinsicID();
1267 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1268 return unwrap<Function>(Fn)->getCallingConv();
1271 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1272 return unwrap<Function>(Fn)->setCallingConv(
1273 static_cast<CallingConv::ID>(CC));
1276 const char *LLVMGetGC(LLVMValueRef Fn) {
1277 Function *F = unwrap<Function>(Fn);
1278 return F->hasGC()? F->getGC() : 0;
1281 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1282 Function *F = unwrap<Function>(Fn);
1289 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1290 Function *Func = unwrap<Function>(Fn);
1291 const AttrListPtr PAL = Func->getAttributes();
1292 const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1293 Func->setAttributes(PALnew);
1296 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1297 Function *Func = unwrap<Function>(Fn);
1298 const AttrListPtr PAL = Func->getAttributes();
1299 const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1300 Func->setAttributes(PALnew);
1303 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1304 Function *Func = unwrap<Function>(Fn);
1305 const AttrListPtr PAL = Func->getAttributes();
1306 Attributes attr = PAL.getFnAttributes();
1307 return (LLVMAttribute)attr;
1310 /*--.. Operations on parameters ............................................--*/
1312 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1313 // This function is strictly redundant to
1314 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1315 return unwrap<Function>(FnRef)->arg_size();
1318 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1319 Function *Fn = unwrap<Function>(FnRef);
1320 for (Function::arg_iterator I = Fn->arg_begin(),
1321 E = Fn->arg_end(); I != E; I++)
1322 *ParamRefs++ = wrap(I);
1325 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1326 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1332 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1333 return wrap(unwrap<Argument>(V)->getParent());
1336 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1337 Function *Func = unwrap<Function>(Fn);
1338 Function::arg_iterator I = Func->arg_begin();
1339 if (I == Func->arg_end())
1344 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1345 Function *Func = unwrap<Function>(Fn);
1346 Function::arg_iterator I = Func->arg_end();
1347 if (I == Func->arg_begin())
1352 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1353 Argument *A = unwrap<Argument>(Arg);
1354 Function::arg_iterator I = A;
1355 if (++I == A->getParent()->arg_end())
1360 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1361 Argument *A = unwrap<Argument>(Arg);
1362 Function::arg_iterator I = A;
1363 if (I == A->getParent()->arg_begin())
1368 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1369 unwrap<Argument>(Arg)->addAttr(PA);
1372 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1373 unwrap<Argument>(Arg)->removeAttr(PA);
1376 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1377 Argument *A = unwrap<Argument>(Arg);
1378 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1380 return (LLVMAttribute)attr;
1384 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1385 unwrap<Argument>(Arg)->addAttr(
1386 Attribute::constructAlignmentFromInt(align));
1389 /*--.. Operations on basic blocks ..........................................--*/
1391 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1392 return wrap(static_cast<Value*>(unwrap(BB)));
1395 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1396 return isa<BasicBlock>(unwrap(Val));
1399 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1400 return wrap(unwrap<BasicBlock>(Val));
1403 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1404 return wrap(unwrap(BB)->getParent());
1407 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1408 return unwrap<Function>(FnRef)->size();
1411 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1412 Function *Fn = unwrap<Function>(FnRef);
1413 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1414 *BasicBlocksRefs++ = wrap(I);
1417 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1418 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1421 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1422 Function *Func = unwrap<Function>(Fn);
1423 Function::iterator I = Func->begin();
1424 if (I == Func->end())
1429 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1430 Function *Func = unwrap<Function>(Fn);
1431 Function::iterator I = Func->end();
1432 if (I == Func->begin())
1437 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1438 BasicBlock *Block = unwrap(BB);
1439 Function::iterator I = Block;
1440 if (++I == Block->getParent()->end())
1445 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1446 BasicBlock *Block = unwrap(BB);
1447 Function::iterator I = Block;
1448 if (I == Block->getParent()->begin())
1453 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1456 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1459 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1460 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1463 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1464 LLVMBasicBlockRef BBRef,
1466 BasicBlock *BB = unwrap(BBRef);
1467 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1470 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1472 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1475 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1476 unwrap(BBRef)->eraseFromParent();
1479 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1480 unwrap(BB)->moveBefore(unwrap(MovePos));
1483 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1484 unwrap(BB)->moveAfter(unwrap(MovePos));
1487 /*--.. Operations on instructions ..........................................--*/
1489 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1490 return wrap(unwrap<Instruction>(Inst)->getParent());
1493 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1494 BasicBlock *Block = unwrap(BB);
1495 BasicBlock::iterator I = Block->begin();
1496 if (I == Block->end())
1501 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1502 BasicBlock *Block = unwrap(BB);
1503 BasicBlock::iterator I = Block->end();
1504 if (I == Block->begin())
1509 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1510 Instruction *Instr = unwrap<Instruction>(Inst);
1511 BasicBlock::iterator I = Instr;
1512 if (++I == Instr->getParent()->end())
1517 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1518 Instruction *Instr = unwrap<Instruction>(Inst);
1519 BasicBlock::iterator I = Instr;
1520 if (I == Instr->getParent()->begin())
1525 /*--.. Call and invoke instructions ........................................--*/
1527 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1528 Value *V = unwrap(Instr);
1529 if (CallInst *CI = dyn_cast<CallInst>(V))
1530 return CI->getCallingConv();
1531 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1532 return II->getCallingConv();
1533 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1537 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1538 Value *V = unwrap(Instr);
1539 if (CallInst *CI = dyn_cast<CallInst>(V))
1540 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1541 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1542 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1543 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1546 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1548 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1550 Call.getAttributes().addAttr(index, PA));
1553 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1555 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1557 Call.getAttributes().removeAttr(index, PA));
1560 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1562 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1564 Call.getAttributes().addAttr(index,
1565 Attribute::constructAlignmentFromInt(align)));
1568 /*--.. Operations on call instructions (only) ..............................--*/
1570 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1571 return unwrap<CallInst>(Call)->isTailCall();
1574 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1575 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1578 /*--.. Operations on phi nodes .............................................--*/
1580 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1581 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1582 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1583 for (unsigned I = 0; I != Count; ++I)
1584 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1587 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1588 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1591 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1592 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1595 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1596 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1600 /*===-- Instruction builders ----------------------------------------------===*/
1602 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1603 return wrap(new IRBuilder<>(*unwrap(C)));
1606 LLVMBuilderRef LLVMCreateBuilder(void) {
1607 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1610 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1611 LLVMValueRef Instr) {
1612 BasicBlock *BB = unwrap(Block);
1613 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1614 unwrap(Builder)->SetInsertPoint(BB, I);
1617 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1618 Instruction *I = unwrap<Instruction>(Instr);
1619 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1622 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1623 BasicBlock *BB = unwrap(Block);
1624 unwrap(Builder)->SetInsertPoint(BB);
1627 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1628 return wrap(unwrap(Builder)->GetInsertBlock());
1631 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1632 unwrap(Builder)->ClearInsertionPoint();
1635 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1636 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1639 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1641 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1644 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1645 delete unwrap(Builder);
1648 /*--.. Metadata builders ...................................................--*/
1650 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1651 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1652 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1655 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1656 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1657 .getAsMDNode(unwrap(Builder)->getContext()));
1660 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1661 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1665 /*--.. Instruction builders ................................................--*/
1667 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1668 return wrap(unwrap(B)->CreateRetVoid());
1671 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1672 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1675 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1677 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1680 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1681 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1684 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1685 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1686 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1689 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1690 LLVMBasicBlockRef Else, unsigned NumCases) {
1691 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1694 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1695 unsigned NumDests) {
1696 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1699 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1700 LLVMValueRef *Args, unsigned NumArgs,
1701 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1703 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1704 unwrap(Args), unwrap(Args) + NumArgs,
1708 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1709 return wrap(unwrap(B)->CreateUnwind());
1712 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1713 return wrap(unwrap(B)->CreateUnreachable());
1716 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1717 LLVMBasicBlockRef Dest) {
1718 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1721 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1722 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1725 /*--.. Arithmetic ..........................................................--*/
1727 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1729 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1732 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1734 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1737 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1739 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1742 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1744 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1747 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1749 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1752 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1754 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1757 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1759 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1762 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1764 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1767 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1769 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1772 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1774 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1777 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1779 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1782 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1784 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1787 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1789 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1792 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1794 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1797 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1798 LLVMValueRef RHS, const char *Name) {
1799 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1802 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1804 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1807 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1809 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1812 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1814 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1817 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1819 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1822 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1824 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1827 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1829 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1832 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1834 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1837 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1839 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1842 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1844 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1847 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1849 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1852 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1853 LLVMValueRef LHS, LLVMValueRef RHS,
1855 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1856 unwrap(RHS), Name));
1859 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1860 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1863 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1865 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1868 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1870 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1873 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1874 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1877 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1878 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1881 /*--.. Memory ..............................................................--*/
1883 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1885 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1886 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1887 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1888 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1889 ITy, unwrap(Ty), AllocSize,
1891 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1894 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1895 LLVMValueRef Val, const char *Name) {
1896 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1897 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1898 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1899 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1900 ITy, unwrap(Ty), AllocSize,
1901 unwrap(Val), 0, "");
1902 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1905 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1907 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1910 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1911 LLVMValueRef Val, const char *Name) {
1912 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1915 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1916 return wrap(unwrap(B)->Insert(
1917 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1921 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1923 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1926 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1927 LLVMValueRef PointerVal) {
1928 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1931 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1932 LLVMValueRef *Indices, unsigned NumIndices,
1934 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1935 unwrap(Indices) + NumIndices, Name));
1938 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1939 LLVMValueRef *Indices, unsigned NumIndices,
1941 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1942 unwrap(Indices) + NumIndices, Name));
1945 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1946 unsigned Idx, const char *Name) {
1947 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1950 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1952 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1955 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1957 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1960 /*--.. Casts ...............................................................--*/
1962 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1963 LLVMTypeRef DestTy, const char *Name) {
1964 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1967 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1968 LLVMTypeRef DestTy, const char *Name) {
1969 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1972 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1973 LLVMTypeRef DestTy, const char *Name) {
1974 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1977 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1978 LLVMTypeRef DestTy, const char *Name) {
1979 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1982 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1983 LLVMTypeRef DestTy, const char *Name) {
1984 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1987 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1988 LLVMTypeRef DestTy, const char *Name) {
1989 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1992 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1993 LLVMTypeRef DestTy, const char *Name) {
1994 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1997 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1998 LLVMTypeRef DestTy, const char *Name) {
1999 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2002 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2003 LLVMTypeRef DestTy, const char *Name) {
2004 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2007 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2008 LLVMTypeRef DestTy, const char *Name) {
2009 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2012 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2013 LLVMTypeRef DestTy, const char *Name) {
2014 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2017 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2018 LLVMTypeRef DestTy, const char *Name) {
2019 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2022 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2023 LLVMTypeRef DestTy, const char *Name) {
2024 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2028 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2029 LLVMTypeRef DestTy, const char *Name) {
2030 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2034 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2035 LLVMTypeRef DestTy, const char *Name) {
2036 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2040 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2041 LLVMTypeRef DestTy, const char *Name) {
2042 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2043 unwrap(DestTy), Name));
2046 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2047 LLVMTypeRef DestTy, const char *Name) {
2048 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2051 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2052 LLVMTypeRef DestTy, const char *Name) {
2053 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2054 /*isSigned*/true, Name));
2057 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2058 LLVMTypeRef DestTy, const char *Name) {
2059 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2062 /*--.. Comparisons .........................................................--*/
2064 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2065 LLVMValueRef LHS, LLVMValueRef RHS,
2067 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2068 unwrap(LHS), unwrap(RHS), Name));
2071 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2072 LLVMValueRef LHS, LLVMValueRef RHS,
2074 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2075 unwrap(LHS), unwrap(RHS), Name));
2078 /*--.. Miscellaneous instructions ..........................................--*/
2080 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2081 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2084 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2085 LLVMValueRef *Args, unsigned NumArgs,
2087 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2088 unwrap(Args) + NumArgs, Name));
2091 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2092 LLVMValueRef Then, LLVMValueRef Else,
2094 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2098 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2099 LLVMTypeRef Ty, const char *Name) {
2100 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2103 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2104 LLVMValueRef Index, const char *Name) {
2105 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2109 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2110 LLVMValueRef EltVal, LLVMValueRef Index,
2112 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2113 unwrap(Index), Name));
2116 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2117 LLVMValueRef V2, LLVMValueRef Mask,
2119 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2120 unwrap(Mask), Name));
2123 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2124 unsigned Index, const char *Name) {
2125 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2128 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2129 LLVMValueRef EltVal, unsigned Index,
2131 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2135 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2137 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2140 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2142 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2145 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2146 LLVMValueRef RHS, const char *Name) {
2147 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2151 /*===-- Module providers --------------------------------------------------===*/
2153 LLVMModuleProviderRef
2154 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2155 return reinterpret_cast<LLVMModuleProviderRef>(M);
2158 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2163 /*===-- Memory buffers ----------------------------------------------------===*/
2165 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2167 LLVMMemoryBufferRef *OutMemBuf,
2168 char **OutMessage) {
2170 OwningPtr<MemoryBuffer> MB;
2172 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2173 *OutMemBuf = wrap(MB.take());
2177 *OutMessage = strdup(ec.message().c_str());
2181 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2182 char **OutMessage) {
2183 OwningPtr<MemoryBuffer> MB;
2185 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2186 *OutMemBuf = wrap(MB.take());
2190 *OutMessage = strdup(ec.message().c_str());
2194 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2195 delete unwrap(MemBuf);
2198 /*===-- Pass Registry -----------------------------------------------------===*/
2200 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2201 return wrap(PassRegistry::getPassRegistry());
2204 /*===-- Pass Manager ------------------------------------------------------===*/
2206 LLVMPassManagerRef LLVMCreatePassManager() {
2207 return wrap(new PassManager());
2210 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2211 return wrap(new FunctionPassManager(unwrap(M)));
2214 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2215 return LLVMCreateFunctionPassManagerForModule(
2216 reinterpret_cast<LLVMModuleRef>(P));
2219 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2220 return unwrap<PassManager>(PM)->run(*unwrap(M));
2223 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2224 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2227 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2228 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2231 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2232 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2235 void LLVMDisposePassManager(LLVMPassManagerRef PM) {