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/GlobalAlias.h"
21 #include "llvm/TypeSymbolTable.h"
22 #include "llvm/ModuleProvider.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/CallSite.h"
33 /*===-- Error handling ----------------------------------------------------===*/
35 void LLVMDisposeMessage(char *Message) {
40 /*===-- Operations on modules ---------------------------------------------===*/
42 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
43 return wrap(new Module(ModuleID));
46 void LLVMDisposeModule(LLVMModuleRef M) {
50 /*--.. Data layout .........................................................--*/
51 const char * LLVMGetDataLayout(LLVMModuleRef M) {
52 return unwrap(M)->getDataLayout().c_str();
55 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
56 unwrap(M)->setDataLayout(Triple);
59 /*--.. Target triple .......................................................--*/
60 const char * LLVMGetTarget(LLVMModuleRef M) {
61 return unwrap(M)->getTargetTriple().c_str();
64 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
65 unwrap(M)->setTargetTriple(Triple);
68 /*--.. Type names ..........................................................--*/
69 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
70 return unwrap(M)->addTypeName(Name, unwrap(Ty));
73 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
76 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
77 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
82 void LLVMDumpModule(LLVMModuleRef M) {
87 /*===-- Operations on types -----------------------------------------------===*/
89 /*--.. Operations on all types (mostly) ....................................--*/
91 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
92 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
95 /*--.. Operations on integer types .........................................--*/
97 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
98 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
99 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
100 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
101 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
103 LLVMTypeRef LLVMIntType(unsigned NumBits) {
104 return wrap(IntegerType::get(NumBits));
107 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
108 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
111 /*--.. Operations on real types ............................................--*/
113 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
114 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
115 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
116 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
117 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
119 /*--.. Operations on function types ........................................--*/
121 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
122 LLVMTypeRef *ParamTypes, unsigned ParamCount,
124 std::vector<const Type*> Tys;
125 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
126 Tys.push_back(unwrap(*I));
128 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
131 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
132 return unwrap<FunctionType>(FunctionTy)->isVarArg();
135 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
136 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
139 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
140 return unwrap<FunctionType>(FunctionTy)->getNumParams();
143 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
144 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
145 for (FunctionType::param_iterator I = Ty->param_begin(),
146 E = Ty->param_end(); I != E; ++I)
150 /*--.. Operations on struct types ..........................................--*/
152 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
153 unsigned ElementCount, int Packed) {
154 std::vector<const Type*> Tys;
155 for (LLVMTypeRef *I = ElementTypes,
156 *E = ElementTypes + ElementCount; I != E; ++I)
157 Tys.push_back(unwrap(*I));
159 return wrap(StructType::get(Tys, Packed != 0));
162 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
163 return unwrap<StructType>(StructTy)->getNumElements();
166 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
167 StructType *Ty = unwrap<StructType>(StructTy);
168 for (FunctionType::param_iterator I = Ty->element_begin(),
169 E = Ty->element_end(); I != E; ++I)
173 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
174 return unwrap<StructType>(StructTy)->isPacked();
177 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
179 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
180 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
183 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
184 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
187 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
188 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
191 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
192 return wrap(unwrap<SequentialType>(Ty)->getElementType());
195 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
196 return unwrap<ArrayType>(ArrayTy)->getNumElements();
199 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
200 return unwrap<PointerType>(PointerTy)->getAddressSpace();
203 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
204 return unwrap<VectorType>(VectorTy)->getNumElements();
207 /*--.. Operations on other types ...........................................--*/
209 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
210 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
212 LLVMTypeRef LLVMOpaqueType(void) {
213 return wrap(llvm::OpaqueType::get());
216 /*--.. Operations on type handles ..........................................--*/
218 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
219 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
222 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
223 delete unwrap(TypeHandle);
226 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
227 return wrap(unwrap(TypeHandle)->get());
230 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
231 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
235 /*===-- Operations on values ----------------------------------------------===*/
237 /*--.. Operations on all values ............................................--*/
239 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
240 return wrap(unwrap(Val)->getType());
243 const char *LLVMGetValueName(LLVMValueRef Val) {
244 return unwrap(Val)->getNameStart();
247 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
248 unwrap(Val)->setName(Name);
251 void LLVMDumpValue(LLVMValueRef Val) {
255 /*--.. Operations on constants of any type .................................--*/
257 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
258 return wrap(Constant::getNullValue(unwrap(Ty)));
261 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
262 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
265 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
266 return wrap(UndefValue::get(unwrap(Ty)));
269 int LLVMIsConstant(LLVMValueRef Ty) {
270 return isa<Constant>(unwrap(Ty));
273 int LLVMIsNull(LLVMValueRef Val) {
274 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
275 return C->isNullValue();
279 int LLVMIsUndef(LLVMValueRef Val) {
280 return isa<UndefValue>(unwrap(Val));
283 /*--.. Operations on scalar constants ......................................--*/
285 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
287 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
290 static const fltSemantics &SemanticsForType(Type *Ty) {
291 assert(Ty->isFloatingPoint() && "Type is not floating point!");
292 if (Ty == Type::FloatTy)
293 return APFloat::IEEEsingle;
294 if (Ty == Type::DoubleTy)
295 return APFloat::IEEEdouble;
296 if (Ty == Type::X86_FP80Ty)
297 return APFloat::x87DoubleExtended;
298 if (Ty == Type::FP128Ty)
299 return APFloat::IEEEquad;
300 if (Ty == Type::PPC_FP128Ty)
301 return APFloat::PPCDoubleDouble;
302 return APFloat::Bogus;
305 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
308 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
310 return wrap(ConstantFP::get(APN));
313 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
314 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
317 /*--.. Operations on composite constants ...................................--*/
319 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
320 int DontNullTerminate) {
321 /* Inverted the sense of AddNull because ', 0)' is a
322 better mnemonic for null termination than ', 1)'. */
323 return wrap(ConstantArray::get(std::string(Str, Length),
324 DontNullTerminate == 0));
327 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
328 LLVMValueRef *ConstantVals, unsigned Length) {
329 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
330 unwrap<Constant>(ConstantVals, Length),
334 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
336 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
337 Count, Packed != 0));
340 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
341 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
345 /*--.. Constant expressions ................................................--*/
347 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
348 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
351 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
352 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
355 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
356 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
359 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
360 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
361 unwrap<Constant>(RHSConstant)));
364 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
365 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
366 unwrap<Constant>(RHSConstant)));
369 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
370 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
371 unwrap<Constant>(RHSConstant)));
374 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
375 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
376 unwrap<Constant>(RHSConstant)));
379 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
380 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
381 unwrap<Constant>(RHSConstant)));
384 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
385 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
386 unwrap<Constant>(RHSConstant)));
389 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
390 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
391 unwrap<Constant>(RHSConstant)));
394 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
395 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
396 unwrap<Constant>(RHSConstant)));
399 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
400 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
401 unwrap<Constant>(RHSConstant)));
404 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
405 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
406 unwrap<Constant>(RHSConstant)));
409 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
410 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
411 unwrap<Constant>(RHSConstant)));
414 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
415 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
416 unwrap<Constant>(RHSConstant)));
419 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
420 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
421 return wrap(ConstantExpr::getICmp(Predicate,
422 unwrap<Constant>(LHSConstant),
423 unwrap<Constant>(RHSConstant)));
426 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
427 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
428 return wrap(ConstantExpr::getFCmp(Predicate,
429 unwrap<Constant>(LHSConstant),
430 unwrap<Constant>(RHSConstant)));
433 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
434 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
435 unwrap<Constant>(RHSConstant)));
438 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
439 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
440 unwrap<Constant>(RHSConstant)));
443 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
444 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
445 unwrap<Constant>(RHSConstant)));
448 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
449 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
450 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
451 unwrap<Constant>(ConstantIndices,
456 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
457 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
461 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
462 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
466 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
467 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
471 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
472 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
476 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
477 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
481 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
482 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
486 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
487 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
491 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
492 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
496 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
497 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
501 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
502 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
506 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
507 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
511 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
512 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
516 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
517 LLVMValueRef ConstantIfTrue,
518 LLVMValueRef ConstantIfFalse) {
519 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
520 unwrap<Constant>(ConstantIfTrue),
521 unwrap<Constant>(ConstantIfFalse)));
524 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
525 LLVMValueRef IndexConstant) {
526 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
527 unwrap<Constant>(IndexConstant)));
530 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
531 LLVMValueRef ElementValueConstant,
532 LLVMValueRef IndexConstant) {
533 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
534 unwrap<Constant>(ElementValueConstant),
535 unwrap<Constant>(IndexConstant)));
538 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
539 LLVMValueRef VectorBConstant,
540 LLVMValueRef MaskConstant) {
541 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
542 unwrap<Constant>(VectorBConstant),
543 unwrap<Constant>(MaskConstant)));
546 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
548 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
552 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
553 LLVMValueRef ElementValueConstant,
554 unsigned *IdxList, unsigned NumIdx) {
555 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
556 unwrap<Constant>(ElementValueConstant),
560 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
561 const char *Constraints, int HasSideEffects) {
562 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
563 Constraints, HasSideEffects));
566 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
568 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
569 return wrap(unwrap<GlobalValue>(Global)->getParent());
572 int LLVMIsDeclaration(LLVMValueRef Global) {
573 return unwrap<GlobalValue>(Global)->isDeclaration();
576 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
577 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
580 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
581 unwrap<GlobalValue>(Global)
582 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
585 const char *LLVMGetSection(LLVMValueRef Global) {
586 return unwrap<GlobalValue>(Global)->getSection().c_str();
589 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
590 unwrap<GlobalValue>(Global)->setSection(Section);
593 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
594 return static_cast<LLVMVisibility>(
595 unwrap<GlobalValue>(Global)->getVisibility());
598 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
599 unwrap<GlobalValue>(Global)
600 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
603 unsigned LLVMGetAlignment(LLVMValueRef Global) {
604 return unwrap<GlobalValue>(Global)->getAlignment();
607 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
608 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
611 /*--.. Operations on global variables ......................................--*/
613 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
614 return wrap(new GlobalVariable(unwrap(Ty), false,
615 GlobalValue::ExternalLinkage, 0, Name,
619 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
620 return wrap(unwrap(M)->getNamedGlobal(Name));
623 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
624 Module *Mod = unwrap(M);
625 Module::global_iterator I = Mod->global_begin();
626 if (I == Mod->global_end())
631 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
632 Module *Mod = unwrap(M);
633 Module::global_iterator I = Mod->global_end();
634 if (I == Mod->global_begin())
639 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
640 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
641 Module::global_iterator I = GV;
642 if (++I == GV->getParent()->global_end())
647 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
648 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
649 Module::global_iterator I = GV;
650 if (I == GV->getParent()->global_begin())
655 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
656 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
659 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
660 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
663 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
664 unwrap<GlobalVariable>(GlobalVar)
665 ->setInitializer(unwrap<Constant>(ConstantVal));
668 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
669 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
672 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
673 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
676 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
677 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
680 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
681 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
684 /*--.. Operations on aliases ......................................--*/
686 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
688 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
689 unwrap<Constant>(Aliasee), unwrap (M)));
692 /*--.. Operations on functions .............................................--*/
694 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
695 LLVMTypeRef FunctionTy) {
696 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
697 GlobalValue::ExternalLinkage, Name, unwrap(M)));
700 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
701 return wrap(unwrap(M)->getFunction(Name));
704 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
705 Module *Mod = unwrap(M);
706 Module::iterator I = Mod->begin();
712 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
713 Module *Mod = unwrap(M);
714 Module::iterator I = Mod->end();
715 if (I == Mod->begin())
720 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
721 Function *Func = unwrap<Function>(Fn);
722 Module::iterator I = Func;
723 if (++I == Func->getParent()->end())
728 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
729 Function *Func = unwrap<Function>(Fn);
730 Module::iterator I = Func;
731 if (I == Func->getParent()->begin())
736 void LLVMDeleteFunction(LLVMValueRef Fn) {
737 unwrap<Function>(Fn)->eraseFromParent();
740 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
741 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
742 return F->getIntrinsicID();
746 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
747 return unwrap<Function>(Fn)->getCallingConv();
750 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
751 return unwrap<Function>(Fn)->setCallingConv(CC);
754 const char *LLVMGetGC(LLVMValueRef Fn) {
755 Function *F = unwrap<Function>(Fn);
756 return F->hasGC()? F->getGC() : 0;
759 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
760 Function *F = unwrap<Function>(Fn);
767 /*--.. Operations on parameters ............................................--*/
769 unsigned LLVMCountParams(LLVMValueRef FnRef) {
770 // This function is strictly redundant to
771 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
772 return unwrap<Function>(FnRef)->arg_size();
775 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
776 Function *Fn = unwrap<Function>(FnRef);
777 for (Function::arg_iterator I = Fn->arg_begin(),
778 E = Fn->arg_end(); I != E; I++)
779 *ParamRefs++ = wrap(I);
782 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
783 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
789 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
790 return wrap(unwrap<Argument>(V)->getParent());
793 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
794 Function *Func = unwrap<Function>(Fn);
795 Function::arg_iterator I = Func->arg_begin();
796 if (I == Func->arg_end())
801 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
802 Function *Func = unwrap<Function>(Fn);
803 Function::arg_iterator I = Func->arg_end();
804 if (I == Func->arg_begin())
809 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
810 Argument *A = unwrap<Argument>(Arg);
811 Function::arg_iterator I = A;
812 if (++I == A->getParent()->arg_end())
817 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
818 Argument *A = unwrap<Argument>(Arg);
819 Function::arg_iterator I = A;
820 if (I == A->getParent()->arg_begin())
825 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
826 unwrap<Argument>(Arg)->addAttr(PA);
829 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
830 unwrap<Argument>(Arg)->removeAttr(PA);
833 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
834 unwrap<Argument>(Arg)->addAttr(
835 Attribute::constructAlignmentFromInt(align));
838 /*--.. Operations on basic blocks ..........................................--*/
840 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
841 return wrap(static_cast<Value*>(unwrap(BB)));
844 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
845 return isa<BasicBlock>(unwrap(Val));
848 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
849 return wrap(unwrap<BasicBlock>(Val));
852 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
853 return wrap(unwrap(BB)->getParent());
856 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
857 return unwrap<Function>(FnRef)->size();
860 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
861 Function *Fn = unwrap<Function>(FnRef);
862 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
863 *BasicBlocksRefs++ = wrap(I);
866 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
867 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
870 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
871 Function *Func = unwrap<Function>(Fn);
872 Function::iterator I = Func->begin();
873 if (I == Func->end())
878 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
879 Function *Func = unwrap<Function>(Fn);
880 Function::iterator I = Func->end();
881 if (I == Func->begin())
886 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
887 BasicBlock *Block = unwrap(BB);
888 Function::iterator I = Block;
889 if (++I == Block->getParent()->end())
894 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
895 BasicBlock *Block = unwrap(BB);
896 Function::iterator I = Block;
897 if (I == Block->getParent()->begin())
902 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
903 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
906 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
908 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
909 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
913 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
914 unwrap(BBRef)->eraseFromParent();
917 /*--.. Operations on instructions ..........................................--*/
919 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
920 return wrap(unwrap<Instruction>(Inst)->getParent());
923 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
924 BasicBlock *Block = unwrap(BB);
925 BasicBlock::iterator I = Block->begin();
926 if (I == Block->end())
931 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
932 BasicBlock *Block = unwrap(BB);
933 BasicBlock::iterator I = Block->end();
934 if (I == Block->begin())
939 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
940 Instruction *Instr = unwrap<Instruction>(Inst);
941 BasicBlock::iterator I = Instr;
942 if (++I == Instr->getParent()->end())
947 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
948 Instruction *Instr = unwrap<Instruction>(Inst);
949 BasicBlock::iterator I = Instr;
950 if (I == Instr->getParent()->begin())
955 /*--.. Call and invoke instructions ........................................--*/
957 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
958 Value *V = unwrap(Instr);
959 if (CallInst *CI = dyn_cast<CallInst>(V))
960 return CI->getCallingConv();
961 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
962 return II->getCallingConv();
963 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
967 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
968 Value *V = unwrap(Instr);
969 if (CallInst *CI = dyn_cast<CallInst>(V))
970 return CI->setCallingConv(CC);
971 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
972 return II->setCallingConv(CC);
973 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
976 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
978 CallSite Call = CallSite(unwrap<Instruction>(Instr));
980 Call.getAttributes().addAttr(index, PA));
983 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
985 CallSite Call = CallSite(unwrap<Instruction>(Instr));
987 Call.getAttributes().removeAttr(index, PA));
990 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
992 CallSite Call = CallSite(unwrap<Instruction>(Instr));
994 Call.getAttributes().addAttr(index,
995 Attribute::constructAlignmentFromInt(align)));
998 /*--.. Operations on call instructions (only) ..............................--*/
1000 int LLVMIsTailCall(LLVMValueRef Call) {
1001 return unwrap<CallInst>(Call)->isTailCall();
1004 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1005 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1008 /*--.. Operations on phi nodes .............................................--*/
1010 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1011 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1012 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1013 for (unsigned I = 0; I != Count; ++I)
1014 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1017 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1018 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1021 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1022 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1025 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1026 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1030 /*===-- Instruction builders ----------------------------------------------===*/
1032 LLVMBuilderRef LLVMCreateBuilder(void) {
1033 return wrap(new IRBuilder<>());
1036 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1037 LLVMValueRef Instr) {
1038 BasicBlock *BB = unwrap(Block);
1039 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1040 unwrap(Builder)->SetInsertPoint(BB, I);
1043 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1044 Instruction *I = unwrap<Instruction>(Instr);
1045 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1048 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1049 BasicBlock *BB = unwrap(Block);
1050 unwrap(Builder)->SetInsertPoint(BB);
1053 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1054 return wrap(unwrap(Builder)->GetInsertBlock());
1057 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1058 unwrap(Builder)->ClearInsertionPoint ();
1061 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1062 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1065 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1066 delete unwrap(Builder);
1069 /*--.. Instruction builders ................................................--*/
1071 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1072 return wrap(unwrap(B)->CreateRetVoid());
1075 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1076 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1079 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1080 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1083 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1084 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1085 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1088 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1089 LLVMBasicBlockRef Else, unsigned NumCases) {
1090 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1093 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1094 LLVMValueRef *Args, unsigned NumArgs,
1095 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1097 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1098 unwrap(Args), unwrap(Args) + NumArgs,
1102 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1103 return wrap(unwrap(B)->CreateUnwind());
1106 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1107 return wrap(unwrap(B)->CreateUnreachable());
1110 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1111 LLVMBasicBlockRef Dest) {
1112 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1115 /*--.. Arithmetic ..........................................................--*/
1117 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1119 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1122 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1124 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1127 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1129 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1132 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1134 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1137 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1139 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1142 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1144 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1147 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1149 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1152 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1154 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1157 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1159 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1162 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1164 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1167 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1169 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1172 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1174 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1177 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1179 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1182 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1184 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1187 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1189 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1192 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1193 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1196 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1197 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1200 /*--.. Memory ..............................................................--*/
1202 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1204 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1207 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1208 LLVMValueRef Val, const char *Name) {
1209 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1212 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1214 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1217 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1218 LLVMValueRef Val, const char *Name) {
1219 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1222 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1223 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1227 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1229 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1232 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1233 LLVMValueRef PointerVal) {
1234 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1237 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1238 LLVMValueRef *Indices, unsigned NumIndices,
1240 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1241 unwrap(Indices) + NumIndices, Name));
1244 /*--.. Casts ...............................................................--*/
1246 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1247 LLVMTypeRef DestTy, const char *Name) {
1248 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1251 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1252 LLVMTypeRef DestTy, const char *Name) {
1253 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1256 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1257 LLVMTypeRef DestTy, const char *Name) {
1258 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1261 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1262 LLVMTypeRef DestTy, const char *Name) {
1263 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1266 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1267 LLVMTypeRef DestTy, const char *Name) {
1268 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1271 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1272 LLVMTypeRef DestTy, const char *Name) {
1273 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1276 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1277 LLVMTypeRef DestTy, const char *Name) {
1278 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1281 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1282 LLVMTypeRef DestTy, const char *Name) {
1283 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1286 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1287 LLVMTypeRef DestTy, const char *Name) {
1288 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1291 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1292 LLVMTypeRef DestTy, const char *Name) {
1293 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1296 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1297 LLVMTypeRef DestTy, const char *Name) {
1298 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1301 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1302 LLVMTypeRef DestTy, const char *Name) {
1303 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1306 /*--.. Comparisons .........................................................--*/
1308 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1309 LLVMValueRef LHS, LLVMValueRef RHS,
1311 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1312 unwrap(LHS), unwrap(RHS), Name));
1315 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1316 LLVMValueRef LHS, LLVMValueRef RHS,
1318 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1319 unwrap(LHS), unwrap(RHS), Name));
1322 /*--.. Miscellaneous instructions ..........................................--*/
1324 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1325 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1328 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1329 LLVMValueRef *Args, unsigned NumArgs,
1331 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1332 unwrap(Args) + NumArgs, Name));
1335 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1336 LLVMValueRef Then, LLVMValueRef Else,
1338 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1342 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1343 LLVMTypeRef Ty, const char *Name) {
1344 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1347 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1348 LLVMValueRef Index, const char *Name) {
1349 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1353 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1354 LLVMValueRef EltVal, LLVMValueRef Index,
1356 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1357 unwrap(Index), Name));
1360 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1361 LLVMValueRef V2, LLVMValueRef Mask,
1363 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1364 unwrap(Mask), Name));
1367 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1368 unsigned Index, const char *Name) {
1369 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1372 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1373 LLVMValueRef EltVal, unsigned Index,
1375 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1380 /*===-- Module providers --------------------------------------------------===*/
1382 LLVMModuleProviderRef
1383 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1384 return wrap(new ExistingModuleProvider(unwrap(M)));
1387 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1392 /*===-- Memory buffers ----------------------------------------------------===*/
1394 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1395 LLVMMemoryBufferRef *OutMemBuf,
1396 char **OutMessage) {
1398 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1399 *OutMemBuf = wrap(MB);
1403 *OutMessage = strdup(Error.c_str());
1407 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1408 char **OutMessage) {
1409 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1410 *OutMemBuf = wrap(MB);
1414 *OutMessage = strdup("stdin is empty.");
1418 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1419 delete unwrap(MemBuf);