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 C bindings for libLLVMCore.a, which implements
11 // 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/TypeSymbolTable.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/CallSite.h"
31 /*===-- Error handling ----------------------------------------------------===*/
33 void LLVMDisposeMessage(char *Message) {
38 /*===-- Operations on modules ---------------------------------------------===*/
40 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
41 return wrap(new Module(ModuleID));
44 void LLVMDisposeModule(LLVMModuleRef M) {
48 /*--.. Data layout .........................................................--*/
49 const char * LLVMGetDataLayout(LLVMModuleRef M) {
50 return unwrap(M)->getDataLayout().c_str();
53 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
54 unwrap(M)->setDataLayout(Triple);
57 /*--.. Target triple .......................................................--*/
58 const char * LLVMGetTarget(LLVMModuleRef M) {
59 return unwrap(M)->getTargetTriple().c_str();
62 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
63 unwrap(M)->setTargetTriple(Triple);
66 /*--.. Type names ..........................................................--*/
67 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
68 return unwrap(M)->addTypeName(Name, unwrap(Ty));
71 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
74 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
75 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
80 void LLVMDumpModule(LLVMModuleRef M) {
85 /*===-- Operations on types -----------------------------------------------===*/
87 /*--.. Operations on all types (mostly) ....................................--*/
89 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
90 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
93 /*--.. Operations on integer types .........................................--*/
95 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
96 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
97 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
98 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
99 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
101 LLVMTypeRef LLVMIntType(unsigned NumBits) {
102 return wrap(IntegerType::get(NumBits));
105 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
106 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
109 /*--.. Operations on real types ............................................--*/
111 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
112 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
113 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
114 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
115 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
117 /*--.. Operations on function types ........................................--*/
119 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
120 LLVMTypeRef *ParamTypes, unsigned ParamCount,
122 std::vector<const Type*> Tys;
123 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
124 Tys.push_back(unwrap(*I));
126 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
129 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
130 return unwrap<FunctionType>(FunctionTy)->isVarArg();
133 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
134 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
137 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
138 return unwrap<FunctionType>(FunctionTy)->getNumParams();
141 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
142 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
143 for (FunctionType::param_iterator I = Ty->param_begin(),
144 E = Ty->param_end(); I != E; ++I)
148 /*--.. Operations on struct types ..........................................--*/
150 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
151 unsigned ElementCount, int Packed) {
152 std::vector<const Type*> Tys;
153 for (LLVMTypeRef *I = ElementTypes,
154 *E = ElementTypes + ElementCount; I != E; ++I)
155 Tys.push_back(unwrap(*I));
157 return wrap(StructType::get(Tys, Packed != 0));
160 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
161 return unwrap<StructType>(StructTy)->getNumElements();
164 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
165 StructType *Ty = unwrap<StructType>(StructTy);
166 for (FunctionType::param_iterator I = Ty->element_begin(),
167 E = Ty->element_end(); I != E; ++I)
171 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
172 return unwrap<StructType>(StructTy)->isPacked();
175 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
177 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
178 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
181 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
182 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
185 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
186 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
189 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
190 return wrap(unwrap<SequentialType>(Ty)->getElementType());
193 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
194 return unwrap<ArrayType>(ArrayTy)->getNumElements();
197 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
198 return unwrap<PointerType>(PointerTy)->getAddressSpace();
201 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
202 return unwrap<VectorType>(VectorTy)->getNumElements();
205 /*--.. Operations on other types ...........................................--*/
207 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
208 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
210 LLVMTypeRef LLVMOpaqueType(void) {
211 return wrap(llvm::OpaqueType::get());
214 /*--.. Operations on type handles ..........................................--*/
216 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
217 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
220 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
221 delete unwrap(TypeHandle);
224 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
225 return wrap(unwrap(TypeHandle)->get());
228 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
229 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
233 /*===-- Operations on values ----------------------------------------------===*/
235 /*--.. Operations on all values ............................................--*/
237 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
238 return wrap(unwrap(Val)->getType());
241 const char *LLVMGetValueName(LLVMValueRef Val) {
242 return unwrap(Val)->getNameStart();
245 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
246 unwrap(Val)->setName(Name);
249 void LLVMDumpValue(LLVMValueRef Val) {
253 /*--.. Operations on constants of any type .................................--*/
255 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
256 return wrap(Constant::getNullValue(unwrap(Ty)));
259 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
260 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
263 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
264 return wrap(UndefValue::get(unwrap(Ty)));
267 int LLVMIsConstant(LLVMValueRef Ty) {
268 return isa<Constant>(unwrap(Ty));
271 int LLVMIsNull(LLVMValueRef Val) {
272 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
273 return C->isNullValue();
277 int LLVMIsUndef(LLVMValueRef Val) {
278 return isa<UndefValue>(unwrap(Val));
281 /*--.. Operations on scalar constants ......................................--*/
283 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
285 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
288 static const fltSemantics &SemanticsForType(Type *Ty) {
289 assert(Ty->isFloatingPoint() && "Type is not floating point!");
290 if (Ty == Type::FloatTy)
291 return APFloat::IEEEsingle;
292 if (Ty == Type::DoubleTy)
293 return APFloat::IEEEdouble;
294 if (Ty == Type::X86_FP80Ty)
295 return APFloat::x87DoubleExtended;
296 if (Ty == Type::FP128Ty)
297 return APFloat::IEEEquad;
298 if (Ty == Type::PPC_FP128Ty)
299 return APFloat::PPCDoubleDouble;
300 return APFloat::Bogus;
303 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
306 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
308 return wrap(ConstantFP::get(APN));
311 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
312 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
315 /*--.. Operations on composite constants ...................................--*/
317 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
318 int DontNullTerminate) {
319 /* Inverted the sense of AddNull because ', 0)' is a
320 better mnemonic for null termination than ', 1)'. */
321 return wrap(ConstantArray::get(std::string(Str, Length),
322 DontNullTerminate == 0));
325 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
326 LLVMValueRef *ConstantVals, unsigned Length) {
327 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
328 unwrap<Constant>(ConstantVals, Length),
332 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
334 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
335 Count, Packed != 0));
338 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
339 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
343 /*--.. Constant expressions ................................................--*/
345 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
346 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
349 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
350 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
353 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
354 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
357 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
358 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
359 unwrap<Constant>(RHSConstant)));
362 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
363 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
364 unwrap<Constant>(RHSConstant)));
367 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
368 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
369 unwrap<Constant>(RHSConstant)));
372 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
373 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
374 unwrap<Constant>(RHSConstant)));
377 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
378 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
379 unwrap<Constant>(RHSConstant)));
382 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
383 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
384 unwrap<Constant>(RHSConstant)));
387 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
388 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
389 unwrap<Constant>(RHSConstant)));
392 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
393 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
394 unwrap<Constant>(RHSConstant)));
397 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
398 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
399 unwrap<Constant>(RHSConstant)));
402 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
403 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
404 unwrap<Constant>(RHSConstant)));
407 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
408 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
409 unwrap<Constant>(RHSConstant)));
412 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
413 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
414 unwrap<Constant>(RHSConstant)));
417 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
418 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
419 return wrap(ConstantExpr::getICmp(Predicate,
420 unwrap<Constant>(LHSConstant),
421 unwrap<Constant>(RHSConstant)));
424 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
425 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
426 return wrap(ConstantExpr::getFCmp(Predicate,
427 unwrap<Constant>(LHSConstant),
428 unwrap<Constant>(RHSConstant)));
431 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
432 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
433 unwrap<Constant>(RHSConstant)));
436 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
437 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
438 unwrap<Constant>(RHSConstant)));
441 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
442 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
443 unwrap<Constant>(RHSConstant)));
446 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
447 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
448 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
449 unwrap<Constant>(ConstantIndices,
454 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
455 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
459 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
460 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
464 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
465 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
469 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
470 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
474 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
475 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
479 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
480 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
484 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
485 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
489 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
490 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
494 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
495 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
499 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
500 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
504 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
505 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
509 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
510 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
514 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
515 LLVMValueRef ConstantIfTrue,
516 LLVMValueRef ConstantIfFalse) {
517 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
518 unwrap<Constant>(ConstantIfTrue),
519 unwrap<Constant>(ConstantIfFalse)));
522 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
523 LLVMValueRef IndexConstant) {
524 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
525 unwrap<Constant>(IndexConstant)));
528 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
529 LLVMValueRef ElementValueConstant,
530 LLVMValueRef IndexConstant) {
531 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
532 unwrap<Constant>(ElementValueConstant),
533 unwrap<Constant>(IndexConstant)));
536 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
537 LLVMValueRef VectorBConstant,
538 LLVMValueRef MaskConstant) {
539 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
540 unwrap<Constant>(VectorBConstant),
541 unwrap<Constant>(MaskConstant)));
544 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
546 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
547 return wrap(unwrap<GlobalValue>(Global)->getParent());
550 int LLVMIsDeclaration(LLVMValueRef Global) {
551 return unwrap<GlobalValue>(Global)->isDeclaration();
554 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
555 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
558 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
559 unwrap<GlobalValue>(Global)
560 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
563 const char *LLVMGetSection(LLVMValueRef Global) {
564 return unwrap<GlobalValue>(Global)->getSection().c_str();
567 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
568 unwrap<GlobalValue>(Global)->setSection(Section);
571 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
572 return static_cast<LLVMVisibility>(
573 unwrap<GlobalValue>(Global)->getVisibility());
576 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
577 unwrap<GlobalValue>(Global)
578 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
581 unsigned LLVMGetAlignment(LLVMValueRef Global) {
582 return unwrap<GlobalValue>(Global)->getAlignment();
585 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
586 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
589 /*--.. Operations on global variables ......................................--*/
591 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
592 return wrap(new GlobalVariable(unwrap(Ty), false,
593 GlobalValue::ExternalLinkage, 0, Name,
597 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
598 return wrap(unwrap(M)->getNamedGlobal(Name));
601 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
602 Module *Mod = unwrap(M);
603 Module::global_iterator I = Mod->global_begin();
604 if (I == Mod->global_end())
609 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
610 Module *Mod = unwrap(M);
611 Module::global_iterator I = Mod->global_end();
612 if (I == Mod->global_begin())
617 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
618 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
619 Module::global_iterator I = GV;
620 if (++I == GV->getParent()->global_end())
625 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
626 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
627 Module::global_iterator I = GV;
628 if (I == GV->getParent()->global_begin())
633 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
634 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
637 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
638 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
641 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
642 unwrap<GlobalVariable>(GlobalVar)
643 ->setInitializer(unwrap<Constant>(ConstantVal));
646 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
647 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
650 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
651 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
654 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
655 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
658 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
659 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
662 /*--.. Operations on functions .............................................--*/
664 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
665 LLVMTypeRef FunctionTy) {
666 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
667 GlobalValue::ExternalLinkage, Name, unwrap(M)));
670 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
671 return wrap(unwrap(M)->getFunction(Name));
674 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
675 Module *Mod = unwrap(M);
676 Module::iterator I = Mod->begin();
682 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
683 Module *Mod = unwrap(M);
684 Module::iterator I = Mod->end();
685 if (I == Mod->begin())
690 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
691 Function *Func = unwrap<Function>(Fn);
692 Module::iterator I = Func;
693 if (++I == Func->getParent()->end())
698 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
699 Function *Func = unwrap<Function>(Fn);
700 Module::iterator I = Func;
701 if (I == Func->getParent()->begin())
706 void LLVMDeleteFunction(LLVMValueRef Fn) {
707 unwrap<Function>(Fn)->eraseFromParent();
710 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
711 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
712 return F->getIntrinsicID();
716 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
717 return unwrap<Function>(Fn)->getCallingConv();
720 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
721 return unwrap<Function>(Fn)->setCallingConv(CC);
724 const char *LLVMGetGC(LLVMValueRef Fn) {
725 Function *F = unwrap<Function>(Fn);
726 return F->hasGC()? F->getGC() : 0;
729 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
730 Function *F = unwrap<Function>(Fn);
737 /*--.. Operations on parameters ............................................--*/
739 unsigned LLVMCountParams(LLVMValueRef FnRef) {
740 // This function is strictly redundant to
741 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
742 return unwrap<Function>(FnRef)->arg_size();
745 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
746 Function *Fn = unwrap<Function>(FnRef);
747 for (Function::arg_iterator I = Fn->arg_begin(),
748 E = Fn->arg_end(); I != E; I++)
749 *ParamRefs++ = wrap(I);
752 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
753 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
759 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
760 return wrap(unwrap<Argument>(V)->getParent());
763 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
764 Function *Func = unwrap<Function>(Fn);
765 Function::arg_iterator I = Func->arg_begin();
766 if (I == Func->arg_end())
771 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
772 Function *Func = unwrap<Function>(Fn);
773 Function::arg_iterator I = Func->arg_end();
774 if (I == Func->arg_begin())
779 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
780 Argument *A = unwrap<Argument>(Arg);
781 Function::arg_iterator I = A;
782 if (++I == A->getParent()->arg_end())
787 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
788 Argument *A = unwrap<Argument>(Arg);
789 Function::arg_iterator I = A;
790 if (I == A->getParent()->arg_begin())
795 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
796 unwrap<Argument>(Arg)->addAttr(PA);
799 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
800 unwrap<Argument>(Arg)->removeAttr(PA);
803 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
804 unwrap<Argument>(Arg)->addAttr(
805 Attribute::constructAlignmentFromInt(align));
808 /*--.. Operations on basic blocks ..........................................--*/
810 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
811 return wrap(static_cast<Value*>(unwrap(BB)));
814 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
815 return isa<BasicBlock>(unwrap(Val));
818 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
819 return wrap(unwrap<BasicBlock>(Val));
822 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
823 return wrap(unwrap(BB)->getParent());
826 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
827 return unwrap<Function>(FnRef)->size();
830 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
831 Function *Fn = unwrap<Function>(FnRef);
832 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
833 *BasicBlocksRefs++ = wrap(I);
836 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
837 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
840 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
841 Function *Func = unwrap<Function>(Fn);
842 Function::iterator I = Func->begin();
843 if (I == Func->end())
848 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
849 Function *Func = unwrap<Function>(Fn);
850 Function::iterator I = Func->end();
851 if (I == Func->begin())
856 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
857 BasicBlock *Block = unwrap(BB);
858 Function::iterator I = Block;
859 if (++I == Block->getParent()->end())
864 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
865 BasicBlock *Block = unwrap(BB);
866 Function::iterator I = Block;
867 if (I == Block->getParent()->begin())
872 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
873 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
876 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
878 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
879 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
883 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
884 unwrap(BBRef)->eraseFromParent();
887 /*--.. Operations on instructions ..........................................--*/
889 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
890 return wrap(unwrap<Instruction>(Inst)->getParent());
893 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
894 BasicBlock *Block = unwrap(BB);
895 BasicBlock::iterator I = Block->begin();
896 if (I == Block->end())
901 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
902 BasicBlock *Block = unwrap(BB);
903 BasicBlock::iterator I = Block->end();
904 if (I == Block->begin())
909 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
910 Instruction *Instr = unwrap<Instruction>(Inst);
911 BasicBlock::iterator I = Instr;
912 if (++I == Instr->getParent()->end())
917 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
918 Instruction *Instr = unwrap<Instruction>(Inst);
919 BasicBlock::iterator I = Instr;
920 if (I == Instr->getParent()->begin())
925 /*--.. Call and invoke instructions ........................................--*/
927 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
928 Value *V = unwrap(Instr);
929 if (CallInst *CI = dyn_cast<CallInst>(V))
930 return CI->getCallingConv();
931 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
932 return II->getCallingConv();
933 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
937 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
938 Value *V = unwrap(Instr);
939 if (CallInst *CI = dyn_cast<CallInst>(V))
940 return CI->setCallingConv(CC);
941 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
942 return II->setCallingConv(CC);
943 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
946 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
948 CallSite Call = CallSite(unwrap<Instruction>(Instr));
950 Call.getAttributes().addAttr(index, PA));
953 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
955 CallSite Call = CallSite(unwrap<Instruction>(Instr));
957 Call.getAttributes().removeAttr(index, PA));
960 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
962 CallSite Call = CallSite(unwrap<Instruction>(Instr));
964 Call.getAttributes().addAttr(index,
965 Attribute::constructAlignmentFromInt(align)));
968 /*--.. Operations on call instructions (only) ..............................--*/
970 int LLVMIsTailCall(LLVMValueRef Call) {
971 return unwrap<CallInst>(Call)->isTailCall();
974 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
975 unwrap<CallInst>(Call)->setTailCall(isTailCall);
978 /*--.. Operations on phi nodes .............................................--*/
980 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
981 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
982 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
983 for (unsigned I = 0; I != Count; ++I)
984 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
987 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
988 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
991 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
992 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
995 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
996 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1000 /*===-- Instruction builders ----------------------------------------------===*/
1002 LLVMBuilderRef LLVMCreateBuilder(void) {
1003 return wrap(new IRBuilder<>());
1006 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1007 LLVMValueRef Instr) {
1008 BasicBlock *BB = unwrap(Block);
1009 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1010 unwrap(Builder)->SetInsertPoint(BB, I);
1013 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1014 Instruction *I = unwrap<Instruction>(Instr);
1015 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1018 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1019 BasicBlock *BB = unwrap(Block);
1020 unwrap(Builder)->SetInsertPoint(BB);
1023 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1024 return wrap(unwrap(Builder)->GetInsertBlock());
1027 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1028 delete unwrap(Builder);
1031 /*--.. Instruction builders ................................................--*/
1033 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1034 return wrap(unwrap(B)->CreateRetVoid());
1037 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1038 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1041 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1042 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1045 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1046 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1047 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1050 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1051 LLVMBasicBlockRef Else, unsigned NumCases) {
1052 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1055 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1056 LLVMValueRef *Args, unsigned NumArgs,
1057 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1059 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1060 unwrap(Args), unwrap(Args) + NumArgs,
1064 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1065 return wrap(unwrap(B)->CreateUnwind());
1068 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1069 return wrap(unwrap(B)->CreateUnreachable());
1072 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1073 LLVMBasicBlockRef Dest) {
1074 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1077 /*--.. Arithmetic ..........................................................--*/
1079 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1081 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1084 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1086 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1089 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1091 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1094 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1096 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1099 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1101 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1104 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1106 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1109 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1111 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1114 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1116 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1119 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1121 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1124 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1126 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1129 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1131 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1134 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1136 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1139 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1141 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1144 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1146 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1149 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1151 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1154 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1155 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1158 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1159 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1162 /*--.. Memory ..............................................................--*/
1164 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1166 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1169 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1170 LLVMValueRef Val, const char *Name) {
1171 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1174 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1176 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1179 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1180 LLVMValueRef Val, const char *Name) {
1181 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1184 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1185 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1189 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1191 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1194 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1195 LLVMValueRef PointerVal) {
1196 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1199 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1200 LLVMValueRef *Indices, unsigned NumIndices,
1202 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1203 unwrap(Indices) + NumIndices, Name));
1206 /*--.. Casts ...............................................................--*/
1208 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1209 LLVMTypeRef DestTy, const char *Name) {
1210 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1213 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1214 LLVMTypeRef DestTy, const char *Name) {
1215 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1218 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1219 LLVMTypeRef DestTy, const char *Name) {
1220 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1223 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1224 LLVMTypeRef DestTy, const char *Name) {
1225 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1228 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1229 LLVMTypeRef DestTy, const char *Name) {
1230 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1233 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1234 LLVMTypeRef DestTy, const char *Name) {
1235 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1238 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1239 LLVMTypeRef DestTy, const char *Name) {
1240 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1243 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1244 LLVMTypeRef DestTy, const char *Name) {
1245 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1248 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1249 LLVMTypeRef DestTy, const char *Name) {
1250 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1253 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1254 LLVMTypeRef DestTy, const char *Name) {
1255 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1258 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1259 LLVMTypeRef DestTy, const char *Name) {
1260 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1263 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1264 LLVMTypeRef DestTy, const char *Name) {
1265 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1268 /*--.. Comparisons .........................................................--*/
1270 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1271 LLVMValueRef LHS, LLVMValueRef RHS,
1273 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1274 unwrap(LHS), unwrap(RHS), Name));
1277 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1278 LLVMValueRef LHS, LLVMValueRef RHS,
1280 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1281 unwrap(LHS), unwrap(RHS), Name));
1284 /*--.. Miscellaneous instructions ..........................................--*/
1286 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1287 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1290 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1291 LLVMValueRef *Args, unsigned NumArgs,
1293 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1294 unwrap(Args) + NumArgs, Name));
1297 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1298 LLVMValueRef Then, LLVMValueRef Else,
1300 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1304 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1305 LLVMTypeRef Ty, const char *Name) {
1306 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1309 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1310 LLVMValueRef Index, const char *Name) {
1311 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1315 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1316 LLVMValueRef EltVal, LLVMValueRef Index,
1318 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1319 unwrap(Index), Name));
1322 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1323 LLVMValueRef V2, LLVMValueRef Mask,
1325 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1326 unwrap(Mask), Name));
1330 /*===-- Module providers --------------------------------------------------===*/
1332 LLVMModuleProviderRef
1333 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1334 return wrap(new ExistingModuleProvider(unwrap(M)));
1337 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1342 /*===-- Memory buffers ----------------------------------------------------===*/
1344 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1345 LLVMMemoryBufferRef *OutMemBuf,
1346 char **OutMessage) {
1348 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1349 *OutMemBuf = wrap(MB);
1353 *OutMessage = strdup(Error.c_str());
1357 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1358 char **OutMessage) {
1359 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1360 *OutMemBuf = wrap(MB);
1364 *OutMessage = strdup("stdin is empty.");
1368 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1369 delete unwrap(MemBuf);