blob: 8c9626af570f5f1cc05a4be9bf950bec0fdac9f2 [file] [log] [blame]
Gordon Henriksen8b94a142007-09-18 03:18:57 +00001//===-- Core.cpp ----------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen8b94a142007-09-18 03:18:57 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C bindings for libLLVMCore.a, which implements
11// the LLVM intermediate representation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/Core.h"
16#include "llvm/Bitcode/ReaderWriter.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/GlobalVariable.h"
Gordon Henriksen46abf912007-09-26 20:56:12 +000020#include "llvm/TypeSymbolTable.h"
Gordon Henriksen1ae61352007-12-12 01:04:30 +000021#include "llvm/ModuleProvider.h"
Gordon Henriksenda1435f2007-12-19 22:30:40 +000022#include "llvm/Support/MemoryBuffer.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000023#include <cassert>
Gordon Henriksenda1435f2007-12-19 22:30:40 +000024#include <cstdlib>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000025#include <cstring>
Gordon Henriksen8b94a142007-09-18 03:18:57 +000026
27using namespace llvm;
28
29
Gordon Henriksenda1435f2007-12-19 22:30:40 +000030/*===-- Error handling ----------------------------------------------------===*/
31
32void LLVMDisposeMessage(char *Message) {
33 free(Message);
34}
35
36
Gordon Henriksen8b94a142007-09-18 03:18:57 +000037/*===-- Operations on modules ---------------------------------------------===*/
38
39LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
40 return wrap(new Module(ModuleID));
41}
42
43void LLVMDisposeModule(LLVMModuleRef M) {
44 delete unwrap(M);
45}
46
Gordon Henriksena353ffa2007-12-27 20:13:47 +000047/*--.. Data layout .........................................................--*/
48const char * LLVMGetDataLayout(LLVMModuleRef M) {
49 return unwrap(M)->getDataLayout().c_str();
50}
51
52void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
53 unwrap(M)->setDataLayout(Triple);
54}
55
56/*--.. Target triple .......................................................--*/
57const char * LLVMGetTarget(LLVMModuleRef M) {
58 return unwrap(M)->getTargetTriple().c_str();
59}
60
61void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
62 unwrap(M)->setTargetTriple(Triple);
63}
64
65/*--.. Type names ..........................................................--*/
Gordon Henriksen8b94a142007-09-18 03:18:57 +000066int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
67 return unwrap(M)->addTypeName(Name, unwrap(Ty));
68}
69
Gordon Henriksen46abf912007-09-26 20:56:12 +000070void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
71 std::string N(Name);
72
73 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
74 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
75 if (I->first == N)
76 TST.remove(I);
77}
78
Gordon Henriksenaf59b102008-03-14 23:58:56 +000079void LLVMDumpModule(LLVMModuleRef M) {
80 unwrap(M)->dump();
81}
82
Gordon Henriksen8b94a142007-09-18 03:18:57 +000083
84/*===-- Operations on types -----------------------------------------------===*/
85
86/*--.. Operations on all types (mostly) ....................................--*/
87
88LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
89 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
90}
91
92void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
93 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
94 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
95}
96
97/*--.. Operations on integer types .........................................--*/
98
99LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
100LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
101LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
102LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
103LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
104
Gordon Henriksen81a78812007-10-06 16:05:20 +0000105LLVMTypeRef LLVMIntType(unsigned NumBits) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000106 return wrap(IntegerType::get(NumBits));
107}
108
Gordon Henriksen46abf912007-09-26 20:56:12 +0000109unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000110 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
111}
112
113/*--.. Operations on real types ............................................--*/
114
115LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
116LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
117LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
118LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
119LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
120
121/*--.. Operations on function types ........................................--*/
122
Gordon Henriksen81a78812007-10-06 16:05:20 +0000123LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
124 LLVMTypeRef *ParamTypes, unsigned ParamCount,
125 int IsVarArg) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000126 std::vector<const Type*> Tys;
127 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
128 Tys.push_back(unwrap(*I));
129
130 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
131}
132
133int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
134 return unwrap<FunctionType>(FunctionTy)->isVarArg();
135}
136
Gordon Henriksen46abf912007-09-26 20:56:12 +0000137LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000138 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
139}
140
Gordon Henriksen46abf912007-09-26 20:56:12 +0000141unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000142 return unwrap<FunctionType>(FunctionTy)->getNumParams();
143}
144
Gordon Henriksen46abf912007-09-26 20:56:12 +0000145void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000146 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
147 for (FunctionType::param_iterator I = Ty->param_begin(),
148 E = Ty->param_end(); I != E; ++I)
149 *Dest++ = wrap(*I);
150}
151
152/*--.. Operations on struct types ..........................................--*/
153
Gordon Henriksen81a78812007-10-06 16:05:20 +0000154LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
155 unsigned ElementCount, int Packed) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000156 std::vector<const Type*> Tys;
157 for (LLVMTypeRef *I = ElementTypes,
158 *E = ElementTypes + ElementCount; I != E; ++I)
159 Tys.push_back(unwrap(*I));
160
161 return wrap(StructType::get(Tys, Packed != 0));
162}
163
Gordon Henriksen46abf912007-09-26 20:56:12 +0000164unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000165 return unwrap<StructType>(StructTy)->getNumElements();
166}
167
168void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
169 StructType *Ty = unwrap<StructType>(StructTy);
170 for (FunctionType::param_iterator I = Ty->element_begin(),
171 E = Ty->element_end(); I != E; ++I)
172 *Dest++ = wrap(*I);
173}
174
175int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
176 return unwrap<StructType>(StructTy)->isPacked();
177}
178
179/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
180
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000181LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000182 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
183}
184
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000185LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
186 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000187}
188
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000189LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000190 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
191}
192
193LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
194 return wrap(unwrap<SequentialType>(Ty)->getElementType());
195}
196
197unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
198 return unwrap<ArrayType>(ArrayTy)->getNumElements();
199}
200
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000201unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
202 return unwrap<PointerType>(PointerTy)->getAddressSpace();
203}
204
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000205unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
206 return unwrap<VectorType>(VectorTy)->getNumElements();
207}
208
209/*--.. Operations on other types ...........................................--*/
210
211LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
212LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
213
Gordon Henriksen81a78812007-10-06 16:05:20 +0000214LLVMTypeRef LLVMOpaqueType() {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000215 return wrap(llvm::OpaqueType::get());
216}
217
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000218/*--.. Operations on type handles ..........................................--*/
Gordon Henriksen1cf08fd2007-10-07 00:13:35 +0000219
220LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
221 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
222}
223
224void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
225 delete unwrap(TypeHandle);
226}
227
228LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
229 return wrap(unwrap(TypeHandle)->get());
230}
231
232void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
233 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
234}
235
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000236
237/*===-- Operations on values ----------------------------------------------===*/
238
239/*--.. Operations on all values ............................................--*/
240
Gordon Henriksen46abf912007-09-26 20:56:12 +0000241LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000242 return wrap(unwrap(Val)->getType());
243}
244
245const char *LLVMGetValueName(LLVMValueRef Val) {
246 return unwrap(Val)->getNameStart();
247}
248
249void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
250 unwrap(Val)->setName(Name);
251}
252
Gordon Henriksen88cc6992007-10-06 00:08:49 +0000253void LLVMDumpValue(LLVMValueRef Val) {
254 unwrap(Val)->dump();
255}
256
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000257/*--.. Operations on constants of any type .................................--*/
258
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000259LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000260 return wrap(Constant::getNullValue(unwrap(Ty)));
261}
262
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000263LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000264 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
265}
266
267LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
268 return wrap(UndefValue::get(unwrap(Ty)));
269}
270
Gordon Henriksen344be5f2007-09-18 18:07:51 +0000271int LLVMIsConstant(LLVMValueRef Ty) {
272 return isa<Constant>(unwrap(Ty));
273}
274
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000275int LLVMIsNull(LLVMValueRef Val) {
276 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
277 return C->isNullValue();
278 return false;
279}
280
Gordon Henriksen344be5f2007-09-18 18:07:51 +0000281int LLVMIsUndef(LLVMValueRef Val) {
282 return isa<UndefValue>(unwrap(Val));
283}
284
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000285/*--.. Operations on scalar constants ......................................--*/
286
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000287LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
288 int SignExtend) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000289 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
290}
291
Gordon Henriksene62a8a32008-02-02 01:07:50 +0000292static const fltSemantics &SemanticsForType(Type *Ty) {
293 assert(Ty->isFloatingPoint() && "Type is not floating point!");
294 if (Ty == Type::FloatTy)
295 return APFloat::IEEEsingle;
296 if (Ty == Type::DoubleTy)
297 return APFloat::IEEEdouble;
298 if (Ty == Type::X86_FP80Ty)
299 return APFloat::x87DoubleExtended;
300 if (Ty == Type::FP128Ty)
301 return APFloat::IEEEquad;
302 if (Ty == Type::PPC_FP128Ty)
303 return APFloat::PPCDoubleDouble;
304 return APFloat::Bogus;
305}
306
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000307LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Gordon Henriksene62a8a32008-02-02 01:07:50 +0000308 APFloat APN(N);
309 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
310 return wrap(ConstantFP::get(unwrap(RealTy), APN));
311}
312
313LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
314 return wrap(ConstantFP::get(unwrap(RealTy),
315 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000316}
317
318/*--.. Operations on composite constants ...................................--*/
319
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000320LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
321 int DontNullTerminate) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000322 /* Inverted the sense of AddNull because ', 0)' is a
323 better mnemonic for null termination than ', 1)'. */
324 return wrap(ConstantArray::get(std::string(Str, Length),
325 DontNullTerminate == 0));
326}
327
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000328LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
329 LLVMValueRef *ConstantVals, unsigned Length) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000330 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
331 unwrap<Constant>(ConstantVals, Length),
332 Length));
333}
334
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000335LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
336 int Packed) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000337 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
338 Count, Packed != 0));
339}
340
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000341LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000342 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
343 Size));
344}
345
Gordon Henriksen46475692007-10-06 14:29:36 +0000346/*--.. Constant expressions ................................................--*/
347
348LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
349 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
350}
351
352LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
353 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
354}
355
356LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
357 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
358}
359
360LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
361 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
362 unwrap<Constant>(RHSConstant)));
363}
364
365LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
366 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
367 unwrap<Constant>(RHSConstant)));
368}
369
370LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
371 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
372 unwrap<Constant>(RHSConstant)));
373}
374
375LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
377 unwrap<Constant>(RHSConstant)));
378}
379
380LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
381 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
382 unwrap<Constant>(RHSConstant)));
383}
384
385LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
386 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
387 unwrap<Constant>(RHSConstant)));
388}
389
390LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
391 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
392 unwrap<Constant>(RHSConstant)));
393}
394
395LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
396 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
398}
399
400LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
401 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
402 unwrap<Constant>(RHSConstant)));
403}
404
405LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
407 unwrap<Constant>(RHSConstant)));
408}
409
410LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
412 unwrap<Constant>(RHSConstant)));
413}
414
415LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
416 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
417 unwrap<Constant>(RHSConstant)));
418}
419
420LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
421 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
422 return wrap(ConstantExpr::getICmp(Predicate,
423 unwrap<Constant>(LHSConstant),
424 unwrap<Constant>(RHSConstant)));
425}
426
427LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
428 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
429 return wrap(ConstantExpr::getFCmp(Predicate,
430 unwrap<Constant>(LHSConstant),
431 unwrap<Constant>(RHSConstant)));
432}
433
434LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
435 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
436 unwrap<Constant>(RHSConstant)));
437}
438
439LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
441 unwrap<Constant>(RHSConstant)));
442}
443
444LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
445 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
446 unwrap<Constant>(RHSConstant)));
447}
448
449LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
450 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
451 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
452 unwrap<Constant>(ConstantIndices,
453 NumIndices),
454 NumIndices));
455}
456
457LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
458 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
459 unwrap(ToType)));
460}
461
462LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
463 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
464 unwrap(ToType)));
465}
466
467LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
468 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
469 unwrap(ToType)));
470}
471
472LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
473 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
474 unwrap(ToType)));
475}
476
477LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
478 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
479 unwrap(ToType)));
480}
481
482LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
483 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
484 unwrap(ToType)));
485}
486
487LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
488 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
489 unwrap(ToType)));
490}
491
492LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
493 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
494 unwrap(ToType)));
495}
496
497LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
498 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
499 unwrap(ToType)));
500}
501
502LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
504 unwrap(ToType)));
505}
506
507LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
509 unwrap(ToType)));
510}
511
512LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
513 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
514 unwrap(ToType)));
515}
516
517LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
518 LLVMValueRef ConstantIfTrue,
519 LLVMValueRef ConstantIfFalse) {
520 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
521 unwrap<Constant>(ConstantIfTrue),
522 unwrap<Constant>(ConstantIfFalse)));
523}
524
525LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
526 LLVMValueRef IndexConstant) {
527 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
528 unwrap<Constant>(IndexConstant)));
529}
530
531LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
532 LLVMValueRef ElementValueConstant,
533 LLVMValueRef IndexConstant) {
534 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
535 unwrap<Constant>(ElementValueConstant),
536 unwrap<Constant>(IndexConstant)));
537}
538
539LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
540 LLVMValueRef VectorBConstant,
541 LLVMValueRef MaskConstant) {
542 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
543 unwrap<Constant>(VectorBConstant),
544 unwrap<Constant>(MaskConstant)));
545}
546
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000547/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
548
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000549LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
550 return wrap(unwrap<GlobalValue>(Global)->getParent());
551}
552
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000553int LLVMIsDeclaration(LLVMValueRef Global) {
554 return unwrap<GlobalValue>(Global)->isDeclaration();
555}
556
557LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
558 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
559}
560
561void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
562 unwrap<GlobalValue>(Global)
563 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
564}
565
566const char *LLVMGetSection(LLVMValueRef Global) {
567 return unwrap<GlobalValue>(Global)->getSection().c_str();
568}
569
570void LLVMSetSection(LLVMValueRef Global, const char *Section) {
571 unwrap<GlobalValue>(Global)->setSection(Section);
572}
573
574LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
575 return static_cast<LLVMVisibility>(
576 unwrap<GlobalValue>(Global)->getVisibility());
577}
578
579void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
580 unwrap<GlobalValue>(Global)
581 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
582}
583
584unsigned LLVMGetAlignment(LLVMValueRef Global) {
585 return unwrap<GlobalValue>(Global)->getAlignment();
586}
587
588void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
589 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
590}
591
592/*--.. Operations on global variables ......................................--*/
593
594LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
595 return wrap(new GlobalVariable(unwrap(Ty), false,
Gordon Henriksendc2c07a2007-12-30 17:46:33 +0000596 GlobalValue::ExternalLinkage, 0, Name,
597 unwrap(M)));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000598}
599
Gordon Henriksen6d6203d2007-10-08 03:45:09 +0000600LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
601 return wrap(unwrap(M)->getNamedGlobal(Name));
602}
603
Gordon Henriksen34000972008-03-19 03:47:18 +0000604LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
605 Module *Mod = unwrap(M);
606 Module::global_iterator I = Mod->global_begin();
607 if (I == Mod->global_end())
608 return 0;
609 return wrap(I);
610}
611
612LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
613 Module *Mod = unwrap(M);
614 Module::global_iterator I = Mod->global_end();
615 if (I == Mod->global_begin())
616 return 0;
617 return wrap(--I);
618}
619
620LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
621 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
622 Module::global_iterator I = GV;
623 if (++I == GV->getParent()->global_end())
624 return 0;
625 return wrap(I);
626}
627
628LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
629 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
630 Module::global_iterator I = GV;
Gordon Henriksen4733be32008-03-23 22:21:29 +0000631 if (I == GV->getParent()->global_begin())
Gordon Henriksen34000972008-03-19 03:47:18 +0000632 return 0;
633 return wrap(--I);
634}
635
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000636void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
637 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
638}
639
640int LLVMHasInitializer(LLVMValueRef GlobalVar) {
641 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
642}
643
644LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
645 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
646}
647
648void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
649 unwrap<GlobalVariable>(GlobalVar)
650 ->setInitializer(unwrap<Constant>(ConstantVal));
651}
652
653int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
654 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
655}
656
657void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
658 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
659}
660
Gordon Henriksenc84c16b2007-10-07 17:31:42 +0000661int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksenc84c16b2007-10-07 17:31:42 +0000662 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
663}
664
665void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
666 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
667}
668
Gordon Henriksen46abf912007-09-26 20:56:12 +0000669/*--.. Operations on functions .............................................--*/
670
671LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
672 LLVMTypeRef FunctionTy) {
Gabor Greif051a9502008-04-06 20:25:17 +0000673 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
674 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000675}
676
Gordon Henriksen6d6203d2007-10-08 03:45:09 +0000677LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
678 return wrap(unwrap(M)->getFunction(Name));
679}
680
Gordon Henriksen34000972008-03-19 03:47:18 +0000681LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
682 Module *Mod = unwrap(M);
683 Module::iterator I = Mod->begin();
684 if (I == Mod->end())
685 return 0;
686 return wrap(I);
687}
688
689LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
690 Module *Mod = unwrap(M);
691 Module::iterator I = Mod->end();
692 if (I == Mod->begin())
693 return 0;
694 return wrap(--I);
695}
696
697LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
698 Function *Func = unwrap<Function>(Fn);
699 Module::iterator I = Func;
700 if (++I == Func->getParent()->end())
701 return 0;
702 return wrap(I);
703}
704
705LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
706 Function *Func = unwrap<Function>(Fn);
707 Module::iterator I = Func;
Gordon Henriksen4733be32008-03-23 22:21:29 +0000708 if (I == Func->getParent()->begin())
Gordon Henriksen34000972008-03-19 03:47:18 +0000709 return 0;
710 return wrap(--I);
711}
712
Gordon Henriksen46abf912007-09-26 20:56:12 +0000713void LLVMDeleteFunction(LLVMValueRef Fn) {
714 unwrap<Function>(Fn)->eraseFromParent();
715}
716
Gordon Henriksen46abf912007-09-26 20:56:12 +0000717unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
718 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
719 return F->getIntrinsicID();
720 return 0;
721}
722
723unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
724 return unwrap<Function>(Fn)->getCallingConv();
725}
726
727void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
728 return unwrap<Function>(Fn)->setCallingConv(CC);
729}
730
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000731const char *LLVMGetCollector(LLVMValueRef Fn) {
732 Function *F = unwrap<Function>(Fn);
733 return F->hasCollector()? F->getCollector() : 0;
734}
735
736void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
737 Function *F = unwrap<Function>(Fn);
738 if (Coll)
739 F->setCollector(Coll);
740 else
741 F->clearCollector();
742}
743
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000744/*--.. Operations on parameters ............................................--*/
745
746unsigned LLVMCountParams(LLVMValueRef FnRef) {
747 // This function is strictly redundant to
748 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
749 return unwrap<Function>(FnRef)->getArgumentList().size();
750}
751
752void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
753 Function *Fn = unwrap<Function>(FnRef);
754 for (Function::arg_iterator I = Fn->arg_begin(),
755 E = Fn->arg_end(); I != E; I++)
756 *ParamRefs++ = wrap(I);
757}
758
759LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
760 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
761 while (index --> 0)
762 AI++;
763 return wrap(AI);
764}
765
766LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
767 return wrap(unwrap<Argument>(V)->getParent());
768}
769
Gordon Henriksen4733be32008-03-23 22:21:29 +0000770LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
771 Function *Func = unwrap<Function>(Fn);
772 Function::arg_iterator I = Func->arg_begin();
773 if (I == Func->arg_end())
774 return 0;
775 return wrap(I);
776}
777
778LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
779 Function *Func = unwrap<Function>(Fn);
780 Function::arg_iterator I = Func->arg_end();
781 if (I == Func->arg_begin())
782 return 0;
783 return wrap(--I);
784}
785
786LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
787 Argument *A = unwrap<Argument>(Arg);
788 Function::arg_iterator I = A;
789 if (++I == A->getParent()->arg_end())
790 return 0;
791 return wrap(I);
792}
793
794LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
795 Argument *A = unwrap<Argument>(Arg);
796 Function::arg_iterator I = A;
797 if (I == A->getParent()->arg_begin())
798 return 0;
799 return wrap(--I);
800}
801
Gordon Henriksen46abf912007-09-26 20:56:12 +0000802/*--.. Operations on basic blocks ..........................................--*/
803
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000804LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
805 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000806}
807
808int LLVMValueIsBasicBlock(LLVMValueRef Val) {
809 return isa<BasicBlock>(unwrap(Val));
810}
811
812LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
813 return wrap(unwrap<BasicBlock>(Val));
814}
815
Gordon Henriksen4733be32008-03-23 22:21:29 +0000816LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
817 return wrap(unwrap(BB)->getParent());
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000818}
819
Gordon Henriksen46abf912007-09-26 20:56:12 +0000820unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
821 return unwrap<Function>(FnRef)->getBasicBlockList().size();
822}
823
824void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
825 Function *Fn = unwrap<Function>(FnRef);
826 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
827 *BasicBlocksRefs++ = wrap(I);
828}
829
830LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
831 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
832}
833
Gordon Henriksen34000972008-03-19 03:47:18 +0000834LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
835 Function *Func = unwrap<Function>(Fn);
836 Function::iterator I = Func->begin();
837 if (I == Func->end())
838 return 0;
839 return wrap(I);
840}
841
842LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
843 Function *Func = unwrap<Function>(Fn);
844 Function::iterator I = Func->end();
845 if (I == Func->begin())
846 return 0;
847 return wrap(--I);
848}
849
850LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
851 BasicBlock *Block = unwrap(BB);
852 Function::iterator I = Block;
853 if (++I == Block->getParent()->end())
854 return 0;
855 return wrap(I);
856}
857
858LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
859 BasicBlock *Block = unwrap(BB);
860 Function::iterator I = Block;
861 if (I == Block->getParent()->begin())
862 return 0;
863 return wrap(--I);
864}
865
Gordon Henriksen46abf912007-09-26 20:56:12 +0000866LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
Gabor Greif051a9502008-04-06 20:25:17 +0000867 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000868}
869
870LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
871 const char *Name) {
872 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
Gabor Greif051a9502008-04-06 20:25:17 +0000873 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
874 InsertBeforeBB));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000875}
876
877void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
878 unwrap(BBRef)->eraseFromParent();
879}
880
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000881/*--.. Operations on instructions ..........................................--*/
882
883LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
884 return wrap(unwrap<Instruction>(Inst)->getParent());
885}
886
Gordon Henriksen34000972008-03-19 03:47:18 +0000887LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
888 BasicBlock *Block = unwrap(BB);
889 BasicBlock::iterator I = Block->begin();
890 if (I == Block->end())
891 return 0;
892 return wrap(I);
893}
894
895LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
896 BasicBlock *Block = unwrap(BB);
897 BasicBlock::iterator I = Block->end();
898 if (I == Block->begin())
899 return 0;
900 return wrap(--I);
901}
902
903LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
904 Instruction *Instr = unwrap<Instruction>(Inst);
905 BasicBlock::iterator I = Instr;
906 if (++I == Instr->getParent()->end())
907 return 0;
908 return wrap(I);
909}
910
911LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
912 Instruction *Instr = unwrap<Instruction>(Inst);
913 BasicBlock::iterator I = Instr;
914 if (I == Instr->getParent()->begin())
915 return 0;
916 return wrap(--I);
917}
918
Gordon Henriksen46abf912007-09-26 20:56:12 +0000919/*--.. Call and invoke instructions ........................................--*/
920
921unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
922 Value *V = unwrap(Instr);
923 if (CallInst *CI = dyn_cast<CallInst>(V))
924 return CI->getCallingConv();
925 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
926 return II->getCallingConv();
927 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
928 return 0;
929}
930
931void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
932 Value *V = unwrap(Instr);
933 if (CallInst *CI = dyn_cast<CallInst>(V))
934 return CI->setCallingConv(CC);
935 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
936 return II->setCallingConv(CC);
937 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
938}
939
Gordon Henriksen2618a6c2007-10-08 18:14:39 +0000940/*--.. Operations on phi nodes .............................................--*/
941
942void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
943 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
944 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
945 for (unsigned I = 0; I != Count; ++I)
946 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
947}
948
949unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
950 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
951}
952
953LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
954 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
955}
956
957LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
958 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
959}
960
Gordon Henriksen46abf912007-09-26 20:56:12 +0000961
962/*===-- Instruction builders ----------------------------------------------===*/
963
964LLVMBuilderRef LLVMCreateBuilder() {
Gordon Henriksen1d2e49c2007-12-27 18:25:59 +0000965 return wrap(new LLVMFoldingBuilder());
Gordon Henriksen46abf912007-09-26 20:56:12 +0000966}
967
Gordon Henriksen34000972008-03-19 03:47:18 +0000968void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
969 LLVMValueRef Instr) {
970 BasicBlock *BB = unwrap(Block);
971 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
972 unwrap(Builder)->SetInsertPoint(BB, I);
973}
974
Gordon Henriksen46abf912007-09-26 20:56:12 +0000975void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
976 Instruction *I = unwrap<Instruction>(Instr);
977 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
978}
979
980void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
981 BasicBlock *BB = unwrap(Block);
982 unwrap(Builder)->SetInsertPoint(BB);
983}
984
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000985LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
986 return wrap(unwrap(Builder)->GetInsertBlock());
987}
988
Gordon Henriksen46abf912007-09-26 20:56:12 +0000989void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
990 delete unwrap(Builder);
991}
992
993/*--.. Instruction builders ................................................--*/
994
995LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
996 return wrap(unwrap(B)->CreateRetVoid());
997}
998
999LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1000 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1001}
1002
1003LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1004 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1005}
1006
1007LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1008 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1009 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1010}
1011
1012LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1013 LLVMBasicBlockRef Else, unsigned NumCases) {
1014 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1015}
1016
1017LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1018 LLVMValueRef *Args, unsigned NumArgs,
1019 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1020 const char *Name) {
1021 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1022 unwrap(Args), unwrap(Args) + NumArgs,
1023 Name));
1024}
1025
1026LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1027 return wrap(unwrap(B)->CreateUnwind());
1028}
1029
1030LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1031 return wrap(unwrap(B)->CreateUnreachable());
1032}
1033
Gordon Henriksenab477cc2008-01-01 05:50:53 +00001034void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1035 LLVMBasicBlockRef Dest) {
1036 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1037}
1038
Gordon Henriksen46abf912007-09-26 20:56:12 +00001039/*--.. Arithmetic ..........................................................--*/
1040
1041LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1042 const char *Name) {
1043 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1044}
1045
1046LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1047 const char *Name) {
1048 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1049}
1050
1051LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1052 const char *Name) {
1053 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1054}
1055
1056LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1057 const char *Name) {
1058 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1059}
1060
1061LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1062 const char *Name) {
1063 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1064}
1065
1066LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1067 const char *Name) {
1068 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1069}
1070
1071LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1072 const char *Name) {
1073 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1074}
1075
1076LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1077 const char *Name) {
1078 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1079}
1080
1081LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1082 const char *Name) {
1083 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1084}
1085
1086LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1087 const char *Name) {
1088 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1089}
1090
1091LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1092 const char *Name) {
1093 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1094}
1095
1096LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1097 const char *Name) {
1098 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1099}
1100
1101LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1102 const char *Name) {
1103 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1104}
1105
1106LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1107 const char *Name) {
1108 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1109}
1110
1111LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1112 const char *Name) {
1113 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1114}
1115
1116LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1117 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1118}
1119
1120LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1121 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1122}
1123
1124/*--.. Memory ..............................................................--*/
1125
1126LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1127 const char *Name) {
1128 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1129}
1130
1131LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1132 LLVMValueRef Val, const char *Name) {
1133 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1134}
1135
1136LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1137 const char *Name) {
1138 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1139}
1140
1141LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1142 LLVMValueRef Val, const char *Name) {
1143 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1144}
1145
1146LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1147 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1148}
1149
1150
1151LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1152 const char *Name) {
1153 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1154}
1155
1156LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1157 LLVMValueRef PointerVal) {
1158 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1159}
1160
1161LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1162 LLVMValueRef *Indices, unsigned NumIndices,
1163 const char *Name) {
1164 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1165 unwrap(Indices) + NumIndices, Name));
1166}
1167
1168/*--.. Casts ...............................................................--*/
1169
1170LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1171 LLVMTypeRef DestTy, const char *Name) {
1172 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1173}
1174
1175LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1176 LLVMTypeRef DestTy, const char *Name) {
1177 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1178}
1179
1180LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1181 LLVMTypeRef DestTy, const char *Name) {
1182 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1183}
1184
1185LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1186 LLVMTypeRef DestTy, const char *Name) {
1187 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1188}
1189
1190LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1191 LLVMTypeRef DestTy, const char *Name) {
1192 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1193}
1194
1195LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1196 LLVMTypeRef DestTy, const char *Name) {
1197 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1198}
1199
1200LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1201 LLVMTypeRef DestTy, const char *Name) {
1202 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1203}
1204
1205LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1206 LLVMTypeRef DestTy, const char *Name) {
1207 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1208}
1209
1210LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1211 LLVMTypeRef DestTy, const char *Name) {
1212 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1213}
1214
1215LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1216 LLVMTypeRef DestTy, const char *Name) {
1217 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1218}
1219
1220LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1221 LLVMTypeRef DestTy, const char *Name) {
1222 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1223}
1224
1225LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1226 LLVMTypeRef DestTy, const char *Name) {
1227 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1228}
1229
1230/*--.. Comparisons .........................................................--*/
1231
1232LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1233 LLVMValueRef LHS, LLVMValueRef RHS,
1234 const char *Name) {
1235 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1236 unwrap(LHS), unwrap(RHS), Name));
1237}
1238
1239LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1240 LLVMValueRef LHS, LLVMValueRef RHS,
1241 const char *Name) {
1242 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1243 unwrap(LHS), unwrap(RHS), Name));
1244}
1245
1246/*--.. Miscellaneous instructions ..........................................--*/
1247
1248LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1249 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1250}
1251
1252LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1253 LLVMValueRef *Args, unsigned NumArgs,
1254 const char *Name) {
1255 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1256 unwrap(Args) + NumArgs, Name));
1257}
1258
1259LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1260 LLVMValueRef Then, LLVMValueRef Else,
1261 const char *Name) {
1262 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1263 Name));
1264}
1265
1266LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1267 LLVMTypeRef Ty, const char *Name) {
1268 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1269}
1270
1271LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1272 LLVMValueRef Index, const char *Name) {
1273 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1274 Name));
1275}
1276
1277LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1278 LLVMValueRef EltVal, LLVMValueRef Index,
1279 const char *Name) {
1280 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1281 unwrap(Index), Name));
1282}
1283
1284LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1285 LLVMValueRef V2, LLVMValueRef Mask,
1286 const char *Name) {
1287 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1288 unwrap(Mask), Name));
1289}
Gordon Henriksen1ae61352007-12-12 01:04:30 +00001290
1291
1292/*===-- Module providers --------------------------------------------------===*/
1293
1294LLVMModuleProviderRef
1295LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1296 return wrap(new ExistingModuleProvider(unwrap(M)));
1297}
1298
1299void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1300 delete unwrap(MP);
1301}
1302
Gordon Henriksenda1435f2007-12-19 22:30:40 +00001303
1304/*===-- Memory buffers ----------------------------------------------------===*/
1305
1306int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1307 LLVMMemoryBufferRef *OutMemBuf,
1308 char **OutMessage) {
1309 std::string Error;
Chris Lattner038112a2008-04-01 18:04:03 +00001310 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
Gordon Henriksenda1435f2007-12-19 22:30:40 +00001311 *OutMemBuf = wrap(MB);
1312 return 0;
1313 }
1314
1315 *OutMessage = strdup(Error.c_str());
1316 return 1;
1317}
1318
1319int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1320 char **OutMessage) {
1321 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1322 *OutMemBuf = wrap(MB);
1323 return 0;
1324 }
1325
1326 *OutMessage = strdup("stdin is empty.");
1327 return 1;
1328}
1329
1330void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1331 delete unwrap(MemBuf);
1332}