blob: a540f49de3af133f5845886a244d39cd7d78d7d4 [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 Henriksene2435da2008-04-28 17:37:06 +000023#include "llvm/Support/CallSite.h"
Gordon Henriksen8b94a142007-09-18 03:18:57 +000024#include <cassert>
Gordon Henriksenda1435f2007-12-19 22:30:40 +000025#include <cstdlib>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000026#include <cstring>
Gordon Henriksen8b94a142007-09-18 03:18:57 +000027
28using namespace llvm;
29
30
Gordon Henriksenda1435f2007-12-19 22:30:40 +000031/*===-- Error handling ----------------------------------------------------===*/
32
33void LLVMDisposeMessage(char *Message) {
34 free(Message);
35}
36
37
Gordon Henriksen8b94a142007-09-18 03:18:57 +000038/*===-- Operations on modules ---------------------------------------------===*/
39
40LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
41 return wrap(new Module(ModuleID));
42}
43
44void LLVMDisposeModule(LLVMModuleRef M) {
45 delete unwrap(M);
46}
47
Gordon Henriksena353ffa2007-12-27 20:13:47 +000048/*--.. Data layout .........................................................--*/
49const char * LLVMGetDataLayout(LLVMModuleRef M) {
50 return unwrap(M)->getDataLayout().c_str();
51}
52
53void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
54 unwrap(M)->setDataLayout(Triple);
55}
56
57/*--.. Target triple .......................................................--*/
58const char * LLVMGetTarget(LLVMModuleRef M) {
59 return unwrap(M)->getTargetTriple().c_str();
60}
61
62void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
63 unwrap(M)->setTargetTriple(Triple);
64}
65
66/*--.. Type names ..........................................................--*/
Gordon Henriksen8b94a142007-09-18 03:18:57 +000067int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
68 return unwrap(M)->addTypeName(Name, unwrap(Ty));
69}
70
Gordon Henriksen46abf912007-09-26 20:56:12 +000071void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
72 std::string N(Name);
73
74 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
75 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
76 if (I->first == N)
77 TST.remove(I);
78}
79
Gordon Henriksenaf59b102008-03-14 23:58:56 +000080void LLVMDumpModule(LLVMModuleRef M) {
81 unwrap(M)->dump();
82}
83
Gordon Henriksen8b94a142007-09-18 03:18:57 +000084
85/*===-- Operations on types -----------------------------------------------===*/
86
87/*--.. Operations on all types (mostly) ....................................--*/
88
89LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
90 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
91}
92
Gordon Henriksen8b94a142007-09-18 03:18:57 +000093/*--.. Operations on integer types .........................................--*/
94
Gordon Henriksen16c1f442008-05-04 12:55:34 +000095LLVMTypeRef LLVMInt1Type(void) { return (LLVMTypeRef) Type::Int1Ty; }
96LLVMTypeRef LLVMInt8Type(void) { return (LLVMTypeRef) Type::Int8Ty; }
97LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
98LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
99LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000100
Gordon Henriksen81a78812007-10-06 16:05:20 +0000101LLVMTypeRef LLVMIntType(unsigned NumBits) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000102 return wrap(IntegerType::get(NumBits));
103}
104
Gordon Henriksen46abf912007-09-26 20:56:12 +0000105unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000106 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
107}
108
109/*--.. Operations on real types ............................................--*/
110
Gordon Henriksen16c1f442008-05-04 12:55:34 +0000111LLVMTypeRef LLVMFloatType(void) { return (LLVMTypeRef) Type::FloatTy; }
112LLVMTypeRef LLVMDoubleType(void) { return (LLVMTypeRef) Type::DoubleTy; }
113LLVMTypeRef LLVMX86FP80Type(void) { return (LLVMTypeRef) Type::X86_FP80Ty; }
114LLVMTypeRef LLVMFP128Type(void) { return (LLVMTypeRef) Type::FP128Ty; }
115LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000116
117/*--.. Operations on function types ........................................--*/
118
Gordon Henriksen81a78812007-10-06 16:05:20 +0000119LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
120 LLVMTypeRef *ParamTypes, unsigned ParamCount,
121 int IsVarArg) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000122 std::vector<const Type*> Tys;
123 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
124 Tys.push_back(unwrap(*I));
125
126 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
127}
128
129int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
130 return unwrap<FunctionType>(FunctionTy)->isVarArg();
131}
132
Gordon Henriksen46abf912007-09-26 20:56:12 +0000133LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000134 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
135}
136
Gordon Henriksen46abf912007-09-26 20:56:12 +0000137unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000138 return unwrap<FunctionType>(FunctionTy)->getNumParams();
139}
140
Gordon Henriksen46abf912007-09-26 20:56:12 +0000141void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000142 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
143 for (FunctionType::param_iterator I = Ty->param_begin(),
144 E = Ty->param_end(); I != E; ++I)
145 *Dest++ = wrap(*I);
146}
147
148/*--.. Operations on struct types ..........................................--*/
149
Gordon Henriksen81a78812007-10-06 16:05:20 +0000150LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
151 unsigned ElementCount, int Packed) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000152 std::vector<const Type*> Tys;
153 for (LLVMTypeRef *I = ElementTypes,
154 *E = ElementTypes + ElementCount; I != E; ++I)
155 Tys.push_back(unwrap(*I));
156
157 return wrap(StructType::get(Tys, Packed != 0));
158}
159
Gordon Henriksen46abf912007-09-26 20:56:12 +0000160unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000161 return unwrap<StructType>(StructTy)->getNumElements();
162}
163
164void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
165 StructType *Ty = unwrap<StructType>(StructTy);
166 for (FunctionType::param_iterator I = Ty->element_begin(),
167 E = Ty->element_end(); I != E; ++I)
168 *Dest++ = wrap(*I);
169}
170
171int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
172 return unwrap<StructType>(StructTy)->isPacked();
173}
174
175/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
176
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000177LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000178 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
179}
180
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000181LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
182 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000183}
184
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000185LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000186 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
187}
188
189LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
190 return wrap(unwrap<SequentialType>(Ty)->getElementType());
191}
192
193unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
194 return unwrap<ArrayType>(ArrayTy)->getNumElements();
195}
196
Gordon Henriksen57cebee2007-12-17 16:08:32 +0000197unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
198 return unwrap<PointerType>(PointerTy)->getAddressSpace();
199}
200
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000201unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
202 return unwrap<VectorType>(VectorTy)->getNumElements();
203}
204
205/*--.. Operations on other types ...........................................--*/
206
Gordon Henriksen16c1f442008-05-04 12:55:34 +0000207LLVMTypeRef LLVMVoidType(void) { return (LLVMTypeRef) Type::VoidTy; }
208LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000209
Gordon Henriksen16c1f442008-05-04 12:55:34 +0000210LLVMTypeRef LLVMOpaqueType(void) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000211 return wrap(llvm::OpaqueType::get());
212}
213
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000214/*--.. Operations on type handles ..........................................--*/
Gordon Henriksen1cf08fd2007-10-07 00:13:35 +0000215
216LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
217 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
218}
219
220void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
221 delete unwrap(TypeHandle);
222}
223
224LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
225 return wrap(unwrap(TypeHandle)->get());
226}
227
228void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
229 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
230}
231
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000232
233/*===-- Operations on values ----------------------------------------------===*/
234
235/*--.. Operations on all values ............................................--*/
236
Gordon Henriksen46abf912007-09-26 20:56:12 +0000237LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000238 return wrap(unwrap(Val)->getType());
239}
240
241const char *LLVMGetValueName(LLVMValueRef Val) {
242 return unwrap(Val)->getNameStart();
243}
244
245void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
246 unwrap(Val)->setName(Name);
247}
248
Gordon Henriksen88cc6992007-10-06 00:08:49 +0000249void LLVMDumpValue(LLVMValueRef Val) {
250 unwrap(Val)->dump();
251}
252
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000253/*--.. Operations on constants of any type .................................--*/
254
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000255LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000256 return wrap(Constant::getNullValue(unwrap(Ty)));
257}
258
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000259LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000260 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
261}
262
263LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
264 return wrap(UndefValue::get(unwrap(Ty)));
265}
266
Gordon Henriksen344be5f2007-09-18 18:07:51 +0000267int LLVMIsConstant(LLVMValueRef Ty) {
268 return isa<Constant>(unwrap(Ty));
269}
270
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000271int LLVMIsNull(LLVMValueRef Val) {
272 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
273 return C->isNullValue();
274 return false;
275}
276
Gordon Henriksen344be5f2007-09-18 18:07:51 +0000277int LLVMIsUndef(LLVMValueRef Val) {
278 return isa<UndefValue>(unwrap(Val));
279}
280
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000281/*--.. Operations on scalar constants ......................................--*/
282
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000283LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
284 int SignExtend) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000285 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
286}
287
Gordon Henriksene62a8a32008-02-02 01:07:50 +0000288static const fltSemantics &SemanticsForType(Type *Ty) {
289 assert(Ty->isFloatingPoint() && "Type is not floating point!");
290 if (Ty == Type::FloatTy)
291 return APFloat::IEEEsingle;
292 if (Ty == Type::DoubleTy)
293 return APFloat::IEEEdouble;
294 if (Ty == Type::X86_FP80Ty)
295 return APFloat::x87DoubleExtended;
296 if (Ty == Type::FP128Ty)
297 return APFloat::IEEEquad;
298 if (Ty == Type::PPC_FP128Ty)
299 return APFloat::PPCDoubleDouble;
300 return APFloat::Bogus;
301}
302
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000303LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Gordon Henriksene62a8a32008-02-02 01:07:50 +0000304 APFloat APN(N);
305 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
Chris Lattnereb9c8e12008-04-20 00:26:06 +0000306 return wrap(ConstantFP::get(APN));
Gordon Henriksene62a8a32008-02-02 01:07:50 +0000307}
308
309LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Chris Lattnereb9c8e12008-04-20 00:26:06 +0000310 return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000311}
312
313/*--.. Operations on composite constants ...................................--*/
314
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000315LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
316 int DontNullTerminate) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000317 /* Inverted the sense of AddNull because ', 0)' is a
318 better mnemonic for null termination than ', 1)'. */
319 return wrap(ConstantArray::get(std::string(Str, Length),
320 DontNullTerminate == 0));
321}
322
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000323LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
324 LLVMValueRef *ConstantVals, unsigned Length) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000325 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
326 unwrap<Constant>(ConstantVals, Length),
327 Length));
328}
329
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000330LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
331 int Packed) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000332 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
333 Count, Packed != 0));
334}
335
Gordon Henriksene3b989d2007-10-06 15:11:06 +0000336LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000337 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
338 Size));
339}
340
Gordon Henriksen46475692007-10-06 14:29:36 +0000341/*--.. Constant expressions ................................................--*/
342
343LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
344 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
345}
346
347LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
348 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
349}
350
351LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
352 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
353}
354
355LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
356 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
357 unwrap<Constant>(RHSConstant)));
358}
359
360LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
361 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
362 unwrap<Constant>(RHSConstant)));
363}
364
365LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
366 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
367 unwrap<Constant>(RHSConstant)));
368}
369
370LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
371 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
372 unwrap<Constant>(RHSConstant)));
373}
374
375LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
377 unwrap<Constant>(RHSConstant)));
378}
379
380LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
381 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
382 unwrap<Constant>(RHSConstant)));
383}
384
385LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
386 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
387 unwrap<Constant>(RHSConstant)));
388}
389
390LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
391 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
392 unwrap<Constant>(RHSConstant)));
393}
394
395LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
396 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
398}
399
400LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
401 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
402 unwrap<Constant>(RHSConstant)));
403}
404
405LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
407 unwrap<Constant>(RHSConstant)));
408}
409
410LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
412 unwrap<Constant>(RHSConstant)));
413}
414
415LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
416 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417 return wrap(ConstantExpr::getICmp(Predicate,
418 unwrap<Constant>(LHSConstant),
419 unwrap<Constant>(RHSConstant)));
420}
421
422LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
423 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
424 return wrap(ConstantExpr::getFCmp(Predicate,
425 unwrap<Constant>(LHSConstant),
426 unwrap<Constant>(RHSConstant)));
427}
428
429LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
430 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
431 unwrap<Constant>(RHSConstant)));
432}
433
434LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
435 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
436 unwrap<Constant>(RHSConstant)));
437}
438
439LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
441 unwrap<Constant>(RHSConstant)));
442}
443
444LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
445 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
446 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
447 unwrap<Constant>(ConstantIndices,
448 NumIndices),
449 NumIndices));
450}
451
452LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
453 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
454 unwrap(ToType)));
455}
456
457LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
458 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
459 unwrap(ToType)));
460}
461
462LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
463 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
464 unwrap(ToType)));
465}
466
467LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
468 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
469 unwrap(ToType)));
470}
471
472LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
473 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
474 unwrap(ToType)));
475}
476
477LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
478 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
479 unwrap(ToType)));
480}
481
482LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
483 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
484 unwrap(ToType)));
485}
486
487LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
488 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
489 unwrap(ToType)));
490}
491
492LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
493 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
494 unwrap(ToType)));
495}
496
497LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
498 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
499 unwrap(ToType)));
500}
501
502LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
504 unwrap(ToType)));
505}
506
507LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
509 unwrap(ToType)));
510}
511
512LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
513 LLVMValueRef ConstantIfTrue,
514 LLVMValueRef ConstantIfFalse) {
515 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
516 unwrap<Constant>(ConstantIfTrue),
517 unwrap<Constant>(ConstantIfFalse)));
518}
519
520LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
521 LLVMValueRef IndexConstant) {
522 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
523 unwrap<Constant>(IndexConstant)));
524}
525
526LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
527 LLVMValueRef ElementValueConstant,
528 LLVMValueRef IndexConstant) {
529 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
530 unwrap<Constant>(ElementValueConstant),
531 unwrap<Constant>(IndexConstant)));
532}
533
534LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
535 LLVMValueRef VectorBConstant,
536 LLVMValueRef MaskConstant) {
537 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
538 unwrap<Constant>(VectorBConstant),
539 unwrap<Constant>(MaskConstant)));
540}
541
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000542/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
543
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000544LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
545 return wrap(unwrap<GlobalValue>(Global)->getParent());
546}
547
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000548int LLVMIsDeclaration(LLVMValueRef Global) {
549 return unwrap<GlobalValue>(Global)->isDeclaration();
550}
551
552LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
553 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
554}
555
556void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
557 unwrap<GlobalValue>(Global)
558 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
559}
560
561const char *LLVMGetSection(LLVMValueRef Global) {
562 return unwrap<GlobalValue>(Global)->getSection().c_str();
563}
564
565void LLVMSetSection(LLVMValueRef Global, const char *Section) {
566 unwrap<GlobalValue>(Global)->setSection(Section);
567}
568
569LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
570 return static_cast<LLVMVisibility>(
571 unwrap<GlobalValue>(Global)->getVisibility());
572}
573
574void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
575 unwrap<GlobalValue>(Global)
576 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
577}
578
579unsigned LLVMGetAlignment(LLVMValueRef Global) {
580 return unwrap<GlobalValue>(Global)->getAlignment();
581}
582
583void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
584 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
585}
586
587/*--.. Operations on global variables ......................................--*/
588
589LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
590 return wrap(new GlobalVariable(unwrap(Ty), false,
Gordon Henriksendc2c07a2007-12-30 17:46:33 +0000591 GlobalValue::ExternalLinkage, 0, Name,
592 unwrap(M)));
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000593}
594
Gordon Henriksen6d6203d2007-10-08 03:45:09 +0000595LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
596 return wrap(unwrap(M)->getNamedGlobal(Name));
597}
598
Gordon Henriksen34000972008-03-19 03:47:18 +0000599LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
600 Module *Mod = unwrap(M);
601 Module::global_iterator I = Mod->global_begin();
602 if (I == Mod->global_end())
603 return 0;
604 return wrap(I);
605}
606
607LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
608 Module *Mod = unwrap(M);
609 Module::global_iterator I = Mod->global_end();
610 if (I == Mod->global_begin())
611 return 0;
612 return wrap(--I);
613}
614
615LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
616 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
617 Module::global_iterator I = GV;
618 if (++I == GV->getParent()->global_end())
619 return 0;
620 return wrap(I);
621}
622
623LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
624 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
625 Module::global_iterator I = GV;
Gordon Henriksen4733be32008-03-23 22:21:29 +0000626 if (I == GV->getParent()->global_begin())
Gordon Henriksen34000972008-03-19 03:47:18 +0000627 return 0;
628 return wrap(--I);
629}
630
Gordon Henriksen8b94a142007-09-18 03:18:57 +0000631void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
632 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
633}
634
635int LLVMHasInitializer(LLVMValueRef GlobalVar) {
636 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
637}
638
639LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
640 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
641}
642
643void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
644 unwrap<GlobalVariable>(GlobalVar)
645 ->setInitializer(unwrap<Constant>(ConstantVal));
646}
647
648int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
649 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
650}
651
652void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
653 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
654}
655
Gordon Henriksenc84c16b2007-10-07 17:31:42 +0000656int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksenc84c16b2007-10-07 17:31:42 +0000657 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
658}
659
660void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
661 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
662}
663
Gordon Henriksen46abf912007-09-26 20:56:12 +0000664/*--.. Operations on functions .............................................--*/
665
666LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
667 LLVMTypeRef FunctionTy) {
Gabor Greif051a9502008-04-06 20:25:17 +0000668 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
669 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000670}
671
Gordon Henriksen6d6203d2007-10-08 03:45:09 +0000672LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
673 return wrap(unwrap(M)->getFunction(Name));
674}
675
Gordon Henriksen34000972008-03-19 03:47:18 +0000676LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
677 Module *Mod = unwrap(M);
678 Module::iterator I = Mod->begin();
679 if (I == Mod->end())
680 return 0;
681 return wrap(I);
682}
683
684LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
685 Module *Mod = unwrap(M);
686 Module::iterator I = Mod->end();
687 if (I == Mod->begin())
688 return 0;
689 return wrap(--I);
690}
691
692LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
693 Function *Func = unwrap<Function>(Fn);
694 Module::iterator I = Func;
695 if (++I == Func->getParent()->end())
696 return 0;
697 return wrap(I);
698}
699
700LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
701 Function *Func = unwrap<Function>(Fn);
702 Module::iterator I = Func;
Gordon Henriksen4733be32008-03-23 22:21:29 +0000703 if (I == Func->getParent()->begin())
Gordon Henriksen34000972008-03-19 03:47:18 +0000704 return 0;
705 return wrap(--I);
706}
707
Gordon Henriksen46abf912007-09-26 20:56:12 +0000708void LLVMDeleteFunction(LLVMValueRef Fn) {
709 unwrap<Function>(Fn)->eraseFromParent();
710}
711
Gordon Henriksen46abf912007-09-26 20:56:12 +0000712unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
713 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
714 return F->getIntrinsicID();
715 return 0;
716}
717
718unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
719 return unwrap<Function>(Fn)->getCallingConv();
720}
721
722void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
723 return unwrap<Function>(Fn)->setCallingConv(CC);
724}
725
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000726const char *LLVMGetCollector(LLVMValueRef Fn) {
727 Function *F = unwrap<Function>(Fn);
728 return F->hasCollector()? F->getCollector() : 0;
729}
730
731void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
732 Function *F = unwrap<Function>(Fn);
733 if (Coll)
734 F->setCollector(Coll);
735 else
736 F->clearCollector();
737}
738
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000739/*--.. Operations on parameters ............................................--*/
740
741unsigned LLVMCountParams(LLVMValueRef FnRef) {
742 // This function is strictly redundant to
743 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
744 return unwrap<Function>(FnRef)->getArgumentList().size();
745}
746
747void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
748 Function *Fn = unwrap<Function>(FnRef);
749 for (Function::arg_iterator I = Fn->arg_begin(),
750 E = Fn->arg_end(); I != E; I++)
751 *ParamRefs++ = wrap(I);
752}
753
754LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
755 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
756 while (index --> 0)
757 AI++;
758 return wrap(AI);
759}
760
761LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
762 return wrap(unwrap<Argument>(V)->getParent());
763}
764
Gordon Henriksen4733be32008-03-23 22:21:29 +0000765LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
766 Function *Func = unwrap<Function>(Fn);
767 Function::arg_iterator I = Func->arg_begin();
768 if (I == Func->arg_end())
769 return 0;
770 return wrap(I);
771}
772
773LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
774 Function *Func = unwrap<Function>(Fn);
775 Function::arg_iterator I = Func->arg_end();
776 if (I == Func->arg_begin())
777 return 0;
778 return wrap(--I);
779}
780
781LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
782 Argument *A = unwrap<Argument>(Arg);
783 Function::arg_iterator I = A;
784 if (++I == A->getParent()->arg_end())
785 return 0;
786 return wrap(I);
787}
788
789LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
790 Argument *A = unwrap<Argument>(Arg);
791 Function::arg_iterator I = A;
792 if (I == A->getParent()->arg_begin())
793 return 0;
794 return wrap(--I);
795}
796
Gordon Henriksene2435da2008-04-28 17:37:06 +0000797void LLVMAddParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
798 unwrap<Argument>(Arg)->addAttr(PA);
799}
800
801void LLVMRemoveParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
802 unwrap<Argument>(Arg)->removeAttr(PA);
803}
804
805void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
806 unwrap<Argument>(Arg)->addAttr(
807 ParamAttr::constructAlignmentFromInt(align));
808}
809
Gordon Henriksen46abf912007-09-26 20:56:12 +0000810/*--.. Operations on basic blocks ..........................................--*/
811
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000812LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
813 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000814}
815
816int LLVMValueIsBasicBlock(LLVMValueRef Val) {
817 return isa<BasicBlock>(unwrap(Val));
818}
819
820LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
821 return wrap(unwrap<BasicBlock>(Val));
822}
823
Gordon Henriksen4733be32008-03-23 22:21:29 +0000824LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
825 return wrap(unwrap(BB)->getParent());
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000826}
827
Gordon Henriksen46abf912007-09-26 20:56:12 +0000828unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
829 return unwrap<Function>(FnRef)->getBasicBlockList().size();
830}
831
832void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
833 Function *Fn = unwrap<Function>(FnRef);
834 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
835 *BasicBlocksRefs++ = wrap(I);
836}
837
838LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
839 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
840}
841
Gordon Henriksen34000972008-03-19 03:47:18 +0000842LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
843 Function *Func = unwrap<Function>(Fn);
844 Function::iterator I = Func->begin();
845 if (I == Func->end())
846 return 0;
847 return wrap(I);
848}
849
850LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
851 Function *Func = unwrap<Function>(Fn);
852 Function::iterator I = Func->end();
853 if (I == Func->begin())
854 return 0;
855 return wrap(--I);
856}
857
858LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
859 BasicBlock *Block = unwrap(BB);
860 Function::iterator I = Block;
861 if (++I == Block->getParent()->end())
862 return 0;
863 return wrap(I);
864}
865
866LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
867 BasicBlock *Block = unwrap(BB);
868 Function::iterator I = Block;
869 if (I == Block->getParent()->begin())
870 return 0;
871 return wrap(--I);
872}
873
Gordon Henriksen46abf912007-09-26 20:56:12 +0000874LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
Gabor Greif051a9502008-04-06 20:25:17 +0000875 return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000876}
877
878LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
879 const char *Name) {
880 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
Gabor Greif051a9502008-04-06 20:25:17 +0000881 return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
882 InsertBeforeBB));
Gordon Henriksen46abf912007-09-26 20:56:12 +0000883}
884
885void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
886 unwrap(BBRef)->eraseFromParent();
887}
888
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +0000889/*--.. Operations on instructions ..........................................--*/
890
891LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
892 return wrap(unwrap<Instruction>(Inst)->getParent());
893}
894
Gordon Henriksen34000972008-03-19 03:47:18 +0000895LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
896 BasicBlock *Block = unwrap(BB);
897 BasicBlock::iterator I = Block->begin();
898 if (I == Block->end())
899 return 0;
900 return wrap(I);
901}
902
903LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
904 BasicBlock *Block = unwrap(BB);
905 BasicBlock::iterator I = Block->end();
906 if (I == Block->begin())
907 return 0;
908 return wrap(--I);
909}
910
911LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
912 Instruction *Instr = unwrap<Instruction>(Inst);
913 BasicBlock::iterator I = Instr;
914 if (++I == Instr->getParent()->end())
915 return 0;
916 return wrap(I);
917}
918
919LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
920 Instruction *Instr = unwrap<Instruction>(Inst);
921 BasicBlock::iterator I = Instr;
922 if (I == Instr->getParent()->begin())
923 return 0;
924 return wrap(--I);
925}
926
Gordon Henriksen46abf912007-09-26 20:56:12 +0000927/*--.. Call and invoke instructions ........................................--*/
928
929unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
930 Value *V = unwrap(Instr);
931 if (CallInst *CI = dyn_cast<CallInst>(V))
932 return CI->getCallingConv();
933 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
934 return II->getCallingConv();
935 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
936 return 0;
937}
938
939void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
940 Value *V = unwrap(Instr);
941 if (CallInst *CI = dyn_cast<CallInst>(V))
942 return CI->setCallingConv(CC);
943 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
944 return II->setCallingConv(CC);
945 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
946}
947
Gordon Henriksene2435da2008-04-28 17:37:06 +0000948void LLVMAddInstrParamAttr(LLVMValueRef Instr, unsigned index,
949 LLVMParamAttr PA) {
950 CallSite Call = CallSite(unwrap<Instruction>(Instr));
951 Call.setParamAttrs(
952 Call.getParamAttrs().addAttr(index, PA));
953}
954
955void LLVMRemoveInstrParamAttr(LLVMValueRef Instr, unsigned index,
956 LLVMParamAttr PA) {
957 CallSite Call = CallSite(unwrap<Instruction>(Instr));
958 Call.setParamAttrs(
959 Call.getParamAttrs().removeAttr(index, PA));
960}
961
962void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
963 unsigned align) {
964 CallSite Call = CallSite(unwrap<Instruction>(Instr));
965 Call.setParamAttrs(
966 Call.getParamAttrs().addAttr(index,
967 ParamAttr::constructAlignmentFromInt(align)));
968}
969
Gordon Henriksen2618a6c2007-10-08 18:14:39 +0000970/*--.. Operations on phi nodes .............................................--*/
971
972void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
973 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
974 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
975 for (unsigned I = 0; I != Count; ++I)
976 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
977}
978
979unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
980 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
981}
982
983LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
984 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
985}
986
987LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
988 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
989}
990
Gordon Henriksen46abf912007-09-26 20:56:12 +0000991
992/*===-- Instruction builders ----------------------------------------------===*/
993
Gordon Henriksen16c1f442008-05-04 12:55:34 +0000994LLVMBuilderRef LLVMCreateBuilder(void) {
Duncan Sands89f6d882008-04-13 06:22:09 +0000995 return wrap(new IRBuilder());
Gordon Henriksen46abf912007-09-26 20:56:12 +0000996}
997
Gordon Henriksen34000972008-03-19 03:47:18 +0000998void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
999 LLVMValueRef Instr) {
1000 BasicBlock *BB = unwrap(Block);
1001 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1002 unwrap(Builder)->SetInsertPoint(BB, I);
1003}
1004
Gordon Henriksen46abf912007-09-26 20:56:12 +00001005void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1006 Instruction *I = unwrap<Instruction>(Instr);
1007 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1008}
1009
1010void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1011 BasicBlock *BB = unwrap(Block);
1012 unwrap(Builder)->SetInsertPoint(BB);
1013}
1014
Gordon Henriksendc1ce7b2008-03-19 01:11:35 +00001015LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1016 return wrap(unwrap(Builder)->GetInsertBlock());
1017}
1018
Gordon Henriksen46abf912007-09-26 20:56:12 +00001019void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1020 delete unwrap(Builder);
1021}
1022
1023/*--.. Instruction builders ................................................--*/
1024
1025LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1026 return wrap(unwrap(B)->CreateRetVoid());
1027}
1028
1029LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1030 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1031}
1032
1033LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1034 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1035}
1036
1037LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1038 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1039 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1040}
1041
1042LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1043 LLVMBasicBlockRef Else, unsigned NumCases) {
1044 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1045}
1046
1047LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1048 LLVMValueRef *Args, unsigned NumArgs,
1049 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1050 const char *Name) {
1051 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1052 unwrap(Args), unwrap(Args) + NumArgs,
1053 Name));
1054}
1055
1056LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1057 return wrap(unwrap(B)->CreateUnwind());
1058}
1059
1060LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1061 return wrap(unwrap(B)->CreateUnreachable());
1062}
1063
Gordon Henriksenab477cc2008-01-01 05:50:53 +00001064void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1065 LLVMBasicBlockRef Dest) {
1066 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1067}
1068
Gordon Henriksen46abf912007-09-26 20:56:12 +00001069/*--.. Arithmetic ..........................................................--*/
1070
1071LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1072 const char *Name) {
1073 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1074}
1075
1076LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1077 const char *Name) {
1078 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1079}
1080
1081LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1082 const char *Name) {
1083 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1084}
1085
1086LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1087 const char *Name) {
1088 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1089}
1090
1091LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1092 const char *Name) {
1093 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1094}
1095
1096LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1097 const char *Name) {
1098 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1099}
1100
1101LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1102 const char *Name) {
1103 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1104}
1105
1106LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1107 const char *Name) {
1108 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1109}
1110
1111LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1112 const char *Name) {
1113 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1114}
1115
1116LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1117 const char *Name) {
1118 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1119}
1120
1121LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1122 const char *Name) {
1123 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1124}
1125
1126LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1127 const char *Name) {
1128 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1129}
1130
1131LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1132 const char *Name) {
1133 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1134}
1135
1136LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1137 const char *Name) {
1138 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1139}
1140
1141LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1142 const char *Name) {
1143 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1144}
1145
1146LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1147 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1148}
1149
1150LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1151 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1152}
1153
1154/*--.. Memory ..............................................................--*/
1155
1156LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1157 const char *Name) {
1158 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1159}
1160
1161LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1162 LLVMValueRef Val, const char *Name) {
1163 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1164}
1165
1166LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1167 const char *Name) {
1168 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1169}
1170
1171LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1172 LLVMValueRef Val, const char *Name) {
1173 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1174}
1175
1176LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1177 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1178}
1179
1180
1181LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1182 const char *Name) {
1183 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1184}
1185
1186LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
1187 LLVMValueRef PointerVal) {
1188 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1189}
1190
1191LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1192 LLVMValueRef *Indices, unsigned NumIndices,
1193 const char *Name) {
1194 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1195 unwrap(Indices) + NumIndices, Name));
1196}
1197
1198/*--.. Casts ...............................................................--*/
1199
1200LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1201 LLVMTypeRef DestTy, const char *Name) {
1202 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1203}
1204
1205LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1206 LLVMTypeRef DestTy, const char *Name) {
1207 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1208}
1209
1210LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1211 LLVMTypeRef DestTy, const char *Name) {
1212 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1213}
1214
1215LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1216 LLVMTypeRef DestTy, const char *Name) {
1217 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1218}
1219
1220LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1221 LLVMTypeRef DestTy, const char *Name) {
1222 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1223}
1224
1225LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1226 LLVMTypeRef DestTy, const char *Name) {
1227 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1228}
1229
1230LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1231 LLVMTypeRef DestTy, const char *Name) {
1232 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1233}
1234
1235LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1236 LLVMTypeRef DestTy, const char *Name) {
1237 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1238}
1239
1240LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1241 LLVMTypeRef DestTy, const char *Name) {
1242 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1243}
1244
1245LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1246 LLVMTypeRef DestTy, const char *Name) {
1247 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1248}
1249
1250LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1251 LLVMTypeRef DestTy, const char *Name) {
1252 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1253}
1254
1255LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1256 LLVMTypeRef DestTy, const char *Name) {
1257 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1258}
1259
1260/*--.. Comparisons .........................................................--*/
1261
1262LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1263 LLVMValueRef LHS, LLVMValueRef RHS,
1264 const char *Name) {
1265 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1266 unwrap(LHS), unwrap(RHS), Name));
1267}
1268
1269LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1270 LLVMValueRef LHS, LLVMValueRef RHS,
1271 const char *Name) {
1272 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1273 unwrap(LHS), unwrap(RHS), Name));
1274}
1275
1276/*--.. Miscellaneous instructions ..........................................--*/
1277
1278LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1279 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1280}
1281
1282LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1283 LLVMValueRef *Args, unsigned NumArgs,
1284 const char *Name) {
1285 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1286 unwrap(Args) + NumArgs, Name));
1287}
1288
1289LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1290 LLVMValueRef Then, LLVMValueRef Else,
1291 const char *Name) {
1292 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1293 Name));
1294}
1295
1296LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1297 LLVMTypeRef Ty, const char *Name) {
1298 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1299}
1300
1301LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1302 LLVMValueRef Index, const char *Name) {
1303 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1304 Name));
1305}
1306
1307LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1308 LLVMValueRef EltVal, LLVMValueRef Index,
1309 const char *Name) {
1310 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1311 unwrap(Index), Name));
1312}
1313
1314LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1315 LLVMValueRef V2, LLVMValueRef Mask,
1316 const char *Name) {
1317 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1318 unwrap(Mask), Name));
1319}
Gordon Henriksen1ae61352007-12-12 01:04:30 +00001320
1321
1322/*===-- Module providers --------------------------------------------------===*/
1323
1324LLVMModuleProviderRef
1325LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1326 return wrap(new ExistingModuleProvider(unwrap(M)));
1327}
1328
1329void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1330 delete unwrap(MP);
1331}
1332
Gordon Henriksenda1435f2007-12-19 22:30:40 +00001333
1334/*===-- Memory buffers ----------------------------------------------------===*/
1335
1336int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1337 LLVMMemoryBufferRef *OutMemBuf,
1338 char **OutMessage) {
1339 std::string Error;
Chris Lattner038112a2008-04-01 18:04:03 +00001340 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
Gordon Henriksenda1435f2007-12-19 22:30:40 +00001341 *OutMemBuf = wrap(MB);
1342 return 0;
1343 }
1344
1345 *OutMessage = strdup(Error.c_str());
1346 return 1;
1347}
1348
1349int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1350 char **OutMessage) {
1351 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1352 *OutMemBuf = wrap(MB);
1353 return 0;
1354 }
1355
1356 *OutMessage = strdup("stdin is empty.");
1357 return 1;
1358}
1359
1360void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1361 delete unwrap(MemBuf);
1362}