blob: 9a680126e559f341508ba392653c506828af13d1 [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>
Gordon Henriksen8b94a142007-09-18 03:18:57 +000025
26using namespace llvm;
27
28
Gordon Henriksenda1435f2007-12-19 22:30:40 +000029/*===-- Error handling ----------------------------------------------------===*/
30
31void LLVMDisposeMessage(char *Message) {
32 free(Message);
33}
34
35
Gordon Henriksen8b94a142007-09-18 03:18:57 +000036/*===-- Operations on modules ---------------------------------------------===*/
37
38LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
39 return wrap(new Module(ModuleID));
40}
41
42void LLVMDisposeModule(LLVMModuleRef M) {
43 delete unwrap(M);
44}
45
Gordon Henriksena353ffa2007-12-27 20:13:47 +000046/*--.. Data layout .........................................................--*/
47const char * LLVMGetDataLayout(LLVMModuleRef M) {
48 return unwrap(M)->getDataLayout().c_str();
49}
50
51void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
52 unwrap(M)->setDataLayout(Triple);
53}
54
55/*--.. Target triple .......................................................--*/
56const char * LLVMGetTarget(LLVMModuleRef M) {
57 return unwrap(M)->getTargetTriple().c_str();
58}
59
60void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
61 unwrap(M)->setTargetTriple(Triple);
62}
63
64/*--.. Type names ..........................................................--*/
Gordon Henriksen8b94a142007-09-18 03:18:57 +000065int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
66 return unwrap(M)->addTypeName(Name, unwrap(Ty));
67}
68
Gordon Henriksen46abf912007-09-26 20:56:12 +000069void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
70 std::string N(Name);
71
72 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
73 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
74 if (I->first == N)
75 TST.remove(I);
76}
77
Gordon Henriksen8b94a142007-09-18 03:18:57 +000078
79/*===-- Operations on types -----------------------------------------------===*/
80
81/*--.. Operations on all types (mostly) ....................................--*/
82
83LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
84 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
85}
86
87void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
88 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
89 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
90}
91
92/*--.. Operations on integer types .........................................--*/
93
94LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
95LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
96LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
97LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
98LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
99
Gordon Henriksen81a78812007-10-06 16:05:20 +0000100LLVMTypeRef LLVMIntType(unsigned NumBits) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000101 return wrap(IntegerType::get(NumBits));
102}
103
Gordon Henriksen46abf912007-09-26 20:56:12 +0000104unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000105 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
106}
107
108/*--.. Operations on real types ............................................--*/
109
110LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
111LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
112LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
113LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
114LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
115
116/*--.. Operations on function types ........................................--*/
117
Gordon Henriksen81a78812007-10-06 16:05:20 +0000118LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
119 LLVMTypeRef *ParamTypes, unsigned ParamCount,
120 int IsVarArg) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000121 std::vector<const Type*> Tys;
122 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
123 Tys.push_back(unwrap(*I));
124
125 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
126}
127
128int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
129 return unwrap<FunctionType>(FunctionTy)->isVarArg();
130}
131
Gordon Henriksen46abf912007-09-26 20:56:12 +0000132LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000133 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
134}
135
Gordon Henriksen46abf912007-09-26 20:56:12 +0000136unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000137 return unwrap<FunctionType>(FunctionTy)->getNumParams();
138}
139
Gordon Henriksen46abf912007-09-26 20:56:12 +0000140void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000141 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
142 for (FunctionType::param_iterator I = Ty->param_begin(),
143 E = Ty->param_end(); I != E; ++I)
144 *Dest++ = wrap(*I);
145}
146
147/*--.. Operations on struct types ..........................................--*/
148
Gordon Henriksen81a78812007-10-06 16:05:20 +0000149LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
150 unsigned ElementCount, int Packed) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000151 std::vector<const Type*> Tys;
152 for (LLVMTypeRef *I = ElementTypes,
153 *E = ElementTypes + ElementCount; I != E; ++I)
154 Tys.push_back(unwrap(*I));
155
156 return wrap(StructType::get(Tys, Packed != 0));
157}
158
Gordon Henriksen46abf912007-09-26 20:56:12 +0000159unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000160 return unwrap<StructType>(StructTy)->getNumElements();
161}
162
163void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
164 StructType *Ty = unwrap<StructType>(StructTy);
165 for (FunctionType::param_iterator I = Ty->element_begin(),
166 E = Ty->element_end(); I != E; ++I)
167 *Dest++ = wrap(*I);
168}
169
170int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
171 return unwrap<StructType>(StructTy)->isPacked();
172}
173
174/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
175
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000176LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000177 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
178}
179
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000180LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
181 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000182}
183
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000184LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000185 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
186}
187
188LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
189 return wrap(unwrap<SequentialType>(Ty)->getElementType());
190}
191
192unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
193 return unwrap<ArrayType>(ArrayTy)->getNumElements();
194}
195
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000196unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
197 return unwrap<PointerType>(PointerTy)->getAddressSpace();
198}
199
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000200unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
201 return unwrap<VectorType>(VectorTy)->getNumElements();
202}
203
204/*--.. Operations on other types ...........................................--*/
205
206LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
207LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
208
Gordon Henriksen81a78812007-10-06 16:05:20 +0000209LLVMTypeRef LLVMOpaqueType() {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000210 return wrap(llvm::OpaqueType::get());
211}
212
Gordon Henriksen1cf08fd2007-10-07 00:13:35 +0000213/* Operations on type handles */
214
215LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
216 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
217}
218
219void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
220 delete unwrap(TypeHandle);
221}
222
223LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
224 return wrap(unwrap(TypeHandle)->get());
225}
226
227void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
228 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
229}
230
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000231
232/*===-- Operations on values ----------------------------------------------===*/
233
234/*--.. Operations on all values ............................................--*/
235
Gordon Henriksen46abf912007-09-26 20:56:12 +0000236LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000237 return wrap(unwrap(Val)->getType());
238}
239
240const char *LLVMGetValueName(LLVMValueRef Val) {
241 return unwrap(Val)->getNameStart();
242}
243
244void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
245 unwrap(Val)->setName(Name);
246}
247
Gordon Henriksen88cc6992007-10-06 00:08:49 +0000248void LLVMDumpValue(LLVMValueRef Val) {
249 unwrap(Val)->dump();
250}
251
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000252/*--.. Operations on constants of any type .................................--*/
253
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000254LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000255 return wrap(Constant::getNullValue(unwrap(Ty)));
256}
257
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000258LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000259 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
260}
261
262LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
263 return wrap(UndefValue::get(unwrap(Ty)));
264}
265
Gordon Henriksen344be5f2007-09-18 18:07:51 +0000266int LLVMIsConstant(LLVMValueRef Ty) {
267 return isa<Constant>(unwrap(Ty));
268}
269
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000270int LLVMIsNull(LLVMValueRef Val) {
271 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
272 return C->isNullValue();
273 return false;
274}
275
Gordon Henriksen344be5f2007-09-18 18:07:51 +0000276int LLVMIsUndef(LLVMValueRef Val) {
277 return isa<UndefValue>(unwrap(Val));
278}
279
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000280/*--.. Operations on scalar constants ......................................--*/
281
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000282LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
283 int SignExtend) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000284 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
285}
286
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000287LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000288 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
289}
290
291/*--.. Operations on composite constants ...................................--*/
292
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000293LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
294 int DontNullTerminate) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000295 /* Inverted the sense of AddNull because ', 0)' is a
296 better mnemonic for null termination than ', 1)'. */
297 return wrap(ConstantArray::get(std::string(Str, Length),
298 DontNullTerminate == 0));
299}
300
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000301LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
302 LLVMValueRef *ConstantVals, unsigned Length) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000303 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
304 unwrap<Constant>(ConstantVals, Length),
305 Length));
306}
307
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000308LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
309 int Packed) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000310 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
311 Count, Packed != 0));
312}
313
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000314LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000315 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
316 Size));
317}
318
Gordon Henriksen46475692007-10-06 14:29:36 +0000319/*--.. Constant expressions ................................................--*/
320
321LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
322 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
323}
324
325LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
326 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
327}
328
329LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
330 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
331}
332
333LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
334 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
335 unwrap<Constant>(RHSConstant)));
336}
337
338LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
339 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
340 unwrap<Constant>(RHSConstant)));
341}
342
343LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
344 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
345 unwrap<Constant>(RHSConstant)));
346}
347
348LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
349 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
350 unwrap<Constant>(RHSConstant)));
351}
352
353LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
354 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
355 unwrap<Constant>(RHSConstant)));
356}
357
358LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
359 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
360 unwrap<Constant>(RHSConstant)));
361}
362
363LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
364 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
365 unwrap<Constant>(RHSConstant)));
366}
367
368LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
369 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
370 unwrap<Constant>(RHSConstant)));
371}
372
373LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
374 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
375 unwrap<Constant>(RHSConstant)));
376}
377
378LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
379 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
380 unwrap<Constant>(RHSConstant)));
381}
382
383LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
384 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
385 unwrap<Constant>(RHSConstant)));
386}
387
388LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
389 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
390 unwrap<Constant>(RHSConstant)));
391}
392
393LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
394 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
395 return wrap(ConstantExpr::getICmp(Predicate,
396 unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
398}
399
400LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
401 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
402 return wrap(ConstantExpr::getFCmp(Predicate,
403 unwrap<Constant>(LHSConstant),
404 unwrap<Constant>(RHSConstant)));
405}
406
407LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
408 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
409 unwrap<Constant>(RHSConstant)));
410}
411
412LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
413 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
414 unwrap<Constant>(RHSConstant)));
415}
416
417LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
418 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
419 unwrap<Constant>(RHSConstant)));
420}
421
422LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
423 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
424 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
425 unwrap<Constant>(ConstantIndices,
426 NumIndices),
427 NumIndices));
428}
429
430LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
431 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
432 unwrap(ToType)));
433}
434
435LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
436 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
437 unwrap(ToType)));
438}
439
440LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
441 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
442 unwrap(ToType)));
443}
444
445LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
446 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
447 unwrap(ToType)));
448}
449
450LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
451 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
452 unwrap(ToType)));
453}
454
455LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
456 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
457 unwrap(ToType)));
458}
459
460LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
461 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
462 unwrap(ToType)));
463}
464
465LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
466 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
467 unwrap(ToType)));
468}
469
470LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
471 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
472 unwrap(ToType)));
473}
474
475LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
476 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
477 unwrap(ToType)));
478}
479
480LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
481 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
482 unwrap(ToType)));
483}
484
485LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
486 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
487 unwrap(ToType)));
488}
489
490LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
491 LLVMValueRef ConstantIfTrue,
492 LLVMValueRef ConstantIfFalse) {
493 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
494 unwrap<Constant>(ConstantIfTrue),
495 unwrap<Constant>(ConstantIfFalse)));
496}
497
498LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
499 LLVMValueRef IndexConstant) {
500 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
501 unwrap<Constant>(IndexConstant)));
502}
503
504LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
505 LLVMValueRef ElementValueConstant,
506 LLVMValueRef IndexConstant) {
507 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
508 unwrap<Constant>(ElementValueConstant),
509 unwrap<Constant>(IndexConstant)));
510}
511
512LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
513 LLVMValueRef VectorBConstant,
514 LLVMValueRef MaskConstant) {
515 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
516 unwrap<Constant>(VectorBConstant),
517 unwrap<Constant>(MaskConstant)));
518}
519
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000520/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
521
522int LLVMIsDeclaration(LLVMValueRef Global) {
523 return unwrap<GlobalValue>(Global)->isDeclaration();
524}
525
526LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
527 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
528}
529
530void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
531 unwrap<GlobalValue>(Global)
532 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
533}
534
535const char *LLVMGetSection(LLVMValueRef Global) {
536 return unwrap<GlobalValue>(Global)->getSection().c_str();
537}
538
539void LLVMSetSection(LLVMValueRef Global, const char *Section) {
540 unwrap<GlobalValue>(Global)->setSection(Section);
541}
542
543LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
544 return static_cast<LLVMVisibility>(
545 unwrap<GlobalValue>(Global)->getVisibility());
546}
547
548void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
549 unwrap<GlobalValue>(Global)
550 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
551}
552
553unsigned LLVMGetAlignment(LLVMValueRef Global) {
554 return unwrap<GlobalValue>(Global)->getAlignment();
555}
556
557void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
558 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
559}
560
561/*--.. Operations on global variables ......................................--*/
562
563LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
564 return wrap(new GlobalVariable(unwrap(Ty), false,
565 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
566}
567
Gordon Henriksen6d6203d2007-10-08 03:45:09 +0000568LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
569 return wrap(unwrap(M)->getNamedGlobal(Name));
570}
571
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000572void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
573 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
574}
575
576int LLVMHasInitializer(LLVMValueRef GlobalVar) {
577 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
578}
579
580LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
581 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
582}
583
584void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
585 unwrap<GlobalVariable>(GlobalVar)
586 ->setInitializer(unwrap<Constant>(ConstantVal));
587}
588
589int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
590 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
591}
592
593void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
594 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
595}
596
Gordon Henriksenc84c16b2007-10-07 17:31:42 +0000597int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksenc84c16b2007-10-07 17:31:42 +0000598 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
599}
600
601void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
602 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
603}
604
Gordon Henriksen46abf912007-09-26 20:56:12 +0000605/*--.. Operations on functions .............................................--*/
606
607LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
608 LLVMTypeRef FunctionTy) {
609 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
610 GlobalValue::ExternalLinkage, Name, unwrap(M)));
611}
612
Gordon Henriksen6d6203d2007-10-08 03:45:09 +0000613LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
614 return wrap(unwrap(M)->getFunction(Name));
615}
616
Gordon Henriksen46abf912007-09-26 20:56:12 +0000617void LLVMDeleteFunction(LLVMValueRef Fn) {
618 unwrap<Function>(Fn)->eraseFromParent();
619}
620
621unsigned LLVMCountParams(LLVMValueRef FnRef) {
622 // This function is strictly redundant to
623 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
624 return unwrap<Function>(FnRef)->getArgumentList().size();
625}
626
627LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
628 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
629 while (index --> 0)
630 AI++;
631 return wrap(AI);
632}
633
634void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
635 Function *Fn = unwrap<Function>(FnRef);
636 for (Function::arg_iterator I = Fn->arg_begin(),
637 E = Fn->arg_end(); I != E; I++)
638 *ParamRefs++ = wrap(I);
639}
640
641unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
642 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
643 return F->getIntrinsicID();
644 return 0;
645}
646
647unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
648 return unwrap<Function>(Fn)->getCallingConv();
649}
650
651void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
652 return unwrap<Function>(Fn)->setCallingConv(CC);
653}
654
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000655const char *LLVMGetCollector(LLVMValueRef Fn) {
656 Function *F = unwrap<Function>(Fn);
657 return F->hasCollector()? F->getCollector() : 0;
658}
659
660void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
661 Function *F = unwrap<Function>(Fn);
662 if (Coll)
663 F->setCollector(Coll);
664 else
665 F->clearCollector();
666}
667
Gordon Henriksen46abf912007-09-26 20:56:12 +0000668/*--.. Operations on basic blocks ..........................................--*/
669
670LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
671 return wrap(static_cast<Value*>(unwrap(Bb)));
672}
673
674int LLVMValueIsBasicBlock(LLVMValueRef Val) {
675 return isa<BasicBlock>(unwrap(Val));
676}
677
678LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
679 return wrap(unwrap<BasicBlock>(Val));
680}
681
682unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
683 return unwrap<Function>(FnRef)->getBasicBlockList().size();
684}
685
686void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
687 Function *Fn = unwrap<Function>(FnRef);
688 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
689 *BasicBlocksRefs++ = wrap(I);
690}
691
692LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
693 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
694}
695
696LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
697 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
698}
699
700LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
701 const char *Name) {
702 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
703 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
704 InsertBeforeBB));
705}
706
707void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
708 unwrap(BBRef)->eraseFromParent();
709}
710
711/*--.. Call and invoke instructions ........................................--*/
712
713unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
714 Value *V = unwrap(Instr);
715 if (CallInst *CI = dyn_cast<CallInst>(V))
716 return CI->getCallingConv();
717 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
718 return II->getCallingConv();
719 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
720 return 0;
721}
722
723void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
724 Value *V = unwrap(Instr);
725 if (CallInst *CI = dyn_cast<CallInst>(V))
726 return CI->setCallingConv(CC);
727 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
728 return II->setCallingConv(CC);
729 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
730}
731
Gordon Henriksen2618a6c2007-10-08 18:14:39 +0000732/*--.. Operations on phi nodes .............................................--*/
733
734void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
735 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
736 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
737 for (unsigned I = 0; I != Count; ++I)
738 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
739}
740
741unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
742 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
743}
744
745LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
746 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
747}
748
749LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
750 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
751}
752
Gordon Henriksen46abf912007-09-26 20:56:12 +0000753
754/*===-- Instruction builders ----------------------------------------------===*/
755
756LLVMBuilderRef LLVMCreateBuilder() {
Gordon Henriksen1d2e49c2007-12-27 18:25:59 +0000757 return wrap(new LLVMFoldingBuilder());
Gordon Henriksen46abf912007-09-26 20:56:12 +0000758}
759
760void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
761 Instruction *I = unwrap<Instruction>(Instr);
762 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
763}
764
765void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
766 BasicBlock *BB = unwrap(Block);
767 unwrap(Builder)->SetInsertPoint(BB);
768}
769
770void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
771 delete unwrap(Builder);
772}
773
774/*--.. Instruction builders ................................................--*/
775
776LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
777 return wrap(unwrap(B)->CreateRetVoid());
778}
779
780LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
781 return wrap(unwrap(B)->CreateRet(unwrap(V)));
782}
783
784LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
785 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
786}
787
788LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
789 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
790 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
791}
792
793LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
794 LLVMBasicBlockRef Else, unsigned NumCases) {
795 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
796}
797
798LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
799 LLVMValueRef *Args, unsigned NumArgs,
800 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
801 const char *Name) {
802 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
803 unwrap(Args), unwrap(Args) + NumArgs,
804 Name));
805}
806
807LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
808 return wrap(unwrap(B)->CreateUnwind());
809}
810
811LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
812 return wrap(unwrap(B)->CreateUnreachable());
813}
814
815/*--.. Arithmetic ..........................................................--*/
816
817LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
818 const char *Name) {
819 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
820}
821
822LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
823 const char *Name) {
824 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
825}
826
827LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
828 const char *Name) {
829 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
830}
831
832LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
833 const char *Name) {
834 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
835}
836
837LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
838 const char *Name) {
839 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
840}
841
842LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
843 const char *Name) {
844 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
845}
846
847LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
848 const char *Name) {
849 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
850}
851
852LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
853 const char *Name) {
854 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
855}
856
857LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
858 const char *Name) {
859 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
860}
861
862LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
863 const char *Name) {
864 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
865}
866
867LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
868 const char *Name) {
869 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
870}
871
872LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
873 const char *Name) {
874 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
875}
876
877LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
878 const char *Name) {
879 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
880}
881
882LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
883 const char *Name) {
884 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
885}
886
887LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
888 const char *Name) {
889 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
890}
891
892LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
893 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
894}
895
896LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
897 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
898}
899
900/*--.. Memory ..............................................................--*/
901
902LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
903 const char *Name) {
904 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
905}
906
907LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
908 LLVMValueRef Val, const char *Name) {
909 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
910}
911
912LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
913 const char *Name) {
914 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
915}
916
917LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
918 LLVMValueRef Val, const char *Name) {
919 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
920}
921
922LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
923 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
924}
925
926
927LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
928 const char *Name) {
929 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
930}
931
932LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
933 LLVMValueRef PointerVal) {
934 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
935}
936
937LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
938 LLVMValueRef *Indices, unsigned NumIndices,
939 const char *Name) {
940 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
941 unwrap(Indices) + NumIndices, Name));
942}
943
944/*--.. Casts ...............................................................--*/
945
946LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
947 LLVMTypeRef DestTy, const char *Name) {
948 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
949}
950
951LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
952 LLVMTypeRef DestTy, const char *Name) {
953 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
954}
955
956LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
957 LLVMTypeRef DestTy, const char *Name) {
958 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
959}
960
961LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
962 LLVMTypeRef DestTy, const char *Name) {
963 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
964}
965
966LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
967 LLVMTypeRef DestTy, const char *Name) {
968 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
969}
970
971LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
972 LLVMTypeRef DestTy, const char *Name) {
973 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
974}
975
976LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
977 LLVMTypeRef DestTy, const char *Name) {
978 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
979}
980
981LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
982 LLVMTypeRef DestTy, const char *Name) {
983 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
984}
985
986LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
987 LLVMTypeRef DestTy, const char *Name) {
988 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
989}
990
991LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
992 LLVMTypeRef DestTy, const char *Name) {
993 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
994}
995
996LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
997 LLVMTypeRef DestTy, const char *Name) {
998 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
999}
1000
1001LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1002 LLVMTypeRef DestTy, const char *Name) {
1003 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1004}
1005
1006/*--.. Comparisons .........................................................--*/
1007
1008LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1009 LLVMValueRef LHS, LLVMValueRef RHS,
1010 const char *Name) {
1011 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1012 unwrap(LHS), unwrap(RHS), Name));
1013}
1014
1015LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1016 LLVMValueRef LHS, LLVMValueRef RHS,
1017 const char *Name) {
1018 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1019 unwrap(LHS), unwrap(RHS), Name));
1020}
1021
1022/*--.. Miscellaneous instructions ..........................................--*/
1023
1024LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1025 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1026}
1027
1028LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1029 LLVMValueRef *Args, unsigned NumArgs,
1030 const char *Name) {
1031 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1032 unwrap(Args) + NumArgs, Name));
1033}
1034
1035LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1036 LLVMValueRef Then, LLVMValueRef Else,
1037 const char *Name) {
1038 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1039 Name));
1040}
1041
1042LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1043 LLVMTypeRef Ty, const char *Name) {
1044 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1045}
1046
1047LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1048 LLVMValueRef Index, const char *Name) {
1049 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1050 Name));
1051}
1052
1053LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1054 LLVMValueRef EltVal, LLVMValueRef Index,
1055 const char *Name) {
1056 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1057 unwrap(Index), Name));
1058}
1059
1060LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1061 LLVMValueRef V2, LLVMValueRef Mask,
1062 const char *Name) {
1063 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1064 unwrap(Mask), Name));
1065}
Gordon Henriksen1ae61352007-12-12 01:04:30 +00001066
1067
1068/*===-- Module providers --------------------------------------------------===*/
1069
1070LLVMModuleProviderRef
1071LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1072 return wrap(new ExistingModuleProvider(unwrap(M)));
1073}
1074
1075void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1076 delete unwrap(MP);
1077}
1078
Gordon Henriksenda1435f2007-12-19 22:30:40 +00001079
1080/*===-- Memory buffers ----------------------------------------------------===*/
1081
1082int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1083 LLVMMemoryBufferRef *OutMemBuf,
1084 char **OutMessage) {
1085 std::string Error;
1086 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1087 *OutMemBuf = wrap(MB);
1088 return 0;
1089 }
1090
1091 *OutMessage = strdup(Error.c_str());
1092 return 1;
1093}
1094
1095int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1096 char **OutMessage) {
1097 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1098 *OutMemBuf = wrap(MB);
1099 return 0;
1100 }
1101
1102 *OutMessage = strdup("stdin is empty.");
1103 return 1;
1104}
1105
1106void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1107 delete unwrap(MemBuf);
1108}