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/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
35 /*===-- Error handling ----------------------------------------------------===*/
37 void LLVMDisposeMessage(char *Message) {
42 /*===-- Operations on contexts --------------------------------------------===*/
44 LLVMContextRef LLVMContextCreate() {
45 return wrap(new LLVMContext());
48 LLVMContextRef LLVMGetGlobalContext() {
49 return wrap(&getGlobalContext());
52 void LLVMContextDispose(LLVMContextRef C) {
57 /*===-- Operations on modules ---------------------------------------------===*/
59 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
60 return wrap(new Module(ModuleID, getGlobalContext()));
63 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
65 return wrap(new Module(ModuleID, *unwrap(C)));
68 void LLVMDisposeModule(LLVMModuleRef M) {
72 /*--.. Data layout .........................................................--*/
73 const char * LLVMGetDataLayout(LLVMModuleRef M) {
74 return unwrap(M)->getDataLayout().c_str();
77 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
78 unwrap(M)->setDataLayout(Triple);
81 /*--.. Target triple .......................................................--*/
82 const char * LLVMGetTarget(LLVMModuleRef M) {
83 return unwrap(M)->getTargetTriple().c_str();
86 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
87 unwrap(M)->setTargetTriple(Triple);
90 /*--.. Type names ..........................................................--*/
91 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
92 return unwrap(M)->addTypeName(Name, unwrap(Ty));
95 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
99 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
104 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106 return wrap(unwrap(M)->getTypeByName(N));
109 void LLVMDumpModule(LLVMModuleRef M) {
114 /*===-- Operations on types -----------------------------------------------===*/
116 /*--.. Operations on all types (mostly) ....................................--*/
118 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
119 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
122 /*--.. Operations on integer types .........................................--*/
124 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
125 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
126 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
127 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
128 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
130 LLVMTypeRef LLVMIntType(unsigned NumBits) {
131 return wrap(getGlobalContext().getIntegerType(NumBits));
134 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
135 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
138 /*--.. Operations on real types ............................................--*/
140 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
141 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
142 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
143 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
144 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
146 /*--.. Operations on function types ........................................--*/
148 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
149 LLVMTypeRef *ParamTypes, unsigned ParamCount,
151 std::vector<const Type*> Tys;
152 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
153 Tys.push_back(unwrap(*I));
155 return wrap(getGlobalContext().getFunctionType(unwrap(ReturnType), Tys,
159 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
160 return unwrap<FunctionType>(FunctionTy)->isVarArg();
163 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
164 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
167 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
168 return unwrap<FunctionType>(FunctionTy)->getNumParams();
171 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
172 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
173 for (FunctionType::param_iterator I = Ty->param_begin(),
174 E = Ty->param_end(); I != E; ++I)
178 /*--.. Operations on struct types ..........................................--*/
180 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
181 unsigned ElementCount, int Packed) {
182 std::vector<const Type*> Tys;
183 for (LLVMTypeRef *I = ElementTypes,
184 *E = ElementTypes + ElementCount; I != E; ++I)
185 Tys.push_back(unwrap(*I));
187 return wrap(getGlobalContext().getStructType(Tys, Packed != 0));
190 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
191 return unwrap<StructType>(StructTy)->getNumElements();
194 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
195 StructType *Ty = unwrap<StructType>(StructTy);
196 for (FunctionType::param_iterator I = Ty->element_begin(),
197 E = Ty->element_end(); I != E; ++I)
201 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
202 return unwrap<StructType>(StructTy)->isPacked();
205 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
207 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
208 return wrap(getGlobalContext().getArrayType(unwrap(ElementType),
212 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
213 return wrap(getGlobalContext().getPointerType(unwrap(ElementType),
217 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
218 return wrap(getGlobalContext().getVectorType(unwrap(ElementType),
222 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
223 return wrap(unwrap<SequentialType>(Ty)->getElementType());
226 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
227 return unwrap<ArrayType>(ArrayTy)->getNumElements();
230 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
231 return unwrap<PointerType>(PointerTy)->getAddressSpace();
234 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
235 return unwrap<VectorType>(VectorTy)->getNumElements();
238 /*--.. Operations on other types ...........................................--*/
240 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
241 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
243 LLVMTypeRef LLVMOpaqueType(void) {
244 return wrap(getGlobalContext().getOpaqueType());
247 /*--.. Operations on type handles ..........................................--*/
249 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
250 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
253 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
254 delete unwrap(TypeHandle);
257 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
258 return wrap(unwrap(TypeHandle)->get());
261 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
262 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
266 /*===-- Operations on values ----------------------------------------------===*/
268 /*--.. Operations on all values ............................................--*/
270 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
271 return wrap(unwrap(Val)->getType());
274 const char *LLVMGetValueName(LLVMValueRef Val) {
275 return unwrap(Val)->getNameStart();
278 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
279 unwrap(Val)->setName(Name);
282 void LLVMDumpValue(LLVMValueRef Val) {
287 /*--.. Conversion functions ................................................--*/
289 #define LLVM_DEFINE_VALUE_CAST(name) \
290 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
291 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
294 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
297 /*--.. Operations on constants of any type .................................--*/
299 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
300 return wrap(getGlobalContext().getNullValue(unwrap(Ty)));
303 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
304 return wrap(getGlobalContext().getAllOnesValue(unwrap(Ty)));
307 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
308 return wrap(getGlobalContext().getUndef(unwrap(Ty)));
311 int LLVMIsConstant(LLVMValueRef Ty) {
312 return isa<Constant>(unwrap(Ty));
315 int LLVMIsNull(LLVMValueRef Val) {
316 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
317 return C->isNullValue();
321 int LLVMIsUndef(LLVMValueRef Val) {
322 return isa<UndefValue>(unwrap(Val));
325 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
327 wrap(getGlobalContext().getConstantPointerNull(unwrap<PointerType>(Ty)));
330 /*--.. Operations on scalar constants ......................................--*/
332 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
334 return wrap(getGlobalContext().getConstantInt(unwrap<IntegerType>(IntTy), N,
338 static const fltSemantics &SemanticsForType(Type *Ty) {
339 assert(Ty->isFloatingPoint() && "Type is not floating point!");
340 if (Ty == Type::FloatTy)
341 return APFloat::IEEEsingle;
342 if (Ty == Type::DoubleTy)
343 return APFloat::IEEEdouble;
344 if (Ty == Type::X86_FP80Ty)
345 return APFloat::x87DoubleExtended;
346 if (Ty == Type::FP128Ty)
347 return APFloat::IEEEquad;
348 if (Ty == Type::PPC_FP128Ty)
349 return APFloat::PPCDoubleDouble;
350 return APFloat::Bogus;
353 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
356 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
358 return wrap(getGlobalContext().getConstantFP(APN));
361 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
362 return wrap(getGlobalContext().getConstantFP(
363 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
366 /*--.. Operations on composite constants ...................................--*/
368 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
369 int DontNullTerminate) {
370 /* Inverted the sense of AddNull because ', 0)' is a
371 better mnemonic for null termination than ', 1)'. */
372 return wrap(getGlobalContext().getConstantArray(std::string(Str, Length),
373 DontNullTerminate == 0));
376 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
377 LLVMValueRef *ConstantVals, unsigned Length) {
378 return wrap(getGlobalContext().getConstantArray(
379 getGlobalContext().getArrayType(unwrap(ElementTy), Length),
380 unwrap<Constant>(ConstantVals, Length),
384 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
386 return wrap(getGlobalContext().getConstantStruct(
387 unwrap<Constant>(ConstantVals, Count),
388 Count, Packed != 0));
391 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
392 return wrap(getGlobalContext().getConstantVector(
393 unwrap<Constant>(ScalarConstantVals, Size), Size));
396 /*--.. Constant expressions ................................................--*/
398 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
399 return wrap(getGlobalContext().getConstantExprAlignOf(unwrap(Ty)));
402 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
403 return wrap(getGlobalContext().getConstantExprSizeOf(unwrap(Ty)));
406 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
407 return wrap(getGlobalContext().getConstantExprNeg(
408 unwrap<Constant>(ConstantVal)));
411 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
412 return wrap(getGlobalContext().getConstantExprNot(
413 unwrap<Constant>(ConstantVal)));
416 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417 return wrap(getGlobalContext().getConstantExprAdd(
418 unwrap<Constant>(LHSConstant),
419 unwrap<Constant>(RHSConstant)));
422 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
423 return wrap(getGlobalContext().getConstantExprSub(
424 unwrap<Constant>(LHSConstant),
425 unwrap<Constant>(RHSConstant)));
428 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
429 return wrap(getGlobalContext().getConstantExprMul(
430 unwrap<Constant>(LHSConstant),
431 unwrap<Constant>(RHSConstant)));
434 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
435 return wrap(getGlobalContext().getConstantExprUDiv(
436 unwrap<Constant>(LHSConstant),
437 unwrap<Constant>(RHSConstant)));
440 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
441 return wrap(getGlobalContext().getConstantExprSDiv(
442 unwrap<Constant>(LHSConstant),
443 unwrap<Constant>(RHSConstant)));
446 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
447 return wrap(getGlobalContext().getConstantExprFDiv(
448 unwrap<Constant>(LHSConstant),
449 unwrap<Constant>(RHSConstant)));
452 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
453 return wrap(getGlobalContext().getConstantExprURem(
454 unwrap<Constant>(LHSConstant),
455 unwrap<Constant>(RHSConstant)));
458 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
459 return wrap(getGlobalContext().getConstantExprSRem(
460 unwrap<Constant>(LHSConstant),
461 unwrap<Constant>(RHSConstant)));
464 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
465 return wrap(getGlobalContext().getConstantExprFRem(
466 unwrap<Constant>(LHSConstant),
467 unwrap<Constant>(RHSConstant)));
470 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
471 return wrap(getGlobalContext().getConstantExprAnd(
472 unwrap<Constant>(LHSConstant),
473 unwrap<Constant>(RHSConstant)));
476 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
477 return wrap(getGlobalContext().getConstantExprOr(
478 unwrap<Constant>(LHSConstant),
479 unwrap<Constant>(RHSConstant)));
482 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
483 return wrap(getGlobalContext().getConstantExprXor(
484 unwrap<Constant>(LHSConstant),
485 unwrap<Constant>(RHSConstant)));
488 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
489 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
490 return wrap(getGlobalContext().getConstantExprICmp(Predicate,
491 unwrap<Constant>(LHSConstant),
492 unwrap<Constant>(RHSConstant)));
495 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
496 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
497 return wrap(getGlobalContext().getConstantExprFCmp(Predicate,
498 unwrap<Constant>(LHSConstant),
499 unwrap<Constant>(RHSConstant)));
502 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
503 return wrap(getGlobalContext().getConstantExprShl(
504 unwrap<Constant>(LHSConstant),
505 unwrap<Constant>(RHSConstant)));
508 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
509 return wrap(getGlobalContext().getConstantExprLShr(
510 unwrap<Constant>(LHSConstant),
511 unwrap<Constant>(RHSConstant)));
514 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
515 return wrap(getGlobalContext().getConstantExprAShr(
516 unwrap<Constant>(LHSConstant),
517 unwrap<Constant>(RHSConstant)));
520 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
521 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
522 return wrap(getGlobalContext().getConstantExprGetElementPtr(
523 unwrap<Constant>(ConstantVal),
524 unwrap<Constant>(ConstantIndices,
529 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
530 return wrap(getGlobalContext().getConstantExprTrunc(
531 unwrap<Constant>(ConstantVal),
535 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
536 return wrap(getGlobalContext().getConstantExprSExt(
537 unwrap<Constant>(ConstantVal),
541 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
542 return wrap(getGlobalContext().getConstantExprZExt(
543 unwrap<Constant>(ConstantVal),
547 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
548 return wrap(getGlobalContext().getConstantExprFPTrunc(
549 unwrap<Constant>(ConstantVal),
553 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
554 return wrap(getGlobalContext().getConstantExprFPExtend(
555 unwrap<Constant>(ConstantVal),
559 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
560 return wrap(getGlobalContext().getConstantExprUIToFP(
561 unwrap<Constant>(ConstantVal),
565 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
566 return wrap(getGlobalContext().getConstantExprSIToFP(unwrap<Constant>(ConstantVal),
570 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
571 return wrap(getGlobalContext().getConstantExprFPToUI(unwrap<Constant>(ConstantVal),
575 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
576 return wrap(getGlobalContext().getConstantExprFPToSI(
577 unwrap<Constant>(ConstantVal),
581 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
582 return wrap(getGlobalContext().getConstantExprPtrToInt(
583 unwrap<Constant>(ConstantVal),
587 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
588 return wrap(getGlobalContext().getConstantExprIntToPtr(
589 unwrap<Constant>(ConstantVal),
593 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
594 return wrap(getGlobalContext().getConstantExprBitCast(
595 unwrap<Constant>(ConstantVal),
599 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
600 LLVMValueRef ConstantIfTrue,
601 LLVMValueRef ConstantIfFalse) {
602 return wrap(getGlobalContext().getConstantExprSelect(
603 unwrap<Constant>(ConstantCondition),
604 unwrap<Constant>(ConstantIfTrue),
605 unwrap<Constant>(ConstantIfFalse)));
608 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
609 LLVMValueRef IndexConstant) {
610 return wrap(getGlobalContext().getConstantExprExtractElement(
611 unwrap<Constant>(VectorConstant),
612 unwrap<Constant>(IndexConstant)));
615 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
616 LLVMValueRef ElementValueConstant,
617 LLVMValueRef IndexConstant) {
618 return wrap(getGlobalContext().getConstantExprInsertElement(
619 unwrap<Constant>(VectorConstant),
620 unwrap<Constant>(ElementValueConstant),
621 unwrap<Constant>(IndexConstant)));
624 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
625 LLVMValueRef VectorBConstant,
626 LLVMValueRef MaskConstant) {
627 return wrap(getGlobalContext().getConstantExprShuffleVector(
628 unwrap<Constant>(VectorAConstant),
629 unwrap<Constant>(VectorBConstant),
630 unwrap<Constant>(MaskConstant)));
633 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
635 return wrap(getGlobalContext().getConstantExprExtractValue(
636 unwrap<Constant>(AggConstant),
640 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
641 LLVMValueRef ElementValueConstant,
642 unsigned *IdxList, unsigned NumIdx) {
643 return wrap(getGlobalContext().getConstantExprInsertValue(
644 unwrap<Constant>(AggConstant),
645 unwrap<Constant>(ElementValueConstant),
649 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
650 const char *Constraints, int HasSideEffects) {
651 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
652 Constraints, HasSideEffects));
655 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
657 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
658 return wrap(unwrap<GlobalValue>(Global)->getParent());
661 int LLVMIsDeclaration(LLVMValueRef Global) {
662 return unwrap<GlobalValue>(Global)->isDeclaration();
665 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
666 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
669 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
670 unwrap<GlobalValue>(Global)
671 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
674 const char *LLVMGetSection(LLVMValueRef Global) {
675 return unwrap<GlobalValue>(Global)->getSection().c_str();
678 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
679 unwrap<GlobalValue>(Global)->setSection(Section);
682 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
683 return static_cast<LLVMVisibility>(
684 unwrap<GlobalValue>(Global)->getVisibility());
687 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
688 unwrap<GlobalValue>(Global)
689 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
692 unsigned LLVMGetAlignment(LLVMValueRef Global) {
693 return unwrap<GlobalValue>(Global)->getAlignment();
696 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
697 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
700 /*--.. Operations on global variables ......................................--*/
702 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
703 LLVMContext &Context = unwrap(M)->getContext();
704 return wrap(new GlobalVariable(Context, unwrap(Ty), false,
705 GlobalValue::ExternalLinkage, 0, Name,
709 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
710 return wrap(unwrap(M)->getNamedGlobal(Name));
713 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
714 Module *Mod = unwrap(M);
715 Module::global_iterator I = Mod->global_begin();
716 if (I == Mod->global_end())
721 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
722 Module *Mod = unwrap(M);
723 Module::global_iterator I = Mod->global_end();
724 if (I == Mod->global_begin())
729 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
730 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
731 Module::global_iterator I = GV;
732 if (++I == GV->getParent()->global_end())
737 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
738 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
739 Module::global_iterator I = GV;
740 if (I == GV->getParent()->global_begin())
745 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
746 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
749 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
750 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
753 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
754 unwrap<GlobalVariable>(GlobalVar)
755 ->setInitializer(unwrap<Constant>(ConstantVal));
758 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
759 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
762 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
763 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
766 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
767 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
770 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
771 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
774 /*--.. Operations on aliases ......................................--*/
776 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
778 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
779 unwrap<Constant>(Aliasee), unwrap (M)));
782 /*--.. Operations on functions .............................................--*/
784 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
785 LLVMTypeRef FunctionTy) {
786 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
787 GlobalValue::ExternalLinkage, Name, unwrap(M)));
790 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
791 return wrap(unwrap(M)->getFunction(Name));
794 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
795 Module *Mod = unwrap(M);
796 Module::iterator I = Mod->begin();
802 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
803 Module *Mod = unwrap(M);
804 Module::iterator I = Mod->end();
805 if (I == Mod->begin())
810 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
811 Function *Func = unwrap<Function>(Fn);
812 Module::iterator I = Func;
813 if (++I == Func->getParent()->end())
818 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
819 Function *Func = unwrap<Function>(Fn);
820 Module::iterator I = Func;
821 if (I == Func->getParent()->begin())
826 void LLVMDeleteFunction(LLVMValueRef Fn) {
827 unwrap<Function>(Fn)->eraseFromParent();
830 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
831 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
832 return F->getIntrinsicID();
836 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
837 return unwrap<Function>(Fn)->getCallingConv();
840 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
841 return unwrap<Function>(Fn)->setCallingConv(CC);
844 const char *LLVMGetGC(LLVMValueRef Fn) {
845 Function *F = unwrap<Function>(Fn);
846 return F->hasGC()? F->getGC() : 0;
849 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
850 Function *F = unwrap<Function>(Fn);
857 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
858 Function *Func = unwrap<Function>(Fn);
859 const AttrListPtr PAL = Func->getAttributes();
860 const AttrListPtr PALnew = PAL.addAttr(0, PA);
861 Func->setAttributes(PALnew);
864 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
865 Function *Func = unwrap<Function>(Fn);
866 const AttrListPtr PAL = Func->getAttributes();
867 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
868 Func->setAttributes(PALnew);
871 /*--.. Operations on parameters ............................................--*/
873 unsigned LLVMCountParams(LLVMValueRef FnRef) {
874 // This function is strictly redundant to
875 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
876 return unwrap<Function>(FnRef)->arg_size();
879 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
880 Function *Fn = unwrap<Function>(FnRef);
881 for (Function::arg_iterator I = Fn->arg_begin(),
882 E = Fn->arg_end(); I != E; I++)
883 *ParamRefs++ = wrap(I);
886 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
887 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
893 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
894 return wrap(unwrap<Argument>(V)->getParent());
897 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
898 Function *Func = unwrap<Function>(Fn);
899 Function::arg_iterator I = Func->arg_begin();
900 if (I == Func->arg_end())
905 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
906 Function *Func = unwrap<Function>(Fn);
907 Function::arg_iterator I = Func->arg_end();
908 if (I == Func->arg_begin())
913 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
914 Argument *A = unwrap<Argument>(Arg);
915 Function::arg_iterator I = A;
916 if (++I == A->getParent()->arg_end())
921 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
922 Argument *A = unwrap<Argument>(Arg);
923 Function::arg_iterator I = A;
924 if (I == A->getParent()->arg_begin())
929 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
930 unwrap<Argument>(Arg)->addAttr(PA);
933 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
934 unwrap<Argument>(Arg)->removeAttr(PA);
937 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
938 unwrap<Argument>(Arg)->addAttr(
939 Attribute::constructAlignmentFromInt(align));
942 /*--.. Operations on basic blocks ..........................................--*/
944 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
945 return wrap(static_cast<Value*>(unwrap(BB)));
948 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
949 return isa<BasicBlock>(unwrap(Val));
952 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
953 return wrap(unwrap<BasicBlock>(Val));
956 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
957 return wrap(unwrap(BB)->getParent());
960 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
961 return unwrap<Function>(FnRef)->size();
964 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
965 Function *Fn = unwrap<Function>(FnRef);
966 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
967 *BasicBlocksRefs++ = wrap(I);
970 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
971 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
974 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
975 Function *Func = unwrap<Function>(Fn);
976 Function::iterator I = Func->begin();
977 if (I == Func->end())
982 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
983 Function *Func = unwrap<Function>(Fn);
984 Function::iterator I = Func->end();
985 if (I == Func->begin())
990 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
991 BasicBlock *Block = unwrap(BB);
992 Function::iterator I = Block;
993 if (++I == Block->getParent()->end())
998 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
999 BasicBlock *Block = unwrap(BB);
1000 Function::iterator I = Block;
1001 if (I == Block->getParent()->begin())
1006 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1007 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
1010 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
1012 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
1013 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
1017 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1018 unwrap(BBRef)->eraseFromParent();
1021 /*--.. Operations on instructions ..........................................--*/
1023 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1024 return wrap(unwrap<Instruction>(Inst)->getParent());
1027 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1028 BasicBlock *Block = unwrap(BB);
1029 BasicBlock::iterator I = Block->begin();
1030 if (I == Block->end())
1035 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1036 BasicBlock *Block = unwrap(BB);
1037 BasicBlock::iterator I = Block->end();
1038 if (I == Block->begin())
1043 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1044 Instruction *Instr = unwrap<Instruction>(Inst);
1045 BasicBlock::iterator I = Instr;
1046 if (++I == Instr->getParent()->end())
1051 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1052 Instruction *Instr = unwrap<Instruction>(Inst);
1053 BasicBlock::iterator I = Instr;
1054 if (I == Instr->getParent()->begin())
1059 /*--.. Call and invoke instructions ........................................--*/
1061 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1062 Value *V = unwrap(Instr);
1063 if (CallInst *CI = dyn_cast<CallInst>(V))
1064 return CI->getCallingConv();
1065 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1066 return II->getCallingConv();
1067 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
1071 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1072 Value *V = unwrap(Instr);
1073 if (CallInst *CI = dyn_cast<CallInst>(V))
1074 return CI->setCallingConv(CC);
1075 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1076 return II->setCallingConv(CC);
1077 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1080 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1082 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1084 Call.getAttributes().addAttr(index, PA));
1087 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1089 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1091 Call.getAttributes().removeAttr(index, PA));
1094 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1096 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1098 Call.getAttributes().addAttr(index,
1099 Attribute::constructAlignmentFromInt(align)));
1102 /*--.. Operations on call instructions (only) ..............................--*/
1104 int LLVMIsTailCall(LLVMValueRef Call) {
1105 return unwrap<CallInst>(Call)->isTailCall();
1108 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1109 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1112 /*--.. Operations on phi nodes .............................................--*/
1114 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1115 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1116 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1117 for (unsigned I = 0; I != Count; ++I)
1118 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1121 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1122 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1125 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1126 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1129 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1130 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1134 /*===-- Instruction builders ----------------------------------------------===*/
1136 LLVMBuilderRef LLVMCreateBuilder(void) {
1137 return wrap(new IRBuilder<>());
1140 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1141 LLVMValueRef Instr) {
1142 BasicBlock *BB = unwrap(Block);
1143 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1144 unwrap(Builder)->SetInsertPoint(BB, I);
1147 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1148 Instruction *I = unwrap<Instruction>(Instr);
1149 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1152 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1153 BasicBlock *BB = unwrap(Block);
1154 unwrap(Builder)->SetInsertPoint(BB);
1157 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1158 return wrap(unwrap(Builder)->GetInsertBlock());
1161 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1162 unwrap(Builder)->ClearInsertionPoint ();
1165 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1166 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1169 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1170 delete unwrap(Builder);
1173 /*--.. Instruction builders ................................................--*/
1175 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1176 return wrap(unwrap(B)->CreateRetVoid());
1179 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1180 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1183 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1184 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1187 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1188 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1189 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1192 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1193 LLVMBasicBlockRef Else, unsigned NumCases) {
1194 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1197 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1198 LLVMValueRef *Args, unsigned NumArgs,
1199 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1201 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1202 unwrap(Args), unwrap(Args) + NumArgs,
1206 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1207 return wrap(unwrap(B)->CreateUnwind());
1210 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1211 return wrap(unwrap(B)->CreateUnreachable());
1214 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1215 LLVMBasicBlockRef Dest) {
1216 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1219 /*--.. Arithmetic ..........................................................--*/
1221 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1223 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1226 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1228 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1231 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1233 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1236 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1238 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1241 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1243 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1246 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1248 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1251 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1253 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1256 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1258 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1261 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1263 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1266 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1268 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1271 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1273 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1276 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1278 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1281 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1283 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1286 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1288 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1291 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1293 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1296 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1297 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1300 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1301 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1304 /*--.. Memory ..............................................................--*/
1306 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1308 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1311 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1312 LLVMValueRef Val, const char *Name) {
1313 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1316 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1318 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1321 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1322 LLVMValueRef Val, const char *Name) {
1323 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1326 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1327 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1331 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1333 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1336 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1337 LLVMValueRef PointerVal) {
1338 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1341 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1342 LLVMValueRef *Indices, unsigned NumIndices,
1344 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1345 unwrap(Indices) + NumIndices, Name));
1348 /*--.. Casts ...............................................................--*/
1350 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1351 LLVMTypeRef DestTy, const char *Name) {
1352 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1355 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1356 LLVMTypeRef DestTy, const char *Name) {
1357 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1360 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1361 LLVMTypeRef DestTy, const char *Name) {
1362 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1365 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1366 LLVMTypeRef DestTy, const char *Name) {
1367 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1370 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1371 LLVMTypeRef DestTy, const char *Name) {
1372 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1375 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1376 LLVMTypeRef DestTy, const char *Name) {
1377 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1380 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1381 LLVMTypeRef DestTy, const char *Name) {
1382 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1385 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1386 LLVMTypeRef DestTy, const char *Name) {
1387 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1390 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1391 LLVMTypeRef DestTy, const char *Name) {
1392 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1395 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1396 LLVMTypeRef DestTy, const char *Name) {
1397 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1400 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1401 LLVMTypeRef DestTy, const char *Name) {
1402 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1405 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1406 LLVMTypeRef DestTy, const char *Name) {
1407 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1410 /*--.. Comparisons .........................................................--*/
1412 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1413 LLVMValueRef LHS, LLVMValueRef RHS,
1415 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1416 unwrap(LHS), unwrap(RHS), Name));
1419 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1420 LLVMValueRef LHS, LLVMValueRef RHS,
1422 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1423 unwrap(LHS), unwrap(RHS), Name));
1426 /*--.. Miscellaneous instructions ..........................................--*/
1428 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1429 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1432 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1433 LLVMValueRef *Args, unsigned NumArgs,
1435 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1436 unwrap(Args) + NumArgs, Name));
1439 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1440 LLVMValueRef Then, LLVMValueRef Else,
1442 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1446 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1447 LLVMTypeRef Ty, const char *Name) {
1448 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1451 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1452 LLVMValueRef Index, const char *Name) {
1453 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1457 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1458 LLVMValueRef EltVal, LLVMValueRef Index,
1460 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1461 unwrap(Index), Name));
1464 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1465 LLVMValueRef V2, LLVMValueRef Mask,
1467 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1468 unwrap(Mask), Name));
1471 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1472 unsigned Index, const char *Name) {
1473 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1476 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1477 LLVMValueRef EltVal, unsigned Index,
1479 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1484 /*===-- Module providers --------------------------------------------------===*/
1486 LLVMModuleProviderRef
1487 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1488 return wrap(new ExistingModuleProvider(unwrap(M)));
1491 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1496 /*===-- Memory buffers ----------------------------------------------------===*/
1498 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1499 LLVMMemoryBufferRef *OutMemBuf,
1500 char **OutMessage) {
1502 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1503 *OutMemBuf = wrap(MB);
1507 *OutMessage = strdup(Error.c_str());
1511 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1512 char **OutMessage) {
1513 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1514 *OutMemBuf = wrap(MB);
1518 *OutMessage = strdup("stdin is empty.");
1522 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1523 delete unwrap(MemBuf);