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/Attributes.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/GlobalAlias.h"
22 #include "llvm/LLVMContext.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 void LLVMDumpModule(LLVMModuleRef M) {
118 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
119 char **ErrorMessage) {
121 raw_fd_ostream dest(Filename, error);
122 if (!error.empty()) {
123 *ErrorMessage = strdup(error.c_str());
127 unwrap(M)->print(dest, NULL);
129 if (!error.empty()) {
130 *ErrorMessage = strdup(error.c_str());
137 /*--.. Operations on inline assembler ......................................--*/
138 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
139 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
143 /*--.. Operations on module contexts ......................................--*/
144 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
145 return wrap(&unwrap(M)->getContext());
149 /*===-- Operations on types -----------------------------------------------===*/
151 /*--.. Operations on all types (mostly) ....................................--*/
153 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
154 switch (unwrap(Ty)->getTypeID()) {
155 default: llvm_unreachable("Unhandled TypeID.");
157 return LLVMVoidTypeKind;
159 return LLVMHalfTypeKind;
160 case Type::FloatTyID:
161 return LLVMFloatTypeKind;
162 case Type::DoubleTyID:
163 return LLVMDoubleTypeKind;
164 case Type::X86_FP80TyID:
165 return LLVMX86_FP80TypeKind;
166 case Type::FP128TyID:
167 return LLVMFP128TypeKind;
168 case Type::PPC_FP128TyID:
169 return LLVMPPC_FP128TypeKind;
170 case Type::LabelTyID:
171 return LLVMLabelTypeKind;
172 case Type::MetadataTyID:
173 return LLVMMetadataTypeKind;
174 case Type::IntegerTyID:
175 return LLVMIntegerTypeKind;
176 case Type::FunctionTyID:
177 return LLVMFunctionTypeKind;
178 case Type::StructTyID:
179 return LLVMStructTypeKind;
180 case Type::ArrayTyID:
181 return LLVMArrayTypeKind;
182 case Type::PointerTyID:
183 return LLVMPointerTypeKind;
184 case Type::VectorTyID:
185 return LLVMVectorTypeKind;
186 case Type::X86_MMXTyID:
187 return LLVMX86_MMXTypeKind;
191 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
193 return unwrap(Ty)->isSized();
196 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
197 return wrap(&unwrap(Ty)->getContext());
200 /*--.. Operations on integer types .........................................--*/
202 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
203 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
205 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
206 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
208 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
209 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
211 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
212 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
214 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
215 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
217 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
218 return wrap(IntegerType::get(*unwrap(C), NumBits));
221 LLVMTypeRef LLVMInt1Type(void) {
222 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
224 LLVMTypeRef LLVMInt8Type(void) {
225 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
227 LLVMTypeRef LLVMInt16Type(void) {
228 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
230 LLVMTypeRef LLVMInt32Type(void) {
231 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
233 LLVMTypeRef LLVMInt64Type(void) {
234 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
236 LLVMTypeRef LLVMIntType(unsigned NumBits) {
237 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
240 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
241 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
244 /*--.. Operations on real types ............................................--*/
246 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
247 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
249 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
250 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
252 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
253 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
255 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
256 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
258 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
259 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
261 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
262 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
264 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
265 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
268 LLVMTypeRef LLVMHalfType(void) {
269 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
271 LLVMTypeRef LLVMFloatType(void) {
272 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
274 LLVMTypeRef LLVMDoubleType(void) {
275 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
277 LLVMTypeRef LLVMX86FP80Type(void) {
278 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
280 LLVMTypeRef LLVMFP128Type(void) {
281 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
283 LLVMTypeRef LLVMPPCFP128Type(void) {
284 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
286 LLVMTypeRef LLVMX86MMXType(void) {
287 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
290 /*--.. Operations on function types ........................................--*/
292 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
293 LLVMTypeRef *ParamTypes, unsigned ParamCount,
295 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
296 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
299 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
300 return unwrap<FunctionType>(FunctionTy)->isVarArg();
303 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
304 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
307 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
308 return unwrap<FunctionType>(FunctionTy)->getNumParams();
311 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
312 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
313 for (FunctionType::param_iterator I = Ty->param_begin(),
314 E = Ty->param_end(); I != E; ++I)
318 /*--.. Operations on struct types ..........................................--*/
320 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
321 unsigned ElementCount, LLVMBool Packed) {
322 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
323 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
326 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
327 unsigned ElementCount, LLVMBool Packed) {
328 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
329 ElementCount, Packed);
332 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
334 return wrap(StructType::create(*unwrap(C), Name));
337 const char *LLVMGetStructName(LLVMTypeRef Ty)
339 StructType *Type = unwrap<StructType>(Ty);
340 if (!Type->hasName())
342 return Type->getName().data();
345 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
346 unsigned ElementCount, LLVMBool Packed) {
347 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
348 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
351 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
352 return unwrap<StructType>(StructTy)->getNumElements();
355 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
356 StructType *Ty = unwrap<StructType>(StructTy);
357 for (StructType::element_iterator I = Ty->element_begin(),
358 E = Ty->element_end(); I != E; ++I)
362 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
363 return unwrap<StructType>(StructTy)->isPacked();
366 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
367 return unwrap<StructType>(StructTy)->isOpaque();
370 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
371 return wrap(unwrap(M)->getTypeByName(Name));
374 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
376 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
377 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
380 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
381 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
384 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
385 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
388 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
389 return wrap(unwrap<SequentialType>(Ty)->getElementType());
392 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
393 return unwrap<ArrayType>(ArrayTy)->getNumElements();
396 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
397 return unwrap<PointerType>(PointerTy)->getAddressSpace();
400 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
401 return unwrap<VectorType>(VectorTy)->getNumElements();
404 /*--.. Operations on other types ...........................................--*/
406 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
407 return wrap(Type::getVoidTy(*unwrap(C)));
409 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
410 return wrap(Type::getLabelTy(*unwrap(C)));
413 LLVMTypeRef LLVMVoidType(void) {
414 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
416 LLVMTypeRef LLVMLabelType(void) {
417 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
420 /*===-- Operations on values ----------------------------------------------===*/
422 /*--.. Operations on all values ............................................--*/
424 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
425 return wrap(unwrap(Val)->getType());
428 const char *LLVMGetValueName(LLVMValueRef Val) {
429 return unwrap(Val)->getName().data();
432 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
433 unwrap(Val)->setName(Name);
436 void LLVMDumpValue(LLVMValueRef Val) {
440 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
441 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
444 int LLVMHasMetadata(LLVMValueRef Inst) {
445 return unwrap<Instruction>(Inst)->hasMetadata();
448 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
449 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
452 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
453 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
456 /*--.. Conversion functions ................................................--*/
458 #define LLVM_DEFINE_VALUE_CAST(name) \
459 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
460 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
463 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
465 /*--.. Operations on Uses ..................................................--*/
466 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
467 Value *V = unwrap(Val);
468 Value::use_iterator I = V->use_begin();
469 if (I == V->use_end())
471 return wrap(&(I.getUse()));
474 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
475 Use *Next = unwrap(U)->getNext();
481 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
482 return wrap(unwrap(U)->getUser());
485 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
486 return wrap(unwrap(U)->get());
489 /*--.. Operations on Users .................................................--*/
490 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
491 Value *V = unwrap(Val);
492 if (MDNode *MD = dyn_cast<MDNode>(V))
493 return wrap(MD->getOperand(Index));
494 return wrap(cast<User>(V)->getOperand(Index));
497 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
498 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
501 int LLVMGetNumOperands(LLVMValueRef Val) {
502 Value *V = unwrap(Val);
503 if (MDNode *MD = dyn_cast<MDNode>(V))
504 return MD->getNumOperands();
505 return cast<User>(V)->getNumOperands();
508 /*--.. Operations on constants of any type .................................--*/
510 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
511 return wrap(Constant::getNullValue(unwrap(Ty)));
514 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
515 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
518 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
519 return wrap(UndefValue::get(unwrap(Ty)));
522 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
523 return isa<Constant>(unwrap(Ty));
526 LLVMBool LLVMIsNull(LLVMValueRef Val) {
527 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
528 return C->isNullValue();
532 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
533 return isa<UndefValue>(unwrap(Val));
536 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
538 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
541 /*--.. Operations on metadata nodes ........................................--*/
543 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
545 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
548 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
549 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
552 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
554 return wrap(MDNode::get(*unwrap(C),
555 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
558 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
559 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
562 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
563 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
564 *Len = S->getString().size();
565 return S->getString().data();
571 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
573 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
574 return N->getNumOperands();
579 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
581 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
584 for (unsigned i=0;i<N->getNumOperands();i++)
585 Dest[i] = wrap(N->getOperand(i));
588 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
591 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
594 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
599 /*--.. Operations on scalar constants ......................................--*/
601 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
602 LLVMBool SignExtend) {
603 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
606 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
608 const uint64_t Words[]) {
609 IntegerType *Ty = unwrap<IntegerType>(IntTy);
610 return wrap(ConstantInt::get(Ty->getContext(),
611 APInt(Ty->getBitWidth(),
612 makeArrayRef(Words, NumWords))));
615 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
617 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
621 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
622 unsigned SLen, uint8_t Radix) {
623 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
627 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
628 return wrap(ConstantFP::get(unwrap(RealTy), N));
631 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
632 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
635 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
637 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
640 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
641 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
644 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
645 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
648 /*--.. Operations on composite constants ...................................--*/
650 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
652 LLVMBool DontNullTerminate) {
653 /* Inverted the sense of AddNull because ', 0)' is a
654 better mnemonic for null termination than ', 1)'. */
655 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
656 DontNullTerminate == 0));
658 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
659 LLVMValueRef *ConstantVals,
660 unsigned Count, LLVMBool Packed) {
661 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
662 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
666 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
667 LLVMBool DontNullTerminate) {
668 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
671 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
672 LLVMValueRef *ConstantVals, unsigned Length) {
673 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
674 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
676 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
678 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
682 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
683 LLVMValueRef *ConstantVals,
685 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
686 StructType *Ty = cast<StructType>(unwrap(StructTy));
688 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
691 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
692 return wrap(ConstantVector::get(makeArrayRef(
693 unwrap<Constant>(ScalarConstantVals, Size), Size)));
696 /*-- Opcode mapping */
698 static LLVMOpcode map_to_llvmopcode(int opcode)
701 default: llvm_unreachable("Unhandled Opcode.");
702 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
703 #include "llvm/Instruction.def"
708 static int map_from_llvmopcode(LLVMOpcode code)
711 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
712 #include "llvm/Instruction.def"
715 llvm_unreachable("Unhandled Opcode.");
718 /*--.. Constant expressions ................................................--*/
720 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
721 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
724 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
725 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
728 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
729 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
732 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
733 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
736 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
737 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
740 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
741 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
745 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
746 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
749 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
750 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
753 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
755 unwrap<Constant>(RHSConstant)));
758 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
759 LLVMValueRef RHSConstant) {
760 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
761 unwrap<Constant>(RHSConstant)));
764 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
765 LLVMValueRef RHSConstant) {
766 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
767 unwrap<Constant>(RHSConstant)));
770 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
771 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
772 unwrap<Constant>(RHSConstant)));
775 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
776 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
777 unwrap<Constant>(RHSConstant)));
780 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
781 LLVMValueRef RHSConstant) {
782 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
783 unwrap<Constant>(RHSConstant)));
786 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
787 LLVMValueRef RHSConstant) {
788 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
789 unwrap<Constant>(RHSConstant)));
792 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
793 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
794 unwrap<Constant>(RHSConstant)));
797 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
798 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
799 unwrap<Constant>(RHSConstant)));
802 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
803 LLVMValueRef RHSConstant) {
804 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
805 unwrap<Constant>(RHSConstant)));
808 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
809 LLVMValueRef RHSConstant) {
810 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
811 unwrap<Constant>(RHSConstant)));
814 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
815 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
816 unwrap<Constant>(RHSConstant)));
819 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
820 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
821 unwrap<Constant>(RHSConstant)));
824 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
825 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
826 unwrap<Constant>(RHSConstant)));
829 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
830 LLVMValueRef RHSConstant) {
831 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
832 unwrap<Constant>(RHSConstant)));
835 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
836 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
837 unwrap<Constant>(RHSConstant)));
840 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
841 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
842 unwrap<Constant>(RHSConstant)));
845 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
846 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
847 unwrap<Constant>(RHSConstant)));
850 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
851 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
852 unwrap<Constant>(RHSConstant)));
855 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
856 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
857 unwrap<Constant>(RHSConstant)));
860 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
861 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
862 unwrap<Constant>(RHSConstant)));
865 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
866 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
867 unwrap<Constant>(RHSConstant)));
870 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
871 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
872 return wrap(ConstantExpr::getICmp(Predicate,
873 unwrap<Constant>(LHSConstant),
874 unwrap<Constant>(RHSConstant)));
877 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
878 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
879 return wrap(ConstantExpr::getFCmp(Predicate,
880 unwrap<Constant>(LHSConstant),
881 unwrap<Constant>(RHSConstant)));
884 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
885 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
886 unwrap<Constant>(RHSConstant)));
889 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
890 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
891 unwrap<Constant>(RHSConstant)));
894 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
895 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
896 unwrap<Constant>(RHSConstant)));
899 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
900 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
901 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
903 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
907 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
908 LLVMValueRef *ConstantIndices,
909 unsigned NumIndices) {
910 Constant* Val = unwrap<Constant>(ConstantVal);
911 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
913 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
916 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
917 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
921 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
922 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
926 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
927 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
931 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
932 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
936 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
937 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
941 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
942 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
946 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
947 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
951 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
952 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
956 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
957 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
961 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
962 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
966 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
967 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
971 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
972 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
976 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
977 LLVMTypeRef ToType) {
978 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
982 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
983 LLVMTypeRef ToType) {
984 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
988 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
989 LLVMTypeRef ToType) {
990 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
994 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
995 LLVMTypeRef ToType) {
996 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1000 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1001 LLVMBool isSigned) {
1002 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1003 unwrap(ToType), isSigned));
1006 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1007 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1011 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1012 LLVMValueRef ConstantIfTrue,
1013 LLVMValueRef ConstantIfFalse) {
1014 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1015 unwrap<Constant>(ConstantIfTrue),
1016 unwrap<Constant>(ConstantIfFalse)));
1019 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1020 LLVMValueRef IndexConstant) {
1021 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1022 unwrap<Constant>(IndexConstant)));
1025 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1026 LLVMValueRef ElementValueConstant,
1027 LLVMValueRef IndexConstant) {
1028 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1029 unwrap<Constant>(ElementValueConstant),
1030 unwrap<Constant>(IndexConstant)));
1033 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1034 LLVMValueRef VectorBConstant,
1035 LLVMValueRef MaskConstant) {
1036 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1037 unwrap<Constant>(VectorBConstant),
1038 unwrap<Constant>(MaskConstant)));
1041 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1043 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1044 makeArrayRef(IdxList, NumIdx)));
1047 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1048 LLVMValueRef ElementValueConstant,
1049 unsigned *IdxList, unsigned NumIdx) {
1050 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1051 unwrap<Constant>(ElementValueConstant),
1052 makeArrayRef(IdxList, NumIdx)));
1055 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1056 const char *Constraints,
1057 LLVMBool HasSideEffects,
1058 LLVMBool IsAlignStack,
1059 unsigned AsmDialect) {
1060 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1061 Constraints, HasSideEffects, IsAlignStack,
1065 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1066 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1069 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1071 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1072 return wrap(unwrap<GlobalValue>(Global)->getParent());
1075 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1076 return unwrap<GlobalValue>(Global)->isDeclaration();
1079 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1080 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1081 case GlobalValue::ExternalLinkage:
1082 return LLVMExternalLinkage;
1083 case GlobalValue::AvailableExternallyLinkage:
1084 return LLVMAvailableExternallyLinkage;
1085 case GlobalValue::LinkOnceAnyLinkage:
1086 return LLVMLinkOnceAnyLinkage;
1087 case GlobalValue::LinkOnceODRLinkage:
1088 return LLVMLinkOnceODRLinkage;
1089 case GlobalValue::LinkOnceODRAutoHideLinkage:
1090 return LLVMLinkOnceODRAutoHideLinkage;
1091 case GlobalValue::WeakAnyLinkage:
1092 return LLVMWeakAnyLinkage;
1093 case GlobalValue::WeakODRLinkage:
1094 return LLVMWeakODRLinkage;
1095 case GlobalValue::AppendingLinkage:
1096 return LLVMAppendingLinkage;
1097 case GlobalValue::InternalLinkage:
1098 return LLVMInternalLinkage;
1099 case GlobalValue::PrivateLinkage:
1100 return LLVMPrivateLinkage;
1101 case GlobalValue::LinkerPrivateLinkage:
1102 return LLVMLinkerPrivateLinkage;
1103 case GlobalValue::LinkerPrivateWeakLinkage:
1104 return LLVMLinkerPrivateWeakLinkage;
1105 case GlobalValue::DLLImportLinkage:
1106 return LLVMDLLImportLinkage;
1107 case GlobalValue::DLLExportLinkage:
1108 return LLVMDLLExportLinkage;
1109 case GlobalValue::ExternalWeakLinkage:
1110 return LLVMExternalWeakLinkage;
1111 case GlobalValue::CommonLinkage:
1112 return LLVMCommonLinkage;
1115 llvm_unreachable("Invalid GlobalValue linkage!");
1118 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1119 GlobalValue *GV = unwrap<GlobalValue>(Global);
1122 case LLVMExternalLinkage:
1123 GV->setLinkage(GlobalValue::ExternalLinkage);
1125 case LLVMAvailableExternallyLinkage:
1126 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1128 case LLVMLinkOnceAnyLinkage:
1129 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1131 case LLVMLinkOnceODRLinkage:
1132 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1134 case LLVMLinkOnceODRAutoHideLinkage:
1135 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1137 case LLVMWeakAnyLinkage:
1138 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1140 case LLVMWeakODRLinkage:
1141 GV->setLinkage(GlobalValue::WeakODRLinkage);
1143 case LLVMAppendingLinkage:
1144 GV->setLinkage(GlobalValue::AppendingLinkage);
1146 case LLVMInternalLinkage:
1147 GV->setLinkage(GlobalValue::InternalLinkage);
1149 case LLVMPrivateLinkage:
1150 GV->setLinkage(GlobalValue::PrivateLinkage);
1152 case LLVMLinkerPrivateLinkage:
1153 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1155 case LLVMLinkerPrivateWeakLinkage:
1156 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1158 case LLVMDLLImportLinkage:
1159 GV->setLinkage(GlobalValue::DLLImportLinkage);
1161 case LLVMDLLExportLinkage:
1162 GV->setLinkage(GlobalValue::DLLExportLinkage);
1164 case LLVMExternalWeakLinkage:
1165 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1167 case LLVMGhostLinkage:
1169 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1171 case LLVMCommonLinkage:
1172 GV->setLinkage(GlobalValue::CommonLinkage);
1177 const char *LLVMGetSection(LLVMValueRef Global) {
1178 return unwrap<GlobalValue>(Global)->getSection().c_str();
1181 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1182 unwrap<GlobalValue>(Global)->setSection(Section);
1185 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1186 return static_cast<LLVMVisibility>(
1187 unwrap<GlobalValue>(Global)->getVisibility());
1190 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1191 unwrap<GlobalValue>(Global)
1192 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1195 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1196 return unwrap<GlobalValue>(Global)->getAlignment();
1199 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1200 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1203 /*--.. Operations on global variables ......................................--*/
1205 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1206 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1207 GlobalValue::ExternalLinkage, 0, Name));
1210 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1212 unsigned AddressSpace) {
1213 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1214 GlobalValue::ExternalLinkage, 0, Name, 0,
1215 GlobalVariable::NotThreadLocal, AddressSpace));
1218 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1219 return wrap(unwrap(M)->getNamedGlobal(Name));
1222 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1223 Module *Mod = unwrap(M);
1224 Module::global_iterator I = Mod->global_begin();
1225 if (I == Mod->global_end())
1230 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1231 Module *Mod = unwrap(M);
1232 Module::global_iterator I = Mod->global_end();
1233 if (I == Mod->global_begin())
1238 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1239 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1240 Module::global_iterator I = GV;
1241 if (++I == GV->getParent()->global_end())
1246 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1247 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1248 Module::global_iterator I = GV;
1249 if (I == GV->getParent()->global_begin())
1254 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1255 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1258 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1259 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1260 if ( !GV->hasInitializer() )
1262 return wrap(GV->getInitializer());
1265 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1266 unwrap<GlobalVariable>(GlobalVar)
1267 ->setInitializer(unwrap<Constant>(ConstantVal));
1270 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1271 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1274 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1275 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1278 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1279 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1282 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1283 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1286 /*--.. Operations on aliases ......................................--*/
1288 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1290 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1291 unwrap<Constant>(Aliasee), unwrap (M)));
1294 /*--.. Operations on functions .............................................--*/
1296 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1297 LLVMTypeRef FunctionTy) {
1298 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1299 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1302 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1303 return wrap(unwrap(M)->getFunction(Name));
1306 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1307 Module *Mod = unwrap(M);
1308 Module::iterator I = Mod->begin();
1309 if (I == Mod->end())
1314 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1315 Module *Mod = unwrap(M);
1316 Module::iterator I = Mod->end();
1317 if (I == Mod->begin())
1322 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1323 Function *Func = unwrap<Function>(Fn);
1324 Module::iterator I = Func;
1325 if (++I == Func->getParent()->end())
1330 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1331 Function *Func = unwrap<Function>(Fn);
1332 Module::iterator I = Func;
1333 if (I == Func->getParent()->begin())
1338 void LLVMDeleteFunction(LLVMValueRef Fn) {
1339 unwrap<Function>(Fn)->eraseFromParent();
1342 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1343 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1344 return F->getIntrinsicID();
1348 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1349 return unwrap<Function>(Fn)->getCallingConv();
1352 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1353 return unwrap<Function>(Fn)->setCallingConv(
1354 static_cast<CallingConv::ID>(CC));
1357 const char *LLVMGetGC(LLVMValueRef Fn) {
1358 Function *F = unwrap<Function>(Fn);
1359 return F->hasGC()? F->getGC() : 0;
1362 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1363 Function *F = unwrap<Function>(Fn);
1370 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1371 Function *Func = unwrap<Function>(Fn);
1372 const AttrListPtr PAL = Func->getAttributes();
1373 const AttrListPtr PALnew = PAL.addAttr(~0U, Attributes(PA));
1374 Func->setAttributes(PALnew);
1377 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1378 Function *Func = unwrap<Function>(Fn);
1379 const AttrListPtr PAL = Func->getAttributes();
1380 const AttrListPtr PALnew = PAL.removeAttr(~0U, Attributes(PA));
1381 Func->setAttributes(PALnew);
1384 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1385 Function *Func = unwrap<Function>(Fn);
1386 const AttrListPtr PAL = Func->getAttributes();
1387 Attributes attr = PAL.getFnAttributes();
1388 return (LLVMAttribute)attr.Raw();
1391 /*--.. Operations on parameters ............................................--*/
1393 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1394 // This function is strictly redundant to
1395 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1396 return unwrap<Function>(FnRef)->arg_size();
1399 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1400 Function *Fn = unwrap<Function>(FnRef);
1401 for (Function::arg_iterator I = Fn->arg_begin(),
1402 E = Fn->arg_end(); I != E; I++)
1403 *ParamRefs++ = wrap(I);
1406 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1407 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1413 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1414 return wrap(unwrap<Argument>(V)->getParent());
1417 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1418 Function *Func = unwrap<Function>(Fn);
1419 Function::arg_iterator I = Func->arg_begin();
1420 if (I == Func->arg_end())
1425 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1426 Function *Func = unwrap<Function>(Fn);
1427 Function::arg_iterator I = Func->arg_end();
1428 if (I == Func->arg_begin())
1433 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1434 Argument *A = unwrap<Argument>(Arg);
1435 Function::arg_iterator I = A;
1436 if (++I == A->getParent()->arg_end())
1441 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1442 Argument *A = unwrap<Argument>(Arg);
1443 Function::arg_iterator I = A;
1444 if (I == A->getParent()->arg_begin())
1449 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1450 unwrap<Argument>(Arg)->addAttr(Attributes(PA));
1453 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1454 unwrap<Argument>(Arg)->removeAttr(Attributes(PA));
1457 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1458 Argument *A = unwrap<Argument>(Arg);
1459 Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1461 return (LLVMAttribute)attr.Raw();
1465 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1466 unwrap<Argument>(Arg)->addAttr(
1467 Attribute::constructAlignmentFromInt(align));
1470 /*--.. Operations on basic blocks ..........................................--*/
1472 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1473 return wrap(static_cast<Value*>(unwrap(BB)));
1476 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1477 return isa<BasicBlock>(unwrap(Val));
1480 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1481 return wrap(unwrap<BasicBlock>(Val));
1484 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1485 return wrap(unwrap(BB)->getParent());
1488 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1489 return wrap(unwrap(BB)->getTerminator());
1492 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1493 return unwrap<Function>(FnRef)->size();
1496 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1497 Function *Fn = unwrap<Function>(FnRef);
1498 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1499 *BasicBlocksRefs++ = wrap(I);
1502 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1503 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1506 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1507 Function *Func = unwrap<Function>(Fn);
1508 Function::iterator I = Func->begin();
1509 if (I == Func->end())
1514 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1515 Function *Func = unwrap<Function>(Fn);
1516 Function::iterator I = Func->end();
1517 if (I == Func->begin())
1522 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1523 BasicBlock *Block = unwrap(BB);
1524 Function::iterator I = Block;
1525 if (++I == Block->getParent()->end())
1530 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1531 BasicBlock *Block = unwrap(BB);
1532 Function::iterator I = Block;
1533 if (I == Block->getParent()->begin())
1538 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1541 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1544 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1545 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1548 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1549 LLVMBasicBlockRef BBRef,
1551 BasicBlock *BB = unwrap(BBRef);
1552 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1555 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1557 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1560 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1561 unwrap(BBRef)->eraseFromParent();
1564 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1565 unwrap(BBRef)->removeFromParent();
1568 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1569 unwrap(BB)->moveBefore(unwrap(MovePos));
1572 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1573 unwrap(BB)->moveAfter(unwrap(MovePos));
1576 /*--.. Operations on instructions ..........................................--*/
1578 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1579 return wrap(unwrap<Instruction>(Inst)->getParent());
1582 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1583 BasicBlock *Block = unwrap(BB);
1584 BasicBlock::iterator I = Block->begin();
1585 if (I == Block->end())
1590 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1591 BasicBlock *Block = unwrap(BB);
1592 BasicBlock::iterator I = Block->end();
1593 if (I == Block->begin())
1598 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1599 Instruction *Instr = unwrap<Instruction>(Inst);
1600 BasicBlock::iterator I = Instr;
1601 if (++I == Instr->getParent()->end())
1606 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1607 Instruction *Instr = unwrap<Instruction>(Inst);
1608 BasicBlock::iterator I = Instr;
1609 if (I == Instr->getParent()->begin())
1614 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1615 unwrap<Instruction>(Inst)->eraseFromParent();
1618 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1619 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1620 return (LLVMIntPredicate)I->getPredicate();
1621 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1622 if (CE->getOpcode() == Instruction::ICmp)
1623 return (LLVMIntPredicate)CE->getPredicate();
1624 return (LLVMIntPredicate)0;
1627 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1628 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1629 return map_to_llvmopcode(C->getOpcode());
1630 return (LLVMOpcode)0;
1633 /*--.. Call and invoke instructions ........................................--*/
1635 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1636 Value *V = unwrap(Instr);
1637 if (CallInst *CI = dyn_cast<CallInst>(V))
1638 return CI->getCallingConv();
1639 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1640 return II->getCallingConv();
1641 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1644 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1645 Value *V = unwrap(Instr);
1646 if (CallInst *CI = dyn_cast<CallInst>(V))
1647 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1648 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1649 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1650 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1653 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1655 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1657 Call.getAttributes().addAttr(index, Attributes(PA)));
1660 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1662 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1664 Call.getAttributes().removeAttr(index, Attributes(PA)));
1667 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1669 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1671 Call.getAttributes().addAttr(index,
1672 Attribute::constructAlignmentFromInt(align)));
1675 /*--.. Operations on call instructions (only) ..............................--*/
1677 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1678 return unwrap<CallInst>(Call)->isTailCall();
1681 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1682 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1685 /*--.. Operations on switch instructions (only) ............................--*/
1687 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1688 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1691 /*--.. Operations on phi nodes .............................................--*/
1693 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1694 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1695 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1696 for (unsigned I = 0; I != Count; ++I)
1697 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1700 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1701 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1704 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1705 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1708 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1709 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1713 /*===-- Instruction builders ----------------------------------------------===*/
1715 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1716 return wrap(new IRBuilder<>(*unwrap(C)));
1719 LLVMBuilderRef LLVMCreateBuilder(void) {
1720 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1723 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1724 LLVMValueRef Instr) {
1725 BasicBlock *BB = unwrap(Block);
1726 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1727 unwrap(Builder)->SetInsertPoint(BB, I);
1730 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1731 Instruction *I = unwrap<Instruction>(Instr);
1732 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1735 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1736 BasicBlock *BB = unwrap(Block);
1737 unwrap(Builder)->SetInsertPoint(BB);
1740 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1741 return wrap(unwrap(Builder)->GetInsertBlock());
1744 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1745 unwrap(Builder)->ClearInsertionPoint();
1748 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1749 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1752 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1754 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1757 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1758 delete unwrap(Builder);
1761 /*--.. Metadata builders ...................................................--*/
1763 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1764 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1765 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1768 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1769 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1770 .getAsMDNode(unwrap(Builder)->getContext()));
1773 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1774 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1778 /*--.. Instruction builders ................................................--*/
1780 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1781 return wrap(unwrap(B)->CreateRetVoid());
1784 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1785 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1788 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1790 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1793 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1794 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1797 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1798 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1799 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1802 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1803 LLVMBasicBlockRef Else, unsigned NumCases) {
1804 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1807 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1808 unsigned NumDests) {
1809 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1812 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1813 LLVMValueRef *Args, unsigned NumArgs,
1814 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1816 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1817 makeArrayRef(unwrap(Args), NumArgs),
1821 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1822 LLVMValueRef PersFn, unsigned NumClauses,
1824 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1825 cast<Function>(unwrap(PersFn)),
1829 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1830 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1833 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1834 return wrap(unwrap(B)->CreateUnreachable());
1837 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1838 LLVMBasicBlockRef Dest) {
1839 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1842 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1843 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1846 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1847 unwrap<LandingPadInst>(LandingPad)->
1848 addClause(cast<Constant>(unwrap(ClauseVal)));
1851 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1852 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1855 /*--.. Arithmetic ..........................................................--*/
1857 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1859 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1862 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1864 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1867 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1869 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1872 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1874 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1877 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1879 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1882 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1884 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1887 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1889 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1892 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1894 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1897 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1899 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1902 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1904 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1907 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1909 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1912 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1914 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1917 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1919 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1922 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1924 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1927 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1928 LLVMValueRef RHS, const char *Name) {
1929 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1932 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1934 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1937 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1939 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1942 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1944 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1947 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1949 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1952 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1954 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1957 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1959 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1962 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1964 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1967 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1969 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1972 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1974 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1977 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1979 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1982 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1983 LLVMValueRef LHS, LLVMValueRef RHS,
1985 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
1986 unwrap(RHS), Name));
1989 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1990 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1993 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1995 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1998 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2000 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2003 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2004 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2007 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2008 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2011 /*--.. Memory ..............................................................--*/
2013 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2015 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2016 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2017 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2018 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2019 ITy, unwrap(Ty), AllocSize,
2021 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2024 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2025 LLVMValueRef Val, const char *Name) {
2026 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2027 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2028 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2029 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2030 ITy, unwrap(Ty), AllocSize,
2031 unwrap(Val), 0, "");
2032 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2035 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2037 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2040 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2041 LLVMValueRef Val, const char *Name) {
2042 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2045 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2046 return wrap(unwrap(B)->Insert(
2047 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2051 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2053 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2056 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2057 LLVMValueRef PointerVal) {
2058 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2061 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2062 LLVMValueRef *Indices, unsigned NumIndices,
2064 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2065 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2068 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2069 LLVMValueRef *Indices, unsigned NumIndices,
2071 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2072 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2075 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2076 unsigned Idx, const char *Name) {
2077 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2080 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2082 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2085 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2087 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2090 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2091 Value *P = unwrap<Value>(MemAccessInst);
2092 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2093 return LI->isVolatile();
2094 return cast<StoreInst>(P)->isVolatile();
2097 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2098 Value *P = unwrap<Value>(MemAccessInst);
2099 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2100 return LI->setVolatile(isVolatile);
2101 return cast<StoreInst>(P)->setVolatile(isVolatile);
2104 /*--.. Casts ...............................................................--*/
2106 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2107 LLVMTypeRef DestTy, const char *Name) {
2108 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2111 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2112 LLVMTypeRef DestTy, const char *Name) {
2113 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2116 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2117 LLVMTypeRef DestTy, const char *Name) {
2118 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2121 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2122 LLVMTypeRef DestTy, const char *Name) {
2123 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2126 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2127 LLVMTypeRef DestTy, const char *Name) {
2128 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2131 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2132 LLVMTypeRef DestTy, const char *Name) {
2133 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2136 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2137 LLVMTypeRef DestTy, const char *Name) {
2138 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2141 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2142 LLVMTypeRef DestTy, const char *Name) {
2143 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2146 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2147 LLVMTypeRef DestTy, const char *Name) {
2148 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2151 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2152 LLVMTypeRef DestTy, const char *Name) {
2153 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2156 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2157 LLVMTypeRef DestTy, const char *Name) {
2158 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2161 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2162 LLVMTypeRef DestTy, const char *Name) {
2163 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2166 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2167 LLVMTypeRef DestTy, const char *Name) {
2168 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2172 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2173 LLVMTypeRef DestTy, const char *Name) {
2174 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2178 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2179 LLVMTypeRef DestTy, const char *Name) {
2180 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2184 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2185 LLVMTypeRef DestTy, const char *Name) {
2186 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2187 unwrap(DestTy), Name));
2190 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2191 LLVMTypeRef DestTy, const char *Name) {
2192 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2195 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2196 LLVMTypeRef DestTy, const char *Name) {
2197 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2198 /*isSigned*/true, Name));
2201 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2202 LLVMTypeRef DestTy, const char *Name) {
2203 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2206 /*--.. Comparisons .........................................................--*/
2208 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2209 LLVMValueRef LHS, LLVMValueRef RHS,
2211 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2212 unwrap(LHS), unwrap(RHS), Name));
2215 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2216 LLVMValueRef LHS, LLVMValueRef RHS,
2218 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2219 unwrap(LHS), unwrap(RHS), Name));
2222 /*--.. Miscellaneous instructions ..........................................--*/
2224 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2225 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2228 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2229 LLVMValueRef *Args, unsigned NumArgs,
2231 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2232 makeArrayRef(unwrap(Args), NumArgs),
2236 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2237 LLVMValueRef Then, LLVMValueRef Else,
2239 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2243 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2244 LLVMTypeRef Ty, const char *Name) {
2245 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2248 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2249 LLVMValueRef Index, const char *Name) {
2250 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2254 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2255 LLVMValueRef EltVal, LLVMValueRef Index,
2257 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2258 unwrap(Index), Name));
2261 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2262 LLVMValueRef V2, LLVMValueRef Mask,
2264 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2265 unwrap(Mask), Name));
2268 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2269 unsigned Index, const char *Name) {
2270 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2273 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2274 LLVMValueRef EltVal, unsigned Index,
2276 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2280 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2282 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2285 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2287 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2290 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2291 LLVMValueRef RHS, const char *Name) {
2292 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2296 /*===-- Module providers --------------------------------------------------===*/
2298 LLVMModuleProviderRef
2299 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2300 return reinterpret_cast<LLVMModuleProviderRef>(M);
2303 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2308 /*===-- Memory buffers ----------------------------------------------------===*/
2310 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2312 LLVMMemoryBufferRef *OutMemBuf,
2313 char **OutMessage) {
2315 OwningPtr<MemoryBuffer> MB;
2317 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2318 *OutMemBuf = wrap(MB.take());
2322 *OutMessage = strdup(ec.message().c_str());
2326 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2327 char **OutMessage) {
2328 OwningPtr<MemoryBuffer> MB;
2330 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2331 *OutMemBuf = wrap(MB.take());
2335 *OutMessage = strdup(ec.message().c_str());
2339 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2340 delete unwrap(MemBuf);
2343 /*===-- Pass Registry -----------------------------------------------------===*/
2345 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2346 return wrap(PassRegistry::getPassRegistry());
2349 /*===-- Pass Manager ------------------------------------------------------===*/
2351 LLVMPassManagerRef LLVMCreatePassManager() {
2352 return wrap(new PassManager());
2355 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2356 return wrap(new FunctionPassManager(unwrap(M)));
2359 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2360 return LLVMCreateFunctionPassManagerForModule(
2361 reinterpret_cast<LLVMModuleRef>(P));
2364 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2365 return unwrap<PassManager>(PM)->run(*unwrap(M));
2368 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2369 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2372 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2373 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2376 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2377 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2380 void LLVMDisposePassManager(LLVMPassManagerRef PM) {