Fix regular expression.
[llvm.git] / lib / VMCore / Core.cpp
blobf85dbe76119e0e9b1c224d250e4d5c53f18dc267
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/IntrinsicInst.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/CallSite.h"
27 #include <cassert>
28 #include <cstdlib>
29 #include <cstring>
31 using namespace llvm;
34 /*===-- Error handling ----------------------------------------------------===*/
36 void LLVMDisposeMessage(char *Message) {
37 free(Message);
41 /*===-- Operations on modules ---------------------------------------------===*/
43 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
44 return wrap(new Module(ModuleID));
47 void LLVMDisposeModule(LLVMModuleRef M) {
48 delete unwrap(M);
51 /*--.. Data layout .........................................................--*/
52 const char * LLVMGetDataLayout(LLVMModuleRef M) {
53 return unwrap(M)->getDataLayout().c_str();
56 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
57 unwrap(M)->setDataLayout(Triple);
60 /*--.. Target triple .......................................................--*/
61 const char * LLVMGetTarget(LLVMModuleRef M) {
62 return unwrap(M)->getTargetTriple().c_str();
65 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
66 unwrap(M)->setTargetTriple(Triple);
69 /*--.. Type names ..........................................................--*/
70 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
71 return unwrap(M)->addTypeName(Name, unwrap(Ty));
74 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
75 std::string N(Name);
77 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
78 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
79 if (I->first == N)
80 TST.remove(I);
83 void LLVMDumpModule(LLVMModuleRef M) {
84 unwrap(M)->dump();
88 /*===-- Operations on types -----------------------------------------------===*/
90 /*--.. Operations on all types (mostly) ....................................--*/
92 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
93 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
96 /*--.. Operations on integer types .........................................--*/
98 LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
99 LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
100 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
101 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
102 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
104 LLVMTypeRef LLVMIntType(unsigned NumBits) {
105 return wrap(IntegerType::get(NumBits));
108 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
109 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
112 /*--.. Operations on real types ............................................--*/
114 LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
115 LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
116 LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
117 LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
118 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
120 /*--.. Operations on function types ........................................--*/
122 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
123 LLVMTypeRef *ParamTypes, unsigned ParamCount,
124 int IsVarArg) {
125 std::vector<const Type*> Tys;
126 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
127 Tys.push_back(unwrap(*I));
129 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
132 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
133 return unwrap<FunctionType>(FunctionTy)->isVarArg();
136 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
137 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
140 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
141 return unwrap<FunctionType>(FunctionTy)->getNumParams();
144 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
145 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
146 for (FunctionType::param_iterator I = Ty->param_begin(),
147 E = Ty->param_end(); I != E; ++I)
148 *Dest++ = wrap(*I);
151 /*--.. Operations on struct types ..........................................--*/
153 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
154 unsigned ElementCount, int Packed) {
155 std::vector<const Type*> Tys;
156 for (LLVMTypeRef *I = ElementTypes,
157 *E = ElementTypes + ElementCount; I != E; ++I)
158 Tys.push_back(unwrap(*I));
160 return wrap(StructType::get(Tys, Packed != 0));
163 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
164 return unwrap<StructType>(StructTy)->getNumElements();
167 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
168 StructType *Ty = unwrap<StructType>(StructTy);
169 for (FunctionType::param_iterator I = Ty->element_begin(),
170 E = Ty->element_end(); I != E; ++I)
171 *Dest++ = wrap(*I);
174 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
175 return unwrap<StructType>(StructTy)->isPacked();
178 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
180 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
181 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
184 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
185 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
188 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
189 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
192 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
193 return wrap(unwrap<SequentialType>(Ty)->getElementType());
196 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
197 return unwrap<ArrayType>(ArrayTy)->getNumElements();
200 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
201 return unwrap<PointerType>(PointerTy)->getAddressSpace();
204 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
205 return unwrap<VectorType>(VectorTy)->getNumElements();
208 /*--.. Operations on other types ...........................................--*/
210 LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
211 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
213 LLVMTypeRef LLVMOpaqueType(void) {
214 return wrap(llvm::OpaqueType::get());
217 /*--.. Operations on type handles ..........................................--*/
219 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
220 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
223 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
224 delete unwrap(TypeHandle);
227 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
228 return wrap(unwrap(TypeHandle)->get());
231 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
232 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
236 /*===-- Operations on values ----------------------------------------------===*/
238 /*--.. Operations on all values ............................................--*/
240 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
241 return wrap(unwrap(Val)->getType());
244 const char *LLVMGetValueName(LLVMValueRef Val) {
245 return unwrap(Val)->getNameStart();
248 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
249 unwrap(Val)->setName(Name);
252 void LLVMDumpValue(LLVMValueRef Val) {
253 unwrap(Val)->dump();
257 /*--.. Conversion functions ................................................--*/
259 #define LLVM_DEFINE_VALUE_CAST(name) \
260 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
261 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
264 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
267 /*--.. Operations on constants of any type .................................--*/
269 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
270 return wrap(Constant::getNullValue(unwrap(Ty)));
273 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
274 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
277 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
278 return wrap(UndefValue::get(unwrap(Ty)));
281 int LLVMIsConstant(LLVMValueRef Ty) {
282 return isa<Constant>(unwrap(Ty));
285 int LLVMIsNull(LLVMValueRef Val) {
286 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
287 return C->isNullValue();
288 return false;
291 int LLVMIsUndef(LLVMValueRef Val) {
292 return isa<UndefValue>(unwrap(Val));
295 /*--.. Operations on scalar constants ......................................--*/
297 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
298 int SignExtend) {
299 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
302 static const fltSemantics &SemanticsForType(Type *Ty) {
303 assert(Ty->isFloatingPoint() && "Type is not floating point!");
304 if (Ty == Type::FloatTy)
305 return APFloat::IEEEsingle;
306 if (Ty == Type::DoubleTy)
307 return APFloat::IEEEdouble;
308 if (Ty == Type::X86_FP80Ty)
309 return APFloat::x87DoubleExtended;
310 if (Ty == Type::FP128Ty)
311 return APFloat::IEEEquad;
312 if (Ty == Type::PPC_FP128Ty)
313 return APFloat::PPCDoubleDouble;
314 return APFloat::Bogus;
317 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
318 APFloat APN(N);
319 bool ignored;
320 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
321 &ignored);
322 return wrap(ConstantFP::get(APN));
325 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
326 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
329 /*--.. Operations on composite constants ...................................--*/
331 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
332 int DontNullTerminate) {
333 /* Inverted the sense of AddNull because ', 0)' is a
334 better mnemonic for null termination than ', 1)'. */
335 return wrap(ConstantArray::get(std::string(Str, Length),
336 DontNullTerminate == 0));
339 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
340 LLVMValueRef *ConstantVals, unsigned Length) {
341 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
342 unwrap<Constant>(ConstantVals, Length),
343 Length));
346 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
347 int Packed) {
348 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
349 Count, Packed != 0));
352 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
353 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
354 Size));
357 /*--.. Constant expressions ................................................--*/
359 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
360 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
363 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
364 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
367 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
368 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
371 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
372 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
375 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
377 unwrap<Constant>(RHSConstant)));
380 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
381 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
382 unwrap<Constant>(RHSConstant)));
385 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
386 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
387 unwrap<Constant>(RHSConstant)));
390 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
391 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
392 unwrap<Constant>(RHSConstant)));
395 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
396 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
400 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
401 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
402 unwrap<Constant>(RHSConstant)));
405 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
407 unwrap<Constant>(RHSConstant)));
410 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
412 unwrap<Constant>(RHSConstant)));
415 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
416 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
417 unwrap<Constant>(RHSConstant)));
420 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
421 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
422 unwrap<Constant>(RHSConstant)));
425 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
426 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
427 unwrap<Constant>(RHSConstant)));
430 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
431 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
432 unwrap<Constant>(RHSConstant)));
435 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
436 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
437 return wrap(ConstantExpr::getICmp(Predicate,
438 unwrap<Constant>(LHSConstant),
439 unwrap<Constant>(RHSConstant)));
442 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
443 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
444 return wrap(ConstantExpr::getFCmp(Predicate,
445 unwrap<Constant>(LHSConstant),
446 unwrap<Constant>(RHSConstant)));
449 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
450 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
451 unwrap<Constant>(RHSConstant)));
454 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
455 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
456 unwrap<Constant>(RHSConstant)));
459 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
460 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
461 unwrap<Constant>(RHSConstant)));
464 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
465 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
466 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
467 unwrap<Constant>(ConstantIndices,
468 NumIndices),
469 NumIndices));
472 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
473 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
474 unwrap(ToType)));
477 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
478 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
479 unwrap(ToType)));
482 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
483 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
484 unwrap(ToType)));
487 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
488 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
489 unwrap(ToType)));
492 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
493 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
494 unwrap(ToType)));
497 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
498 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
499 unwrap(ToType)));
502 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
504 unwrap(ToType)));
507 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
509 unwrap(ToType)));
512 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
513 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
514 unwrap(ToType)));
517 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
518 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
519 unwrap(ToType)));
522 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
523 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
524 unwrap(ToType)));
527 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
528 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
529 unwrap(ToType)));
532 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
533 LLVMValueRef ConstantIfTrue,
534 LLVMValueRef ConstantIfFalse) {
535 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
536 unwrap<Constant>(ConstantIfTrue),
537 unwrap<Constant>(ConstantIfFalse)));
540 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
541 LLVMValueRef IndexConstant) {
542 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
543 unwrap<Constant>(IndexConstant)));
546 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
547 LLVMValueRef ElementValueConstant,
548 LLVMValueRef IndexConstant) {
549 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
550 unwrap<Constant>(ElementValueConstant),
551 unwrap<Constant>(IndexConstant)));
554 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
555 LLVMValueRef VectorBConstant,
556 LLVMValueRef MaskConstant) {
557 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
558 unwrap<Constant>(VectorBConstant),
559 unwrap<Constant>(MaskConstant)));
562 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
563 unsigned NumIdx) {
564 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
565 IdxList, NumIdx));
568 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
569 LLVMValueRef ElementValueConstant,
570 unsigned *IdxList, unsigned NumIdx) {
571 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
572 unwrap<Constant>(ElementValueConstant),
573 IdxList, NumIdx));
576 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
577 const char *Constraints, int HasSideEffects) {
578 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
579 Constraints, HasSideEffects));
582 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
584 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
585 return wrap(unwrap<GlobalValue>(Global)->getParent());
588 int LLVMIsDeclaration(LLVMValueRef Global) {
589 return unwrap<GlobalValue>(Global)->isDeclaration();
592 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
593 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
596 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
597 unwrap<GlobalValue>(Global)
598 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
601 const char *LLVMGetSection(LLVMValueRef Global) {
602 return unwrap<GlobalValue>(Global)->getSection().c_str();
605 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
606 unwrap<GlobalValue>(Global)->setSection(Section);
609 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
610 return static_cast<LLVMVisibility>(
611 unwrap<GlobalValue>(Global)->getVisibility());
614 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
615 unwrap<GlobalValue>(Global)
616 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
619 unsigned LLVMGetAlignment(LLVMValueRef Global) {
620 return unwrap<GlobalValue>(Global)->getAlignment();
623 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
624 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
627 /*--.. Operations on global variables ......................................--*/
629 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
630 return wrap(new GlobalVariable(unwrap(Ty), false,
631 GlobalValue::ExternalLinkage, 0, Name,
632 unwrap(M)));
635 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
636 return wrap(unwrap(M)->getNamedGlobal(Name));
639 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
640 Module *Mod = unwrap(M);
641 Module::global_iterator I = Mod->global_begin();
642 if (I == Mod->global_end())
643 return 0;
644 return wrap(I);
647 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
648 Module *Mod = unwrap(M);
649 Module::global_iterator I = Mod->global_end();
650 if (I == Mod->global_begin())
651 return 0;
652 return wrap(--I);
655 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
656 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
657 Module::global_iterator I = GV;
658 if (++I == GV->getParent()->global_end())
659 return 0;
660 return wrap(I);
663 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
664 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
665 Module::global_iterator I = GV;
666 if (I == GV->getParent()->global_begin())
667 return 0;
668 return wrap(--I);
671 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
672 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
675 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
676 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
679 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
680 unwrap<GlobalVariable>(GlobalVar)
681 ->setInitializer(unwrap<Constant>(ConstantVal));
684 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
685 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
688 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
689 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
692 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
693 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
696 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
697 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
700 /*--.. Operations on aliases ......................................--*/
702 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
703 const char *Name) {
704 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
705 unwrap<Constant>(Aliasee), unwrap (M)));
708 /*--.. Operations on functions .............................................--*/
710 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
711 LLVMTypeRef FunctionTy) {
712 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
713 GlobalValue::ExternalLinkage, Name, unwrap(M)));
716 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
717 return wrap(unwrap(M)->getFunction(Name));
720 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
721 Module *Mod = unwrap(M);
722 Module::iterator I = Mod->begin();
723 if (I == Mod->end())
724 return 0;
725 return wrap(I);
728 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
729 Module *Mod = unwrap(M);
730 Module::iterator I = Mod->end();
731 if (I == Mod->begin())
732 return 0;
733 return wrap(--I);
736 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
737 Function *Func = unwrap<Function>(Fn);
738 Module::iterator I = Func;
739 if (++I == Func->getParent()->end())
740 return 0;
741 return wrap(I);
744 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
745 Function *Func = unwrap<Function>(Fn);
746 Module::iterator I = Func;
747 if (I == Func->getParent()->begin())
748 return 0;
749 return wrap(--I);
752 void LLVMDeleteFunction(LLVMValueRef Fn) {
753 unwrap<Function>(Fn)->eraseFromParent();
756 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
757 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
758 return F->getIntrinsicID();
759 return 0;
762 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
763 return unwrap<Function>(Fn)->getCallingConv();
766 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
767 return unwrap<Function>(Fn)->setCallingConv(CC);
770 const char *LLVMGetGC(LLVMValueRef Fn) {
771 Function *F = unwrap<Function>(Fn);
772 return F->hasGC()? F->getGC() : 0;
775 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
776 Function *F = unwrap<Function>(Fn);
777 if (GC)
778 F->setGC(GC);
779 else
780 F->clearGC();
783 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
784 Function *Func = unwrap<Function>(Fn);
785 const AttrListPtr PAL = Func->getAttributes();
786 const AttrListPtr PALnew = PAL.addAttr(0, PA);
787 Func->setAttributes(PALnew);
790 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
791 Function *Func = unwrap<Function>(Fn);
792 const AttrListPtr PAL = Func->getAttributes();
793 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
794 Func->setAttributes(PALnew);
797 /*--.. Operations on parameters ............................................--*/
799 unsigned LLVMCountParams(LLVMValueRef FnRef) {
800 // This function is strictly redundant to
801 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
802 return unwrap<Function>(FnRef)->arg_size();
805 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
806 Function *Fn = unwrap<Function>(FnRef);
807 for (Function::arg_iterator I = Fn->arg_begin(),
808 E = Fn->arg_end(); I != E; I++)
809 *ParamRefs++ = wrap(I);
812 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
813 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
814 while (index --> 0)
815 AI++;
816 return wrap(AI);
819 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
820 return wrap(unwrap<Argument>(V)->getParent());
823 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
824 Function *Func = unwrap<Function>(Fn);
825 Function::arg_iterator I = Func->arg_begin();
826 if (I == Func->arg_end())
827 return 0;
828 return wrap(I);
831 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
832 Function *Func = unwrap<Function>(Fn);
833 Function::arg_iterator I = Func->arg_end();
834 if (I == Func->arg_begin())
835 return 0;
836 return wrap(--I);
839 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
840 Argument *A = unwrap<Argument>(Arg);
841 Function::arg_iterator I = A;
842 if (++I == A->getParent()->arg_end())
843 return 0;
844 return wrap(I);
847 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
848 Argument *A = unwrap<Argument>(Arg);
849 Function::arg_iterator I = A;
850 if (I == A->getParent()->arg_begin())
851 return 0;
852 return wrap(--I);
855 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
856 unwrap<Argument>(Arg)->addAttr(PA);
859 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
860 unwrap<Argument>(Arg)->removeAttr(PA);
863 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
864 unwrap<Argument>(Arg)->addAttr(
865 Attribute::constructAlignmentFromInt(align));
868 /*--.. Operations on basic blocks ..........................................--*/
870 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
871 return wrap(static_cast<Value*>(unwrap(BB)));
874 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
875 return isa<BasicBlock>(unwrap(Val));
878 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
879 return wrap(unwrap<BasicBlock>(Val));
882 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
883 return wrap(unwrap(BB)->getParent());
886 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
887 return unwrap<Function>(FnRef)->size();
890 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
891 Function *Fn = unwrap<Function>(FnRef);
892 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
893 *BasicBlocksRefs++ = wrap(I);
896 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
897 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
900 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
901 Function *Func = unwrap<Function>(Fn);
902 Function::iterator I = Func->begin();
903 if (I == Func->end())
904 return 0;
905 return wrap(I);
908 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
909 Function *Func = unwrap<Function>(Fn);
910 Function::iterator I = Func->end();
911 if (I == Func->begin())
912 return 0;
913 return wrap(--I);
916 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
917 BasicBlock *Block = unwrap(BB);
918 Function::iterator I = Block;
919 if (++I == Block->getParent()->end())
920 return 0;
921 return wrap(I);
924 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
925 BasicBlock *Block = unwrap(BB);
926 Function::iterator I = Block;
927 if (I == Block->getParent()->begin())
928 return 0;
929 return wrap(--I);
932 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
933 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
936 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
937 const char *Name) {
938 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
939 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
940 InsertBeforeBB));
943 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
944 unwrap(BBRef)->eraseFromParent();
947 /*--.. Operations on instructions ..........................................--*/
949 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
950 return wrap(unwrap<Instruction>(Inst)->getParent());
953 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
954 BasicBlock *Block = unwrap(BB);
955 BasicBlock::iterator I = Block->begin();
956 if (I == Block->end())
957 return 0;
958 return wrap(I);
961 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
962 BasicBlock *Block = unwrap(BB);
963 BasicBlock::iterator I = Block->end();
964 if (I == Block->begin())
965 return 0;
966 return wrap(--I);
969 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
970 Instruction *Instr = unwrap<Instruction>(Inst);
971 BasicBlock::iterator I = Instr;
972 if (++I == Instr->getParent()->end())
973 return 0;
974 return wrap(I);
977 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
978 Instruction *Instr = unwrap<Instruction>(Inst);
979 BasicBlock::iterator I = Instr;
980 if (I == Instr->getParent()->begin())
981 return 0;
982 return wrap(--I);
985 /*--.. Call and invoke instructions ........................................--*/
987 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
988 Value *V = unwrap(Instr);
989 if (CallInst *CI = dyn_cast<CallInst>(V))
990 return CI->getCallingConv();
991 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
992 return II->getCallingConv();
993 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
994 return 0;
997 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
998 Value *V = unwrap(Instr);
999 if (CallInst *CI = dyn_cast<CallInst>(V))
1000 return CI->setCallingConv(CC);
1001 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1002 return II->setCallingConv(CC);
1003 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1006 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1007 LLVMAttribute PA) {
1008 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1009 Call.setAttributes(
1010 Call.getAttributes().addAttr(index, PA));
1013 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1014 LLVMAttribute PA) {
1015 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1016 Call.setAttributes(
1017 Call.getAttributes().removeAttr(index, PA));
1020 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1021 unsigned align) {
1022 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1023 Call.setAttributes(
1024 Call.getAttributes().addAttr(index,
1025 Attribute::constructAlignmentFromInt(align)));
1028 /*--.. Operations on call instructions (only) ..............................--*/
1030 int LLVMIsTailCall(LLVMValueRef Call) {
1031 return unwrap<CallInst>(Call)->isTailCall();
1034 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1035 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1038 /*--.. Operations on phi nodes .............................................--*/
1040 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1041 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1042 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1043 for (unsigned I = 0; I != Count; ++I)
1044 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1047 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1048 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1051 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1052 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1055 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1056 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1060 /*===-- Instruction builders ----------------------------------------------===*/
1062 LLVMBuilderRef LLVMCreateBuilder(void) {
1063 return wrap(new IRBuilder<>());
1066 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1067 LLVMValueRef Instr) {
1068 BasicBlock *BB = unwrap(Block);
1069 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1070 unwrap(Builder)->SetInsertPoint(BB, I);
1073 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1074 Instruction *I = unwrap<Instruction>(Instr);
1075 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1078 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1079 BasicBlock *BB = unwrap(Block);
1080 unwrap(Builder)->SetInsertPoint(BB);
1083 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1084 return wrap(unwrap(Builder)->GetInsertBlock());
1087 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1088 unwrap(Builder)->ClearInsertionPoint ();
1091 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1092 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1095 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1096 delete unwrap(Builder);
1099 /*--.. Instruction builders ................................................--*/
1101 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1102 return wrap(unwrap(B)->CreateRetVoid());
1105 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1106 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1109 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1110 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1113 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1114 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1115 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1118 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1119 LLVMBasicBlockRef Else, unsigned NumCases) {
1120 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1123 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1124 LLVMValueRef *Args, unsigned NumArgs,
1125 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1126 const char *Name) {
1127 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1128 unwrap(Args), unwrap(Args) + NumArgs,
1129 Name));
1132 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1133 return wrap(unwrap(B)->CreateUnwind());
1136 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1137 return wrap(unwrap(B)->CreateUnreachable());
1140 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1141 LLVMBasicBlockRef Dest) {
1142 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1145 /*--.. Arithmetic ..........................................................--*/
1147 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1148 const char *Name) {
1149 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1152 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1153 const char *Name) {
1154 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1157 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1158 const char *Name) {
1159 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1162 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1163 const char *Name) {
1164 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1167 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1168 const char *Name) {
1169 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1172 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1173 const char *Name) {
1174 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1177 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1178 const char *Name) {
1179 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1182 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1183 const char *Name) {
1184 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1187 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1188 const char *Name) {
1189 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1192 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1193 const char *Name) {
1194 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1197 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1198 const char *Name) {
1199 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1202 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1203 const char *Name) {
1204 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1207 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1208 const char *Name) {
1209 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1212 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1213 const char *Name) {
1214 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1217 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1218 const char *Name) {
1219 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1222 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1223 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1226 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1227 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1230 /*--.. Memory ..............................................................--*/
1232 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1233 const char *Name) {
1234 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1237 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1238 LLVMValueRef Val, const char *Name) {
1239 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1242 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1243 const char *Name) {
1244 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1247 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1248 LLVMValueRef Val, const char *Name) {
1249 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1252 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1253 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1257 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1258 const char *Name) {
1259 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1262 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1263 LLVMValueRef PointerVal) {
1264 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1267 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1268 LLVMValueRef *Indices, unsigned NumIndices,
1269 const char *Name) {
1270 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1271 unwrap(Indices) + NumIndices, Name));
1274 /*--.. Casts ...............................................................--*/
1276 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1277 LLVMTypeRef DestTy, const char *Name) {
1278 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1281 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1282 LLVMTypeRef DestTy, const char *Name) {
1283 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1286 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1287 LLVMTypeRef DestTy, const char *Name) {
1288 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1291 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1292 LLVMTypeRef DestTy, const char *Name) {
1293 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1296 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1297 LLVMTypeRef DestTy, const char *Name) {
1298 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1301 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1302 LLVMTypeRef DestTy, const char *Name) {
1303 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1306 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1307 LLVMTypeRef DestTy, const char *Name) {
1308 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1311 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1312 LLVMTypeRef DestTy, const char *Name) {
1313 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1316 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1317 LLVMTypeRef DestTy, const char *Name) {
1318 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1321 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1322 LLVMTypeRef DestTy, const char *Name) {
1323 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1326 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1327 LLVMTypeRef DestTy, const char *Name) {
1328 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1331 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1332 LLVMTypeRef DestTy, const char *Name) {
1333 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1336 /*--.. Comparisons .........................................................--*/
1338 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1339 LLVMValueRef LHS, LLVMValueRef RHS,
1340 const char *Name) {
1341 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1342 unwrap(LHS), unwrap(RHS), Name));
1345 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1346 LLVMValueRef LHS, LLVMValueRef RHS,
1347 const char *Name) {
1348 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1349 unwrap(LHS), unwrap(RHS), Name));
1352 /*--.. Miscellaneous instructions ..........................................--*/
1354 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1355 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1358 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1359 LLVMValueRef *Args, unsigned NumArgs,
1360 const char *Name) {
1361 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1362 unwrap(Args) + NumArgs, Name));
1365 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1366 LLVMValueRef Then, LLVMValueRef Else,
1367 const char *Name) {
1368 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1369 Name));
1372 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1373 LLVMTypeRef Ty, const char *Name) {
1374 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1377 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1378 LLVMValueRef Index, const char *Name) {
1379 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1380 Name));
1383 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1384 LLVMValueRef EltVal, LLVMValueRef Index,
1385 const char *Name) {
1386 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1387 unwrap(Index), Name));
1390 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1391 LLVMValueRef V2, LLVMValueRef Mask,
1392 const char *Name) {
1393 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1394 unwrap(Mask), Name));
1397 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1398 unsigned Index, const char *Name) {
1399 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1402 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1403 LLVMValueRef EltVal, unsigned Index,
1404 const char *Name) {
1405 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1406 Index, Name));
1410 /*===-- Module providers --------------------------------------------------===*/
1412 LLVMModuleProviderRef
1413 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1414 return wrap(new ExistingModuleProvider(unwrap(M)));
1417 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1418 delete unwrap(MP);
1422 /*===-- Memory buffers ----------------------------------------------------===*/
1424 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1425 LLVMMemoryBufferRef *OutMemBuf,
1426 char **OutMessage) {
1427 std::string Error;
1428 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1429 *OutMemBuf = wrap(MB);
1430 return 0;
1433 *OutMessage = strdup(Error.c_str());
1434 return 1;
1437 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1438 char **OutMessage) {
1439 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1440 *OutMemBuf = wrap(MB);
1441 return 0;
1444 *OutMessage = strdup("stdin is empty.");
1445 return 1;
1448 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1449 delete unwrap(MemBuf);