~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/VMCore/Core.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===-- Core.cpp ----------------------------------------------------------===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// This file implements the C bindings for libLLVMCore.a, which implements
 
11
// the LLVM intermediate representation.
 
12
//
 
13
//===----------------------------------------------------------------------===//
 
14
 
 
15
#include "llvm-c/Core.h"
 
16
#include "llvm/Bitcode/ReaderWriter.h"
 
17
#include "llvm/Constants.h"
 
18
#include "llvm/DerivedTypes.h"
 
19
#include "llvm/GlobalVariable.h"
 
20
#include "llvm/GlobalAlias.h"
 
21
#include "llvm/LLVMContext.h"
 
22
#include "llvm/TypeSymbolTable.h"
 
23
#include "llvm/InlineAsm.h"
 
24
#include "llvm/IntrinsicInst.h"
 
25
#include "llvm/Support/CallSite.h"
 
26
#include "llvm/Support/Debug.h"
 
27
#include "llvm/Support/ErrorHandling.h"
 
28
#include "llvm/Support/MemoryBuffer.h"
 
29
#include "llvm/Support/raw_ostream.h"
 
30
#include <cassert>
 
31
#include <cstdlib>
 
32
#include <cstring>
 
33
 
 
34
using namespace llvm;
 
35
 
 
36
 
 
37
/*===-- Error handling ----------------------------------------------------===*/
 
38
 
 
39
void LLVMDisposeMessage(char *Message) {
 
40
  free(Message);
 
41
}
 
42
 
 
43
 
 
44
/*===-- Operations on contexts --------------------------------------------===*/
 
45
 
 
46
LLVMContextRef LLVMContextCreate() {
 
47
  return wrap(new LLVMContext());
 
48
}
 
49
 
 
50
LLVMContextRef LLVMGetGlobalContext() {
 
51
  return wrap(&getGlobalContext());
 
52
}
 
53
 
 
54
void LLVMContextDispose(LLVMContextRef C) {
 
55
  delete unwrap(C);
 
56
}
 
57
 
 
58
unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
 
59
                                  unsigned SLen) {
 
60
  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
 
61
}
 
62
 
 
63
unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
 
64
  return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
 
65
}
 
66
 
 
67
 
 
68
/*===-- Operations on modules ---------------------------------------------===*/
 
69
 
 
70
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
 
71
  return wrap(new Module(ModuleID, getGlobalContext()));
 
72
}
 
73
 
 
74
LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
 
75
                                                LLVMContextRef C) {
 
76
  return wrap(new Module(ModuleID, *unwrap(C)));
 
77
}
 
78
 
 
79
void LLVMDisposeModule(LLVMModuleRef M) {
 
80
  delete unwrap(M);
 
81
}
 
82
 
 
83
/*--.. Data layout .........................................................--*/
 
84
const char * LLVMGetDataLayout(LLVMModuleRef M) {
 
85
  return unwrap(M)->getDataLayout().c_str();
 
86
}
 
87
 
 
88
void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
 
89
  unwrap(M)->setDataLayout(Triple);
 
90
}
 
91
 
 
92
/*--.. Target triple .......................................................--*/
 
93
const char * LLVMGetTarget(LLVMModuleRef M) {
 
94
  return unwrap(M)->getTargetTriple().c_str();
 
95
}
 
96
 
 
97
void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
 
98
  unwrap(M)->setTargetTriple(Triple);
 
99
}
 
100
 
 
101
/*--.. Type names ..........................................................--*/
 
102
LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
 
103
  return unwrap(M)->addTypeName(Name, unwrap(Ty));
 
104
}
 
105
 
 
106
void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
 
107
  TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
 
108
 
 
109
  TypeSymbolTable::iterator I = TST.find(Name);
 
110
  if (I != TST.end())
 
111
    TST.remove(I);
 
112
}
 
113
 
 
114
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
 
115
  return wrap(unwrap(M)->getTypeByName(Name));
 
116
}
 
117
 
 
118
void LLVMDumpModule(LLVMModuleRef M) {
 
119
  unwrap(M)->dump();
 
120
}
 
121
 
 
122
 
 
123
/*===-- Operations on types -----------------------------------------------===*/
 
124
 
 
125
/*--.. Operations on all types (mostly) ....................................--*/
 
126
 
 
127
LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
 
128
  switch (unwrap(Ty)->getTypeID()) {
 
129
  default:
 
130
    assert(false && "Unhandled TypeID.");
 
131
  case Type::VoidTyID:
 
132
    return LLVMVoidTypeKind;
 
133
  case Type::FloatTyID:
 
134
    return LLVMFloatTypeKind;
 
135
  case Type::DoubleTyID:
 
136
    return LLVMDoubleTypeKind;
 
137
  case Type::X86_FP80TyID:
 
138
    return LLVMX86_FP80TypeKind;
 
139
  case Type::FP128TyID:
 
140
    return LLVMFP128TypeKind;
 
141
  case Type::PPC_FP128TyID:
 
142
    return LLVMPPC_FP128TypeKind;
 
143
  case Type::LabelTyID:
 
144
    return LLVMLabelTypeKind;
 
145
  case Type::MetadataTyID:
 
146
    return LLVMMetadataTypeKind;
 
147
  case Type::IntegerTyID:
 
148
    return LLVMIntegerTypeKind;
 
149
  case Type::FunctionTyID:
 
150
    return LLVMFunctionTypeKind;
 
151
  case Type::StructTyID:
 
152
    return LLVMStructTypeKind;
 
153
  case Type::UnionTyID:
 
154
    return LLVMUnionTypeKind;
 
155
  case Type::ArrayTyID:
 
156
    return LLVMArrayTypeKind;
 
157
  case Type::PointerTyID:
 
158
    return LLVMPointerTypeKind;
 
159
  case Type::OpaqueTyID:
 
160
    return LLVMOpaqueTypeKind;
 
161
  case Type::VectorTyID:
 
162
    return LLVMVectorTypeKind;
 
163
  }
 
164
}
 
165
 
 
166
LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
 
167
  return wrap(&unwrap(Ty)->getContext());
 
168
}
 
169
 
 
170
/*--.. Operations on integer types .........................................--*/
 
171
 
 
172
LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
 
173
  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
 
174
}
 
175
LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
 
176
  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
 
177
}
 
178
LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
 
179
  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
 
180
}
 
181
LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
 
182
  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
 
183
}
 
184
LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
 
185
  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
 
186
}
 
187
LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
 
188
  return wrap(IntegerType::get(*unwrap(C), NumBits));
 
189
}
 
190
 
 
191
LLVMTypeRef LLVMInt1Type(void)  {
 
192
  return LLVMInt1TypeInContext(LLVMGetGlobalContext());
 
193
}
 
194
LLVMTypeRef LLVMInt8Type(void)  {
 
195
  return LLVMInt8TypeInContext(LLVMGetGlobalContext());
 
196
}
 
197
LLVMTypeRef LLVMInt16Type(void) {
 
198
  return LLVMInt16TypeInContext(LLVMGetGlobalContext());
 
199
}
 
200
LLVMTypeRef LLVMInt32Type(void) {
 
201
  return LLVMInt32TypeInContext(LLVMGetGlobalContext());
 
202
}
 
203
LLVMTypeRef LLVMInt64Type(void) {
 
204
  return LLVMInt64TypeInContext(LLVMGetGlobalContext());
 
205
}
 
206
LLVMTypeRef LLVMIntType(unsigned NumBits) {
 
207
  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
 
208
}
 
209
 
 
210
unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
 
211
  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
 
212
}
 
213
 
 
214
/*--.. Operations on real types ............................................--*/
 
215
 
 
216
LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
 
217
  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
 
218
}
 
219
LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
 
220
  return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
 
221
}
 
222
LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
 
223
  return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
 
224
}
 
225
LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
 
226
  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
 
227
}
 
228
LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
 
229
  return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
 
230
}
 
231
 
 
232
LLVMTypeRef LLVMFloatType(void) {
 
233
  return LLVMFloatTypeInContext(LLVMGetGlobalContext());
 
234
}
 
235
LLVMTypeRef LLVMDoubleType(void) {
 
236
  return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
 
237
}
 
238
LLVMTypeRef LLVMX86FP80Type(void) {
 
239
  return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
 
240
}
 
241
LLVMTypeRef LLVMFP128Type(void) {
 
242
  return LLVMFP128TypeInContext(LLVMGetGlobalContext());
 
243
}
 
244
LLVMTypeRef LLVMPPCFP128Type(void) {
 
245
  return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
 
246
}
 
247
 
 
248
/*--.. Operations on function types ........................................--*/
 
249
 
 
250
LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
 
251
                             LLVMTypeRef *ParamTypes, unsigned ParamCount,
 
252
                             LLVMBool IsVarArg) {
 
253
  std::vector<const Type*> Tys;
 
254
  for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
 
255
    Tys.push_back(unwrap(*I));
 
256
  
 
257
  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
 
258
}
 
259
 
 
260
LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
 
261
  return unwrap<FunctionType>(FunctionTy)->isVarArg();
 
262
}
 
263
 
 
264
LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
 
265
  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
 
266
}
 
267
 
 
268
unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
 
269
  return unwrap<FunctionType>(FunctionTy)->getNumParams();
 
270
}
 
271
 
 
272
void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
 
273
  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
 
274
  for (FunctionType::param_iterator I = Ty->param_begin(),
 
275
                                    E = Ty->param_end(); I != E; ++I)
 
276
    *Dest++ = wrap(*I);
 
277
}
 
278
 
 
279
/*--.. Operations on struct types ..........................................--*/
 
280
 
 
281
LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
 
282
                           unsigned ElementCount, LLVMBool Packed) {
 
283
  std::vector<const Type*> Tys;
 
284
  for (LLVMTypeRef *I = ElementTypes,
 
285
                   *E = ElementTypes + ElementCount; I != E; ++I)
 
286
    Tys.push_back(unwrap(*I));
 
287
  
 
288
  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
 
289
}
 
290
 
 
291
LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
 
292
                           unsigned ElementCount, LLVMBool Packed) {
 
293
  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
 
294
                                 ElementCount, Packed);
 
295
}
 
296
 
 
297
 
 
298
unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
 
299
  return unwrap<StructType>(StructTy)->getNumElements();
 
300
}
 
301
 
 
302
void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
 
303
  StructType *Ty = unwrap<StructType>(StructTy);
 
304
  for (FunctionType::param_iterator I = Ty->element_begin(),
 
305
                                    E = Ty->element_end(); I != E; ++I)
 
306
    *Dest++ = wrap(*I);
 
307
}
 
308
 
 
309
LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
 
310
  return unwrap<StructType>(StructTy)->isPacked();
 
311
}
 
312
 
 
313
/*--.. Operations on union types ..........................................--*/
 
314
 
 
315
LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
 
316
                                   unsigned ElementCount) {
 
317
  SmallVector<const Type*, 8> Tys;
 
318
  for (LLVMTypeRef *I = ElementTypes,
 
319
                   *E = ElementTypes + ElementCount; I != E; ++I)
 
320
    Tys.push_back(unwrap(*I));
 
321
  
 
322
  return wrap(UnionType::get(&Tys[0], Tys.size()));
 
323
}
 
324
 
 
325
LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes,
 
326
                           unsigned ElementCount, int Packed) {
 
327
  return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
 
328
                                ElementCount);
 
329
}
 
330
 
 
331
unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
 
332
  return unwrap<UnionType>(UnionTy)->getNumElements();
 
333
}
 
334
 
 
335
void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
 
336
  UnionType *Ty = unwrap<UnionType>(UnionTy);
 
337
  for (FunctionType::param_iterator I = Ty->element_begin(),
 
338
                                    E = Ty->element_end(); I != E; ++I)
 
339
    *Dest++ = wrap(*I);
 
340
}
 
341
 
 
342
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
 
343
 
 
344
LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
 
345
  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
 
346
}
 
347
 
 
348
LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
 
349
  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
 
350
}
 
351
 
 
352
LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
 
353
  return wrap(VectorType::get(unwrap(ElementType), ElementCount));
 
354
}
 
355
 
 
356
LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
 
357
  return wrap(unwrap<SequentialType>(Ty)->getElementType());
 
358
}
 
359
 
 
360
unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
 
361
  return unwrap<ArrayType>(ArrayTy)->getNumElements();
 
362
}
 
363
 
 
364
unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
 
365
  return unwrap<PointerType>(PointerTy)->getAddressSpace();
 
366
}
 
367
 
 
368
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
 
369
  return unwrap<VectorType>(VectorTy)->getNumElements();
 
370
}
 
371
 
 
372
/*--.. Operations on other types ...........................................--*/
 
373
 
 
374
LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
 
375
  return wrap(Type::getVoidTy(*unwrap(C)));
 
376
}
 
377
LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
 
378
  return wrap(Type::getLabelTy(*unwrap(C)));
 
379
}
 
380
LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
 
381
  return wrap(OpaqueType::get(*unwrap(C)));
 
382
}
 
383
 
 
384
LLVMTypeRef LLVMVoidType(void)  {
 
385
  return LLVMVoidTypeInContext(LLVMGetGlobalContext());
 
386
}
 
387
LLVMTypeRef LLVMLabelType(void) {
 
388
  return LLVMLabelTypeInContext(LLVMGetGlobalContext());
 
389
}
 
390
LLVMTypeRef LLVMOpaqueType(void) {
 
391
  return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
 
392
}
 
393
 
 
394
/*--.. Operations on type handles ..........................................--*/
 
395
 
 
396
LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
 
397
  return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
 
398
}
 
399
 
 
400
void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
 
401
  delete unwrap(TypeHandle);
 
402
}
 
403
 
 
404
LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
 
405
  return wrap(unwrap(TypeHandle)->get());
 
406
}
 
407
 
 
408
void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
 
409
  unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
 
410
}
 
411
 
 
412
 
 
413
/*===-- Operations on values ----------------------------------------------===*/
 
414
 
 
415
/*--.. Operations on all values ............................................--*/
 
416
 
 
417
LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
 
418
  return wrap(unwrap(Val)->getType());
 
419
}
 
420
 
 
421
const char *LLVMGetValueName(LLVMValueRef Val) {
 
422
  return unwrap(Val)->getName().data();
 
423
}
 
424
 
 
425
void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
 
426
  unwrap(Val)->setName(Name);
 
427
}
 
428
 
 
429
void LLVMDumpValue(LLVMValueRef Val) {
 
430
  unwrap(Val)->dump();
 
431
}
 
432
 
 
433
void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
 
434
  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
 
435
}
 
436
 
 
437
int LLVMHasMetadata(LLVMValueRef Inst) {
 
438
  return unwrap<Instruction>(Inst)->hasMetadata();
 
439
}
 
440
 
 
441
LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
 
442
  return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
 
443
}
 
444
 
 
445
void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
 
446
  unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
 
447
}
 
448
 
 
449
/*--.. Conversion functions ................................................--*/
 
450
 
 
451
#define LLVM_DEFINE_VALUE_CAST(name)                                       \
 
452
  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
 
453
    return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
 
454
  }
 
455
 
 
456
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
 
457
 
 
458
/*--.. Operations on Uses ..................................................--*/
 
459
LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
 
460
  Value *V = unwrap(Val);
 
461
  Value::use_iterator I = V->use_begin();
 
462
  if (I == V->use_end())
 
463
    return 0;
 
464
  return wrap(&(I.getUse()));
 
465
}
 
466
 
 
467
LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
 
468
  Use *Next = unwrap(U)->getNext();
 
469
  if (Next)
 
470
    return wrap(Next);
 
471
  return 0;
 
472
}
 
473
 
 
474
LLVMValueRef LLVMGetUser(LLVMUseRef U) {
 
475
  return wrap(unwrap(U)->getUser());
 
476
}
 
477
 
 
478
LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
 
479
  return wrap(unwrap(U)->get());
 
480
}
 
481
 
 
482
/*--.. Operations on Users .................................................--*/
 
483
LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
 
484
  return wrap(unwrap<User>(Val)->getOperand(Index));
 
485
}
 
486
 
 
487
/*--.. Operations on constants of any type .................................--*/
 
488
 
 
489
LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
 
490
  return wrap(Constant::getNullValue(unwrap(Ty)));
 
491
}
 
492
 
 
493
LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
 
494
  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
 
495
}
 
496
 
 
497
LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
 
498
  return wrap(UndefValue::get(unwrap(Ty)));
 
499
}
 
500
 
 
501
LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
 
502
  return isa<Constant>(unwrap(Ty));
 
503
}
 
504
 
 
505
LLVMBool LLVMIsNull(LLVMValueRef Val) {
 
506
  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
 
507
    return C->isNullValue();
 
508
  return false;
 
509
}
 
510
 
 
511
LLVMBool LLVMIsUndef(LLVMValueRef Val) {
 
512
  return isa<UndefValue>(unwrap(Val));
 
513
}
 
514
 
 
515
LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
 
516
  return
 
517
      wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
 
518
}
 
519
 
 
520
/*--.. Operations on metadata nodes ........................................--*/
 
521
 
 
522
LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
 
523
                                   unsigned SLen) {
 
524
  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
 
525
}
 
526
 
 
527
LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
 
528
  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
 
529
}
 
530
 
 
531
LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
 
532
                                 unsigned Count) {
 
533
  return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
 
534
}
 
535
 
 
536
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
 
537
  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
 
538
}
 
539
 
 
540
/*--.. Operations on scalar constants ......................................--*/
 
541
 
 
542
LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
 
543
                          LLVMBool SignExtend) {
 
544
  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
 
545
}
 
546
 
 
547
LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
 
548
                                  uint8_t Radix) {
 
549
  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
 
550
                               Radix));
 
551
}
 
552
 
 
553
LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
 
554
                                         unsigned SLen, uint8_t Radix) {
 
555
  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
 
556
                               Radix));
 
557
}
 
558
 
 
559
LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
 
560
  return wrap(ConstantFP::get(unwrap(RealTy), N));
 
561
}
 
562
 
 
563
LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
 
564
  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
 
565
}
 
566
 
 
567
LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
 
568
                                          unsigned SLen) {
 
569
  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
 
570
}
 
571
 
 
572
unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
 
573
  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
 
574
}
 
575
 
 
576
long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
 
577
  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
 
578
}
 
579
 
 
580
/*--.. Operations on composite constants ...................................--*/
 
581
 
 
582
LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
 
583
                                      unsigned Length,
 
584
                                      LLVMBool DontNullTerminate) {
 
585
  /* Inverted the sense of AddNull because ', 0)' is a
 
586
     better mnemonic for null termination than ', 1)'. */
 
587
  return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
 
588
                                 DontNullTerminate == 0));
 
589
}
 
590
LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
 
591
                                      LLVMValueRef *ConstantVals,
 
592
                                      unsigned Count, LLVMBool Packed) {
 
593
  return wrap(ConstantStruct::get(*unwrap(C),
 
594
                                  unwrap<Constant>(ConstantVals, Count),
 
595
                                  Count, Packed != 0));
 
596
}
 
597
 
 
598
LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
 
599
                             LLVMBool DontNullTerminate) {
 
600
  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
 
601
                                  DontNullTerminate);
 
602
}
 
603
LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
 
604
                            LLVMValueRef *ConstantVals, unsigned Length) {
 
605
  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
 
606
                                 unwrap<Constant>(ConstantVals, Length),
 
607
                                 Length));
 
608
}
 
609
LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
 
610
                             LLVMBool Packed) {
 
611
  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
 
612
                                  Packed);
 
613
}
 
614
LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
 
615
  return wrap(ConstantVector::get(
 
616
                            unwrap<Constant>(ScalarConstantVals, Size), Size));
 
617
}
 
618
LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
 
619
  return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
 
620
}
 
621
 
 
622
/*--.. Constant expressions ................................................--*/
 
623
 
 
624
LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
 
625
  return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
 
626
}
 
627
 
 
628
LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
 
629
  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
 
630
}
 
631
 
 
632
LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
 
633
  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
 
634
}
 
635
 
 
636
LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
 
637
  return wrap(ConstantExpr::getNeg(
 
638
                                   unwrap<Constant>(ConstantVal)));
 
639
}
 
640
 
 
641
LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
 
642
  return wrap(ConstantExpr::getNSWNeg(
 
643
                                      unwrap<Constant>(ConstantVal)));
 
644
}
 
645
 
 
646
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
 
647
  return wrap(ConstantExpr::getNUWNeg(
 
648
                                      unwrap<Constant>(ConstantVal)));
 
649
}
 
650
 
 
651
 
 
652
LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
 
653
  return wrap(ConstantExpr::getFNeg(
 
654
                                    unwrap<Constant>(ConstantVal)));
 
655
}
 
656
 
 
657
LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
 
658
  return wrap(ConstantExpr::getNot(
 
659
                                   unwrap<Constant>(ConstantVal)));
 
660
}
 
661
 
 
662
LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
663
  return wrap(ConstantExpr::getAdd(
 
664
                                   unwrap<Constant>(LHSConstant),
 
665
                                   unwrap<Constant>(RHSConstant)));
 
666
}
 
667
 
 
668
LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
 
669
                             LLVMValueRef RHSConstant) {
 
670
  return wrap(ConstantExpr::getNSWAdd(
 
671
                                      unwrap<Constant>(LHSConstant),
 
672
                                      unwrap<Constant>(RHSConstant)));
 
673
}
 
674
 
 
675
LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
 
676
                             LLVMValueRef RHSConstant) {
 
677
  return wrap(ConstantExpr::getNUWAdd(
 
678
                                      unwrap<Constant>(LHSConstant),
 
679
                                      unwrap<Constant>(RHSConstant)));
 
680
}
 
681
 
 
682
LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
683
  return wrap(ConstantExpr::getFAdd(
 
684
                                    unwrap<Constant>(LHSConstant),
 
685
                                    unwrap<Constant>(RHSConstant)));
 
686
}
 
687
 
 
688
LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
689
  return wrap(ConstantExpr::getSub(
 
690
                                   unwrap<Constant>(LHSConstant),
 
691
                                   unwrap<Constant>(RHSConstant)));
 
692
}
 
693
 
 
694
LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
 
695
                             LLVMValueRef RHSConstant) {
 
696
  return wrap(ConstantExpr::getNSWSub(
 
697
                                      unwrap<Constant>(LHSConstant),
 
698
                                      unwrap<Constant>(RHSConstant)));
 
699
}
 
700
 
 
701
LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
 
702
                             LLVMValueRef RHSConstant) {
 
703
  return wrap(ConstantExpr::getNUWSub(
 
704
                                      unwrap<Constant>(LHSConstant),
 
705
                                      unwrap<Constant>(RHSConstant)));
 
706
}
 
707
 
 
708
LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
709
  return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
 
710
                                    unwrap<Constant>(RHSConstant)));
 
711
}
 
712
 
 
713
LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
714
  return wrap(ConstantExpr::getMul(
 
715
                                   unwrap<Constant>(LHSConstant),
 
716
                                   unwrap<Constant>(RHSConstant)));
 
717
}
 
718
 
 
719
LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
 
720
                             LLVMValueRef RHSConstant) {
 
721
  return wrap(ConstantExpr::getNSWMul(
 
722
                                      unwrap<Constant>(LHSConstant),
 
723
                                      unwrap<Constant>(RHSConstant)));
 
724
}
 
725
 
 
726
LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
 
727
                             LLVMValueRef RHSConstant) {
 
728
  return wrap(ConstantExpr::getNUWMul(
 
729
                                      unwrap<Constant>(LHSConstant),
 
730
                                      unwrap<Constant>(RHSConstant)));
 
731
}
 
732
 
 
733
LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
734
  return wrap(ConstantExpr::getFMul(
 
735
                                    unwrap<Constant>(LHSConstant),
 
736
                                    unwrap<Constant>(RHSConstant)));
 
737
}
 
738
 
 
739
LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
740
  return wrap(ConstantExpr::getUDiv(
 
741
                                    unwrap<Constant>(LHSConstant),
 
742
                                    unwrap<Constant>(RHSConstant)));
 
743
}
 
744
 
 
745
LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
746
  return wrap(ConstantExpr::getSDiv(
 
747
                                    unwrap<Constant>(LHSConstant),
 
748
                                    unwrap<Constant>(RHSConstant)));
 
749
}
 
750
 
 
751
LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
 
752
                                LLVMValueRef RHSConstant) {
 
753
  return wrap(ConstantExpr::getExactSDiv(
 
754
                                         unwrap<Constant>(LHSConstant),
 
755
                                         unwrap<Constant>(RHSConstant)));
 
756
}
 
757
 
 
758
LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
759
  return wrap(ConstantExpr::getFDiv(
 
760
                                    unwrap<Constant>(LHSConstant),
 
761
                                    unwrap<Constant>(RHSConstant)));
 
762
}
 
763
 
 
764
LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
765
  return wrap(ConstantExpr::getURem(
 
766
                                    unwrap<Constant>(LHSConstant),
 
767
                                    unwrap<Constant>(RHSConstant)));
 
768
}
 
769
 
 
770
LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
771
  return wrap(ConstantExpr::getSRem(
 
772
                                    unwrap<Constant>(LHSConstant),
 
773
                                    unwrap<Constant>(RHSConstant)));
 
774
}
 
775
 
 
776
LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
777
  return wrap(ConstantExpr::getFRem(
 
778
                                    unwrap<Constant>(LHSConstant),
 
779
                                    unwrap<Constant>(RHSConstant)));
 
780
}
 
781
 
 
782
LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
783
  return wrap(ConstantExpr::getAnd(
 
784
                                   unwrap<Constant>(LHSConstant),
 
785
                                   unwrap<Constant>(RHSConstant)));
 
786
}
 
787
 
 
788
LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
789
  return wrap(ConstantExpr::getOr(
 
790
                                  unwrap<Constant>(LHSConstant),
 
791
                                  unwrap<Constant>(RHSConstant)));
 
792
}
 
793
 
 
794
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
795
  return wrap(ConstantExpr::getXor(
 
796
                                   unwrap<Constant>(LHSConstant),
 
797
                                   unwrap<Constant>(RHSConstant)));
 
798
}
 
799
 
 
800
LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
 
801
                           LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
802
  return wrap(ConstantExpr::getICmp(Predicate,
 
803
                                    unwrap<Constant>(LHSConstant),
 
804
                                    unwrap<Constant>(RHSConstant)));
 
805
}
 
806
 
 
807
LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
 
808
                           LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
809
  return wrap(ConstantExpr::getFCmp(Predicate,
 
810
                                    unwrap<Constant>(LHSConstant),
 
811
                                    unwrap<Constant>(RHSConstant)));
 
812
}
 
813
 
 
814
LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
815
  return wrap(ConstantExpr::getShl(
 
816
                                  unwrap<Constant>(LHSConstant),
 
817
                                  unwrap<Constant>(RHSConstant)));
 
818
}
 
819
 
 
820
LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
821
  return wrap(ConstantExpr::getLShr(
 
822
                                    unwrap<Constant>(LHSConstant),
 
823
                                    unwrap<Constant>(RHSConstant)));
 
824
}
 
825
 
 
826
LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
 
827
  return wrap(ConstantExpr::getAShr(
 
828
                                    unwrap<Constant>(LHSConstant),
 
829
                                    unwrap<Constant>(RHSConstant)));
 
830
}
 
831
 
 
832
LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
 
833
                          LLVMValueRef *ConstantIndices, unsigned NumIndices) {
 
834
  return wrap(ConstantExpr::getGetElementPtr(
 
835
                                             unwrap<Constant>(ConstantVal),
 
836
                                             unwrap<Constant>(ConstantIndices, 
 
837
                                                              NumIndices),
 
838
                                             NumIndices));
 
839
}
 
840
 
 
841
LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
 
842
                                  LLVMValueRef *ConstantIndices,
 
843
                                  unsigned NumIndices) {
 
844
  Constant* Val = unwrap<Constant>(ConstantVal);
 
845
  Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
 
846
  return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
 
847
}
 
848
 
 
849
LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
850
  return wrap(ConstantExpr::getTrunc(
 
851
                                     unwrap<Constant>(ConstantVal),
 
852
                                     unwrap(ToType)));
 
853
}
 
854
 
 
855
LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
856
  return wrap(ConstantExpr::getSExt(
 
857
                                    unwrap<Constant>(ConstantVal),
 
858
                                    unwrap(ToType)));
 
859
}
 
860
 
 
861
LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
862
  return wrap(ConstantExpr::getZExt(
 
863
                                    unwrap<Constant>(ConstantVal),
 
864
                                    unwrap(ToType)));
 
865
}
 
866
 
 
867
LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
868
  return wrap(ConstantExpr::getFPTrunc(
 
869
                                       unwrap<Constant>(ConstantVal),
 
870
                                       unwrap(ToType)));
 
871
}
 
872
 
 
873
LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
874
  return wrap(ConstantExpr::getFPExtend(
 
875
                                        unwrap<Constant>(ConstantVal),
 
876
                                        unwrap(ToType)));
 
877
}
 
878
 
 
879
LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
880
  return wrap(ConstantExpr::getUIToFP(
 
881
                                      unwrap<Constant>(ConstantVal),
 
882
                                      unwrap(ToType)));
 
883
}
 
884
 
 
885
LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
886
  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
 
887
                                      unwrap(ToType)));
 
888
}
 
889
 
 
890
LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
891
  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
 
892
                                      unwrap(ToType)));
 
893
}
 
894
 
 
895
LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
896
  return wrap(ConstantExpr::getFPToSI(
 
897
                                      unwrap<Constant>(ConstantVal),
 
898
                                      unwrap(ToType)));
 
899
}
 
900
 
 
901
LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
902
  return wrap(ConstantExpr::getPtrToInt(
 
903
                                        unwrap<Constant>(ConstantVal),
 
904
                                        unwrap(ToType)));
 
905
}
 
906
 
 
907
LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
908
  return wrap(ConstantExpr::getIntToPtr(
 
909
                                        unwrap<Constant>(ConstantVal),
 
910
                                        unwrap(ToType)));
 
911
}
 
912
 
 
913
LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
914
  return wrap(ConstantExpr::getBitCast(
 
915
                                       unwrap<Constant>(ConstantVal),
 
916
                                       unwrap(ToType)));
 
917
}
 
918
 
 
919
LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
 
920
                                    LLVMTypeRef ToType) {
 
921
  return wrap(ConstantExpr::getZExtOrBitCast(
 
922
                                             unwrap<Constant>(ConstantVal),
 
923
                                             unwrap(ToType)));
 
924
}
 
925
 
 
926
LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
 
927
                                    LLVMTypeRef ToType) {
 
928
  return wrap(ConstantExpr::getSExtOrBitCast(
 
929
                                             unwrap<Constant>(ConstantVal),
 
930
                                             unwrap(ToType)));
 
931
}
 
932
 
 
933
LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
 
934
                                     LLVMTypeRef ToType) {
 
935
  return wrap(ConstantExpr::getTruncOrBitCast(
 
936
                                              unwrap<Constant>(ConstantVal),
 
937
                                              unwrap(ToType)));
 
938
}
 
939
 
 
940
LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
 
941
                                  LLVMTypeRef ToType) {
 
942
  return wrap(ConstantExpr::getPointerCast(
 
943
                                           unwrap<Constant>(ConstantVal),
 
944
                                           unwrap(ToType)));
 
945
}
 
946
 
 
947
LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
 
948
                              LLVMBool isSigned) {
 
949
  return wrap(ConstantExpr::getIntegerCast(
 
950
                                           unwrap<Constant>(ConstantVal),
 
951
                                           unwrap(ToType),
 
952
                                           isSigned));
 
953
}
 
954
 
 
955
LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
 
956
  return wrap(ConstantExpr::getFPCast(
 
957
                                      unwrap<Constant>(ConstantVal),
 
958
                                      unwrap(ToType)));
 
959
}
 
960
 
 
961
LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
 
962
                             LLVMValueRef ConstantIfTrue,
 
963
                             LLVMValueRef ConstantIfFalse) {
 
964
  return wrap(ConstantExpr::getSelect(
 
965
                                      unwrap<Constant>(ConstantCondition),
 
966
                                      unwrap<Constant>(ConstantIfTrue),
 
967
                                      unwrap<Constant>(ConstantIfFalse)));
 
968
}
 
969
 
 
970
LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
 
971
                                     LLVMValueRef IndexConstant) {
 
972
  return wrap(ConstantExpr::getExtractElement(
 
973
                                              unwrap<Constant>(VectorConstant),
 
974
                                              unwrap<Constant>(IndexConstant)));
 
975
}
 
976
 
 
977
LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
 
978
                                    LLVMValueRef ElementValueConstant,
 
979
                                    LLVMValueRef IndexConstant) {
 
980
  return wrap(ConstantExpr::getInsertElement(
 
981
                                         unwrap<Constant>(VectorConstant),
 
982
                                         unwrap<Constant>(ElementValueConstant),
 
983
                                             unwrap<Constant>(IndexConstant)));
 
984
}
 
985
 
 
986
LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
 
987
                                    LLVMValueRef VectorBConstant,
 
988
                                    LLVMValueRef MaskConstant) {
 
989
  return wrap(ConstantExpr::getShuffleVector(
 
990
                                             unwrap<Constant>(VectorAConstant),
 
991
                                             unwrap<Constant>(VectorBConstant),
 
992
                                             unwrap<Constant>(MaskConstant)));
 
993
}
 
994
 
 
995
LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
 
996
                                   unsigned NumIdx) {
 
997
  return wrap(ConstantExpr::getExtractValue(
 
998
                                            unwrap<Constant>(AggConstant),
 
999
                                            IdxList, NumIdx));
 
1000
}
 
1001
 
 
1002
LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
 
1003
                                  LLVMValueRef ElementValueConstant,
 
1004
                                  unsigned *IdxList, unsigned NumIdx) {
 
1005
  return wrap(ConstantExpr::getInsertValue(
 
1006
                                         unwrap<Constant>(AggConstant),
 
1007
                                         unwrap<Constant>(ElementValueConstant),
 
1008
                                           IdxList, NumIdx));
 
1009
}
 
1010
 
 
1011
LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
 
1012
                                const char *Constraints,
 
1013
                                LLVMBool HasSideEffects,
 
1014
                                LLVMBool IsAlignStack) {
 
1015
  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
 
1016
                             Constraints, HasSideEffects, IsAlignStack));
 
1017
}
 
1018
 
 
1019
LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
 
1020
  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
 
1021
}
 
1022
 
 
1023
/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
 
1024
 
 
1025
LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
 
1026
  return wrap(unwrap<GlobalValue>(Global)->getParent());
 
1027
}
 
1028
 
 
1029
LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
 
1030
  return unwrap<GlobalValue>(Global)->isDeclaration();
 
1031
}
 
1032
 
 
1033
LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
 
1034
  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
 
1035
  default:
 
1036
    assert(false && "Unhandled Linkage Type.");
 
1037
  case GlobalValue::ExternalLinkage:
 
1038
    return LLVMExternalLinkage;
 
1039
  case GlobalValue::AvailableExternallyLinkage:
 
1040
    return LLVMAvailableExternallyLinkage;
 
1041
  case GlobalValue::LinkOnceAnyLinkage:
 
1042
    return LLVMLinkOnceAnyLinkage;
 
1043
  case GlobalValue::LinkOnceODRLinkage:
 
1044
    return LLVMLinkOnceODRLinkage;
 
1045
  case GlobalValue::WeakAnyLinkage:
 
1046
    return LLVMWeakAnyLinkage;
 
1047
  case GlobalValue::WeakODRLinkage:
 
1048
    return LLVMWeakODRLinkage;
 
1049
  case GlobalValue::AppendingLinkage:
 
1050
    return LLVMAppendingLinkage;
 
1051
  case GlobalValue::InternalLinkage:
 
1052
    return LLVMInternalLinkage;
 
1053
  case GlobalValue::PrivateLinkage:
 
1054
    return LLVMPrivateLinkage;
 
1055
  case GlobalValue::LinkerPrivateLinkage:
 
1056
    return LLVMLinkerPrivateLinkage;
 
1057
  case GlobalValue::DLLImportLinkage:
 
1058
    return LLVMDLLImportLinkage;
 
1059
  case GlobalValue::DLLExportLinkage:
 
1060
    return LLVMDLLExportLinkage;
 
1061
  case GlobalValue::ExternalWeakLinkage:
 
1062
    return LLVMExternalWeakLinkage;
 
1063
  case GlobalValue::CommonLinkage:
 
1064
    return LLVMCommonLinkage;
 
1065
  }
 
1066
 
 
1067
  // Should never get here.
 
1068
  return static_cast<LLVMLinkage>(0);
 
1069
}
 
1070
 
 
1071
void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
 
1072
  GlobalValue *GV = unwrap<GlobalValue>(Global);
 
1073
 
 
1074
  switch (Linkage) {
 
1075
  default:
 
1076
    assert(false && "Unhandled Linkage Type.");
 
1077
  case LLVMExternalLinkage:
 
1078
    GV->setLinkage(GlobalValue::ExternalLinkage);
 
1079
    break;
 
1080
  case LLVMAvailableExternallyLinkage:
 
1081
    GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
 
1082
    break;
 
1083
  case LLVMLinkOnceAnyLinkage:
 
1084
    GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
 
1085
    break;
 
1086
  case LLVMLinkOnceODRLinkage:
 
1087
    GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
 
1088
    break;
 
1089
  case LLVMWeakAnyLinkage:
 
1090
    GV->setLinkage(GlobalValue::WeakAnyLinkage);
 
1091
    break;
 
1092
  case LLVMWeakODRLinkage:
 
1093
    GV->setLinkage(GlobalValue::WeakODRLinkage);
 
1094
    break;
 
1095
  case LLVMAppendingLinkage:
 
1096
    GV->setLinkage(GlobalValue::AppendingLinkage);
 
1097
    break;
 
1098
  case LLVMInternalLinkage:
 
1099
    GV->setLinkage(GlobalValue::InternalLinkage);
 
1100
    break;
 
1101
  case LLVMPrivateLinkage:
 
1102
    GV->setLinkage(GlobalValue::PrivateLinkage);
 
1103
    break;
 
1104
  case LLVMLinkerPrivateLinkage:
 
1105
    GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
 
1106
    break;
 
1107
  case LLVMDLLImportLinkage:
 
1108
    GV->setLinkage(GlobalValue::DLLImportLinkage);
 
1109
    break;
 
1110
  case LLVMDLLExportLinkage:
 
1111
    GV->setLinkage(GlobalValue::DLLExportLinkage);
 
1112
    break;
 
1113
  case LLVMExternalWeakLinkage:
 
1114
    GV->setLinkage(GlobalValue::ExternalWeakLinkage);
 
1115
    break;
 
1116
  case LLVMGhostLinkage:
 
1117
    DEBUG(errs()
 
1118
          << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
 
1119
    break;
 
1120
  case LLVMCommonLinkage:
 
1121
    GV->setLinkage(GlobalValue::CommonLinkage);
 
1122
    break;
 
1123
  }
 
1124
}
 
1125
 
 
1126
const char *LLVMGetSection(LLVMValueRef Global) {
 
1127
  return unwrap<GlobalValue>(Global)->getSection().c_str();
 
1128
}
 
1129
 
 
1130
void LLVMSetSection(LLVMValueRef Global, const char *Section) {
 
1131
  unwrap<GlobalValue>(Global)->setSection(Section);
 
1132
}
 
1133
 
 
1134
LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
 
1135
  return static_cast<LLVMVisibility>(
 
1136
    unwrap<GlobalValue>(Global)->getVisibility());
 
1137
}
 
1138
 
 
1139
void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
 
1140
  unwrap<GlobalValue>(Global)
 
1141
    ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
 
1142
}
 
1143
 
 
1144
unsigned LLVMGetAlignment(LLVMValueRef Global) {
 
1145
  return unwrap<GlobalValue>(Global)->getAlignment();
 
1146
}
 
1147
 
 
1148
void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
 
1149
  unwrap<GlobalValue>(Global)->setAlignment(Bytes);
 
1150
}
 
1151
 
 
1152
/*--.. Operations on global variables ......................................--*/
 
1153
 
 
1154
LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
 
1155
  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
 
1156
                                 GlobalValue::ExternalLinkage, 0, Name));
 
1157
}
 
1158
 
 
1159
LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
 
1160
                                         const char *Name,
 
1161
                                         unsigned AddressSpace) {
 
1162
  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
 
1163
                                 GlobalValue::ExternalLinkage, 0, Name, 0,
 
1164
                                 false, AddressSpace));
 
1165
}
 
1166
 
 
1167
LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
 
1168
  return wrap(unwrap(M)->getNamedGlobal(Name));
 
1169
}
 
1170
 
 
1171
LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
 
1172
  Module *Mod = unwrap(M);
 
1173
  Module::global_iterator I = Mod->global_begin();
 
1174
  if (I == Mod->global_end())
 
1175
    return 0;
 
1176
  return wrap(I);
 
1177
}
 
1178
 
 
1179
LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
 
1180
  Module *Mod = unwrap(M);
 
1181
  Module::global_iterator I = Mod->global_end();
 
1182
  if (I == Mod->global_begin())
 
1183
    return 0;
 
1184
  return wrap(--I);
 
1185
}
 
1186
 
 
1187
LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
 
1188
  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
 
1189
  Module::global_iterator I = GV;
 
1190
  if (++I == GV->getParent()->global_end())
 
1191
    return 0;
 
1192
  return wrap(I);
 
1193
}
 
1194
 
 
1195
LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
 
1196
  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
 
1197
  Module::global_iterator I = GV;
 
1198
  if (I == GV->getParent()->global_begin())
 
1199
    return 0;
 
1200
  return wrap(--I);
 
1201
}
 
1202
 
 
1203
void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
 
1204
  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
 
1205
}
 
1206
 
 
1207
LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
 
1208
  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
 
1209
  if ( !GV->hasInitializer() )
 
1210
    return 0;
 
1211
  return wrap(GV->getInitializer());
 
1212
}
 
1213
 
 
1214
void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
 
1215
  unwrap<GlobalVariable>(GlobalVar)
 
1216
    ->setInitializer(unwrap<Constant>(ConstantVal));
 
1217
}
 
1218
 
 
1219
LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
 
1220
  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
 
1221
}
 
1222
 
 
1223
void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
 
1224
  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
 
1225
}
 
1226
 
 
1227
LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
 
1228
  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
 
1229
}
 
1230
 
 
1231
void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
 
1232
  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
 
1233
}
 
1234
 
 
1235
/*--.. Operations on aliases ......................................--*/
 
1236
 
 
1237
LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
 
1238
                          const char *Name) {
 
1239
  return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
 
1240
                              unwrap<Constant>(Aliasee), unwrap (M)));
 
1241
}
 
1242
 
 
1243
/*--.. Operations on functions .............................................--*/
 
1244
 
 
1245
LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
 
1246
                             LLVMTypeRef FunctionTy) {
 
1247
  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
 
1248
                               GlobalValue::ExternalLinkage, Name, unwrap(M)));
 
1249
}
 
1250
 
 
1251
LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
 
1252
  return wrap(unwrap(M)->getFunction(Name));
 
1253
}
 
1254
 
 
1255
LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
 
1256
  Module *Mod = unwrap(M);
 
1257
  Module::iterator I = Mod->begin();
 
1258
  if (I == Mod->end())
 
1259
    return 0;
 
1260
  return wrap(I);
 
1261
}
 
1262
 
 
1263
LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
 
1264
  Module *Mod = unwrap(M);
 
1265
  Module::iterator I = Mod->end();
 
1266
  if (I == Mod->begin())
 
1267
    return 0;
 
1268
  return wrap(--I);
 
1269
}
 
1270
 
 
1271
LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
 
1272
  Function *Func = unwrap<Function>(Fn);
 
1273
  Module::iterator I = Func;
 
1274
  if (++I == Func->getParent()->end())
 
1275
    return 0;
 
1276
  return wrap(I);
 
1277
}
 
1278
 
 
1279
LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
 
1280
  Function *Func = unwrap<Function>(Fn);
 
1281
  Module::iterator I = Func;
 
1282
  if (I == Func->getParent()->begin())
 
1283
    return 0;
 
1284
  return wrap(--I);
 
1285
}
 
1286
 
 
1287
void LLVMDeleteFunction(LLVMValueRef Fn) {
 
1288
  unwrap<Function>(Fn)->eraseFromParent();
 
1289
}
 
1290
 
 
1291
unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
 
1292
  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
 
1293
    return F->getIntrinsicID();
 
1294
  return 0;
 
1295
}
 
1296
 
 
1297
unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
 
1298
  return unwrap<Function>(Fn)->getCallingConv();
 
1299
}
 
1300
 
 
1301
void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
 
1302
  return unwrap<Function>(Fn)->setCallingConv(
 
1303
    static_cast<CallingConv::ID>(CC));
 
1304
}
 
1305
 
 
1306
const char *LLVMGetGC(LLVMValueRef Fn) {
 
1307
  Function *F = unwrap<Function>(Fn);
 
1308
  return F->hasGC()? F->getGC() : 0;
 
1309
}
 
1310
 
 
1311
void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
 
1312
  Function *F = unwrap<Function>(Fn);
 
1313
  if (GC)
 
1314
    F->setGC(GC);
 
1315
  else
 
1316
    F->clearGC();
 
1317
}
 
1318
 
 
1319
void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
 
1320
  Function *Func = unwrap<Function>(Fn);
 
1321
  const AttrListPtr PAL = Func->getAttributes();
 
1322
  const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
 
1323
  Func->setAttributes(PALnew);
 
1324
}
 
1325
 
 
1326
void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
 
1327
  Function *Func = unwrap<Function>(Fn);
 
1328
  const AttrListPtr PAL = Func->getAttributes();
 
1329
  const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
 
1330
  Func->setAttributes(PALnew);
 
1331
}
 
1332
 
 
1333
LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
 
1334
  Function *Func = unwrap<Function>(Fn);
 
1335
  const AttrListPtr PAL = Func->getAttributes();
 
1336
  Attributes attr = PAL.getFnAttributes();
 
1337
  return (LLVMAttribute)attr;
 
1338
}
 
1339
 
 
1340
/*--.. Operations on parameters ............................................--*/
 
1341
 
 
1342
unsigned LLVMCountParams(LLVMValueRef FnRef) {
 
1343
  // This function is strictly redundant to
 
1344
  //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
 
1345
  return unwrap<Function>(FnRef)->arg_size();
 
1346
}
 
1347
 
 
1348
void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
 
1349
  Function *Fn = unwrap<Function>(FnRef);
 
1350
  for (Function::arg_iterator I = Fn->arg_begin(),
 
1351
                              E = Fn->arg_end(); I != E; I++)
 
1352
    *ParamRefs++ = wrap(I);
 
1353
}
 
1354
 
 
1355
LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
 
1356
  Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
 
1357
  while (index --> 0)
 
1358
    AI++;
 
1359
  return wrap(AI);
 
1360
}
 
1361
 
 
1362
LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
 
1363
  return wrap(unwrap<Argument>(V)->getParent());
 
1364
}
 
1365
 
 
1366
LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
 
1367
  Function *Func = unwrap<Function>(Fn);
 
1368
  Function::arg_iterator I = Func->arg_begin();
 
1369
  if (I == Func->arg_end())
 
1370
    return 0;
 
1371
  return wrap(I);
 
1372
}
 
1373
 
 
1374
LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
 
1375
  Function *Func = unwrap<Function>(Fn);
 
1376
  Function::arg_iterator I = Func->arg_end();
 
1377
  if (I == Func->arg_begin())
 
1378
    return 0;
 
1379
  return wrap(--I);
 
1380
}
 
1381
 
 
1382
LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
 
1383
  Argument *A = unwrap<Argument>(Arg);
 
1384
  Function::arg_iterator I = A;
 
1385
  if (++I == A->getParent()->arg_end())
 
1386
    return 0;
 
1387
  return wrap(I);
 
1388
}
 
1389
 
 
1390
LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
 
1391
  Argument *A = unwrap<Argument>(Arg);
 
1392
  Function::arg_iterator I = A;
 
1393
  if (I == A->getParent()->arg_begin())
 
1394
    return 0;
 
1395
  return wrap(--I);
 
1396
}
 
1397
 
 
1398
void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
 
1399
  unwrap<Argument>(Arg)->addAttr(PA);
 
1400
}
 
1401
 
 
1402
void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
 
1403
  unwrap<Argument>(Arg)->removeAttr(PA);
 
1404
}
 
1405
 
 
1406
LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
 
1407
  Argument *A = unwrap<Argument>(Arg);
 
1408
  Attributes attr = A->getParent()->getAttributes().getParamAttributes(
 
1409
    A->getArgNo()+1);
 
1410
  return (LLVMAttribute)attr;
 
1411
}
 
1412
  
 
1413
 
 
1414
void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
 
1415
  unwrap<Argument>(Arg)->addAttr(
 
1416
          Attribute::constructAlignmentFromInt(align));
 
1417
}
 
1418
 
 
1419
/*--.. Operations on basic blocks ..........................................--*/
 
1420
 
 
1421
LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
 
1422
  return wrap(static_cast<Value*>(unwrap(BB)));
 
1423
}
 
1424
 
 
1425
LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
 
1426
  return isa<BasicBlock>(unwrap(Val));
 
1427
}
 
1428
 
 
1429
LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
 
1430
  return wrap(unwrap<BasicBlock>(Val));
 
1431
}
 
1432
 
 
1433
LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
 
1434
  return wrap(unwrap(BB)->getParent());
 
1435
}
 
1436
 
 
1437
unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
 
1438
  return unwrap<Function>(FnRef)->size();
 
1439
}
 
1440
 
 
1441
void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
 
1442
  Function *Fn = unwrap<Function>(FnRef);
 
1443
  for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
 
1444
    *BasicBlocksRefs++ = wrap(I);
 
1445
}
 
1446
 
 
1447
LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
 
1448
  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
 
1449
}
 
1450
 
 
1451
LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
 
1452
  Function *Func = unwrap<Function>(Fn);
 
1453
  Function::iterator I = Func->begin();
 
1454
  if (I == Func->end())
 
1455
    return 0;
 
1456
  return wrap(I);
 
1457
}
 
1458
 
 
1459
LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
 
1460
  Function *Func = unwrap<Function>(Fn);
 
1461
  Function::iterator I = Func->end();
 
1462
  if (I == Func->begin())
 
1463
    return 0;
 
1464
  return wrap(--I);
 
1465
}
 
1466
 
 
1467
LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
 
1468
  BasicBlock *Block = unwrap(BB);
 
1469
  Function::iterator I = Block;
 
1470
  if (++I == Block->getParent()->end())
 
1471
    return 0;
 
1472
  return wrap(I);
 
1473
}
 
1474
 
 
1475
LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
 
1476
  BasicBlock *Block = unwrap(BB);
 
1477
  Function::iterator I = Block;
 
1478
  if (I == Block->getParent()->begin())
 
1479
    return 0;
 
1480
  return wrap(--I);
 
1481
}
 
1482
 
 
1483
LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
 
1484
                                                LLVMValueRef FnRef,
 
1485
                                                const char *Name) {
 
1486
  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
 
1487
}
 
1488
 
 
1489
LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
 
1490
  return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
 
1491
}
 
1492
 
 
1493
LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
 
1494
                                                LLVMBasicBlockRef BBRef,
 
1495
                                                const char *Name) {
 
1496
  BasicBlock *BB = unwrap(BBRef);
 
1497
  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
 
1498
}
 
1499
 
 
1500
LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
 
1501
                                       const char *Name) {
 
1502
  return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
 
1503
}
 
1504
 
 
1505
void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
 
1506
  unwrap(BBRef)->eraseFromParent();
 
1507
}
 
1508
 
 
1509
/*--.. Operations on instructions ..........................................--*/
 
1510
 
 
1511
LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
 
1512
  return wrap(unwrap<Instruction>(Inst)->getParent());
 
1513
}
 
1514
 
 
1515
LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
 
1516
  BasicBlock *Block = unwrap(BB);
 
1517
  BasicBlock::iterator I = Block->begin();
 
1518
  if (I == Block->end())
 
1519
    return 0;
 
1520
  return wrap(I);
 
1521
}
 
1522
 
 
1523
LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
 
1524
  BasicBlock *Block = unwrap(BB);
 
1525
  BasicBlock::iterator I = Block->end();
 
1526
  if (I == Block->begin())
 
1527
    return 0;
 
1528
  return wrap(--I);
 
1529
}
 
1530
 
 
1531
LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
 
1532
  Instruction *Instr = unwrap<Instruction>(Inst);
 
1533
  BasicBlock::iterator I = Instr;
 
1534
  if (++I == Instr->getParent()->end())
 
1535
    return 0;
 
1536
  return wrap(I);
 
1537
}
 
1538
 
 
1539
LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
 
1540
  Instruction *Instr = unwrap<Instruction>(Inst);
 
1541
  BasicBlock::iterator I = Instr;
 
1542
  if (I == Instr->getParent()->begin())
 
1543
    return 0;
 
1544
  return wrap(--I);
 
1545
}
 
1546
 
 
1547
/*--.. Call and invoke instructions ........................................--*/
 
1548
 
 
1549
unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
 
1550
  Value *V = unwrap(Instr);
 
1551
  if (CallInst *CI = dyn_cast<CallInst>(V))
 
1552
    return CI->getCallingConv();
 
1553
  else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
 
1554
    return II->getCallingConv();
 
1555
  llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
 
1556
  return 0;
 
1557
}
 
1558
 
 
1559
void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
 
1560
  Value *V = unwrap(Instr);
 
1561
  if (CallInst *CI = dyn_cast<CallInst>(V))
 
1562
    return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
 
1563
  else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
 
1564
    return II->setCallingConv(static_cast<CallingConv::ID>(CC));
 
1565
  llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
 
1566
}
 
1567
 
 
1568
void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
 
1569
                           LLVMAttribute PA) {
 
1570
  CallSite Call = CallSite(unwrap<Instruction>(Instr));
 
1571
  Call.setAttributes(
 
1572
    Call.getAttributes().addAttr(index, PA));
 
1573
}
 
1574
 
 
1575
void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
 
1576
                              LLVMAttribute PA) {
 
1577
  CallSite Call = CallSite(unwrap<Instruction>(Instr));
 
1578
  Call.setAttributes(
 
1579
    Call.getAttributes().removeAttr(index, PA));
 
1580
}
 
1581
 
 
1582
void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
 
1583
                                unsigned align) {
 
1584
  CallSite Call = CallSite(unwrap<Instruction>(Instr));
 
1585
  Call.setAttributes(
 
1586
    Call.getAttributes().addAttr(index, 
 
1587
        Attribute::constructAlignmentFromInt(align)));
 
1588
}
 
1589
 
 
1590
/*--.. Operations on call instructions (only) ..............................--*/
 
1591
 
 
1592
LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
 
1593
  return unwrap<CallInst>(Call)->isTailCall();
 
1594
}
 
1595
 
 
1596
void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
 
1597
  unwrap<CallInst>(Call)->setTailCall(isTailCall);
 
1598
}
 
1599
 
 
1600
/*--.. Operations on phi nodes .............................................--*/
 
1601
 
 
1602
void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
 
1603
                     LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
 
1604
  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
 
1605
  for (unsigned I = 0; I != Count; ++I)
 
1606
    PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
 
1607
}
 
1608
 
 
1609
unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
 
1610
  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
 
1611
}
 
1612
 
 
1613
LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
 
1614
  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
 
1615
}
 
1616
 
 
1617
LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
 
1618
  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
 
1619
}
 
1620
 
 
1621
 
 
1622
/*===-- Instruction builders ----------------------------------------------===*/
 
1623
 
 
1624
LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
 
1625
  return wrap(new IRBuilder<>(*unwrap(C)));
 
1626
}
 
1627
 
 
1628
LLVMBuilderRef LLVMCreateBuilder(void) {
 
1629
  return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
 
1630
}
 
1631
 
 
1632
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
 
1633
                         LLVMValueRef Instr) {
 
1634
  BasicBlock *BB = unwrap(Block);
 
1635
  Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
 
1636
  unwrap(Builder)->SetInsertPoint(BB, I);
 
1637
}
 
1638
 
 
1639
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
 
1640
  Instruction *I = unwrap<Instruction>(Instr);
 
1641
  unwrap(Builder)->SetInsertPoint(I->getParent(), I);
 
1642
}
 
1643
 
 
1644
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
 
1645
  BasicBlock *BB = unwrap(Block);
 
1646
  unwrap(Builder)->SetInsertPoint(BB);
 
1647
}
 
1648
 
 
1649
LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
 
1650
   return wrap(unwrap(Builder)->GetInsertBlock());
 
1651
}
 
1652
 
 
1653
void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
 
1654
  unwrap(Builder)->ClearInsertionPoint ();
 
1655
}
 
1656
 
 
1657
void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
 
1658
  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
 
1659
}
 
1660
 
 
1661
void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
 
1662
                                   const char *Name) {
 
1663
  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
 
1664
}
 
1665
 
 
1666
void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
 
1667
  delete unwrap(Builder);
 
1668
}
 
1669
 
 
1670
/*--.. Metadata builders ...................................................--*/
 
1671
 
 
1672
void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
 
1673
  unwrap(Builder)->SetCurrentDebugLocation(L? unwrap<MDNode>(L) : NULL);
 
1674
}
 
1675
 
 
1676
LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
 
1677
  return wrap(unwrap(Builder)->getCurrentDebugLocation());
 
1678
}
 
1679
 
 
1680
void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
 
1681
  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
 
1682
}
 
1683
 
 
1684
 
 
1685
/*--.. Instruction builders ................................................--*/
 
1686
 
 
1687
LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
 
1688
  return wrap(unwrap(B)->CreateRetVoid());
 
1689
}
 
1690
 
 
1691
LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
 
1692
  return wrap(unwrap(B)->CreateRet(unwrap(V)));
 
1693
}
 
1694
 
 
1695
LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
 
1696
                                   unsigned N) {
 
1697
  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
 
1698
}
 
1699
 
 
1700
LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
 
1701
  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
 
1702
}
 
1703
 
 
1704
LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
 
1705
                             LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
 
1706
  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
 
1707
}
 
1708
 
 
1709
LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
 
1710
                             LLVMBasicBlockRef Else, unsigned NumCases) {
 
1711
  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
 
1712
}
 
1713
 
 
1714
LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
 
1715
                                 unsigned NumDests) {
 
1716
  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
 
1717
}
 
1718
 
 
1719
LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
 
1720
                             LLVMValueRef *Args, unsigned NumArgs,
 
1721
                             LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
 
1722
                             const char *Name) {
 
1723
  return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
 
1724
                                      unwrap(Args), unwrap(Args) + NumArgs,
 
1725
                                      Name));
 
1726
}
 
1727
 
 
1728
LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
 
1729
  return wrap(unwrap(B)->CreateUnwind());
 
1730
}
 
1731
 
 
1732
LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
 
1733
  return wrap(unwrap(B)->CreateUnreachable());
 
1734
}
 
1735
 
 
1736
void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
 
1737
                 LLVMBasicBlockRef Dest) {
 
1738
  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
 
1739
}
 
1740
 
 
1741
void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
 
1742
  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
 
1743
}
 
1744
 
 
1745
/*--.. Arithmetic ..........................................................--*/
 
1746
 
 
1747
LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1748
                          const char *Name) {
 
1749
  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
 
1750
}
 
1751
 
 
1752
LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1753
                          const char *Name) {
 
1754
  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
 
1755
}
 
1756
 
 
1757
LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1758
                          const char *Name) {
 
1759
  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
 
1760
}
 
1761
 
 
1762
LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1763
                          const char *Name) {
 
1764
  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
 
1765
}
 
1766
 
 
1767
LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1768
                          const char *Name) {
 
1769
  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
 
1770
}
 
1771
 
 
1772
LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1773
                          const char *Name) {
 
1774
  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
 
1775
}
 
1776
 
 
1777
LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1778
                          const char *Name) {
 
1779
  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
 
1780
}
 
1781
 
 
1782
LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1783
                          const char *Name) {
 
1784
  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
 
1785
}
 
1786
 
 
1787
LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1788
                          const char *Name) {
 
1789
  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
 
1790
}
 
1791
 
 
1792
LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1793
                          const char *Name) {
 
1794
  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
 
1795
}
 
1796
 
 
1797
LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1798
                          const char *Name) {
 
1799
  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
 
1800
}
 
1801
 
 
1802
LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1803
                          const char *Name) {
 
1804
  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
 
1805
}
 
1806
 
 
1807
LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1808
                           const char *Name) {
 
1809
  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
 
1810
}
 
1811
 
 
1812
LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1813
                           const char *Name) {
 
1814
  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
 
1815
}
 
1816
 
 
1817
LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
 
1818
                                LLVMValueRef RHS, const char *Name) {
 
1819
  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
 
1820
}
 
1821
 
 
1822
LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1823
                           const char *Name) {
 
1824
  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
 
1825
}
 
1826
 
 
1827
LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1828
                           const char *Name) {
 
1829
  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
 
1830
}
 
1831
 
 
1832
LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1833
                           const char *Name) {
 
1834
  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
 
1835
}
 
1836
 
 
1837
LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1838
                           const char *Name) {
 
1839
  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
 
1840
}
 
1841
 
 
1842
LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1843
                          const char *Name) {
 
1844
  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
 
1845
}
 
1846
 
 
1847
LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1848
                           const char *Name) {
 
1849
  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
 
1850
}
 
1851
 
 
1852
LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1853
                           const char *Name) {
 
1854
  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
 
1855
}
 
1856
 
 
1857
LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1858
                          const char *Name) {
 
1859
  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
 
1860
}
 
1861
 
 
1862
LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1863
                         const char *Name) {
 
1864
  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
 
1865
}
 
1866
 
 
1867
LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
 
1868
                          const char *Name) {
 
1869
  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
 
1870
}
 
1871
 
 
1872
LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
 
1873
                            LLVMValueRef LHS, LLVMValueRef RHS,
 
1874
                            const char *Name) {
 
1875
  return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
 
1876
                                     unwrap(RHS), Name));
 
1877
}
 
1878
 
 
1879
LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
 
1880
  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
 
1881
}
 
1882
 
 
1883
LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
 
1884
                             const char *Name) {
 
1885
  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
 
1886
}
 
1887
 
 
1888
LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
 
1889
                             const char *Name) {
 
1890
  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
 
1891
}
 
1892
 
 
1893
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
 
1894
  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
 
1895
}
 
1896
 
 
1897
LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
 
1898
  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
 
1899
}
 
1900
 
 
1901
/*--.. Memory ..............................................................--*/
 
1902
 
 
1903
LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
 
1904
                             const char *Name) {
 
1905
  const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
 
1906
  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
 
1907
  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
 
1908
  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
 
1909
                                               ITy, unwrap(Ty), AllocSize, 
 
1910
                                               0, 0, "");
 
1911
  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
 
1912
}
 
1913
 
 
1914
LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
 
1915
                                  LLVMValueRef Val, const char *Name) {
 
1916
  const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
 
1917
  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
 
1918
  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
 
1919
  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
 
1920
                                               ITy, unwrap(Ty), AllocSize, 
 
1921
                                               unwrap(Val), 0, "");
 
1922
  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
 
1923
}
 
1924
 
 
1925
LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
 
1926
                             const char *Name) {
 
1927
  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
 
1928
}
 
1929
 
 
1930
LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
 
1931
                                  LLVMValueRef Val, const char *Name) {
 
1932
  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
 
1933
}
 
1934
 
 
1935
LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
 
1936
  return wrap(unwrap(B)->Insert(
 
1937
     CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
 
1938
}
 
1939
 
 
1940
 
 
1941
LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
 
1942
                           const char *Name) {
 
1943
  return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
 
1944
}
 
1945
 
 
1946
LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
 
1947
                            LLVMValueRef PointerVal) {
 
1948
  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
 
1949
}
 
1950
 
 
1951
LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
 
1952
                          LLVMValueRef *Indices, unsigned NumIndices,
 
1953
                          const char *Name) {
 
1954
  return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
 
1955
                                   unwrap(Indices) + NumIndices, Name));
 
1956
}
 
1957
 
 
1958
LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
 
1959
                                  LLVMValueRef *Indices, unsigned NumIndices,
 
1960
                                  const char *Name) {
 
1961
  return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
 
1962
                                           unwrap(Indices) + NumIndices, Name));
 
1963
}
 
1964
 
 
1965
LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
 
1966
                                unsigned Idx, const char *Name) {
 
1967
  return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
 
1968
}
 
1969
 
 
1970
LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
 
1971
                                   const char *Name) {
 
1972
  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
 
1973
}
 
1974
 
 
1975
LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
 
1976
                                      const char *Name) {
 
1977
  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
 
1978
}
 
1979
 
 
1980
/*--.. Casts ...............................................................--*/
 
1981
 
 
1982
LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
 
1983
                            LLVMTypeRef DestTy, const char *Name) {
 
1984
  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
 
1985
}
 
1986
 
 
1987
LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
 
1988
                           LLVMTypeRef DestTy, const char *Name) {
 
1989
  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
 
1990
}
 
1991
 
 
1992
LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
 
1993
                           LLVMTypeRef DestTy, const char *Name) {
 
1994
  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
 
1995
}
 
1996
 
 
1997
LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
 
1998
                             LLVMTypeRef DestTy, const char *Name) {
 
1999
  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
 
2000
}
 
2001
 
 
2002
LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
 
2003
                             LLVMTypeRef DestTy, const char *Name) {
 
2004
  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
 
2005
}
 
2006
 
 
2007
LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
 
2008
                             LLVMTypeRef DestTy, const char *Name) {
 
2009
  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
 
2010
}
 
2011
 
 
2012
LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
 
2013
                             LLVMTypeRef DestTy, const char *Name) {
 
2014
  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
 
2015
}
 
2016
 
 
2017
LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
 
2018
                              LLVMTypeRef DestTy, const char *Name) {
 
2019
  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
 
2020
}
 
2021
 
 
2022
LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
 
2023
                            LLVMTypeRef DestTy, const char *Name) {
 
2024
  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
 
2025
}
 
2026
 
 
2027
LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
 
2028
                               LLVMTypeRef DestTy, const char *Name) {
 
2029
  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
 
2030
}
 
2031
 
 
2032
LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
 
2033
                               LLVMTypeRef DestTy, const char *Name) {
 
2034
  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
 
2035
}
 
2036
 
 
2037
LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2038
                              LLVMTypeRef DestTy, const char *Name) {
 
2039
  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
 
2040
}
 
2041
 
 
2042
LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2043
                                    LLVMTypeRef DestTy, const char *Name) {
 
2044
  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
 
2045
                                             Name));
 
2046
}
 
2047
 
 
2048
LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2049
                                    LLVMTypeRef DestTy, const char *Name) {
 
2050
  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
 
2051
                                             Name));
 
2052
}
 
2053
 
 
2054
LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2055
                                     LLVMTypeRef DestTy, const char *Name) {
 
2056
  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
 
2057
                                              Name));
 
2058
}
 
2059
 
 
2060
LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
 
2061
                           LLVMTypeRef DestTy, const char *Name) {
 
2062
  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
 
2063
                                    unwrap(DestTy), Name));
 
2064
}
 
2065
 
 
2066
LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2067
                                  LLVMTypeRef DestTy, const char *Name) {
 
2068
  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
 
2069
}
 
2070
 
 
2071
LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2072
                              LLVMTypeRef DestTy, const char *Name) {
 
2073
  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
 
2074
                                       /*isSigned*/true, Name));
 
2075
}
 
2076
 
 
2077
LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
 
2078
                             LLVMTypeRef DestTy, const char *Name) {
 
2079
  return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
 
2080
}
 
2081
 
 
2082
/*--.. Comparisons .........................................................--*/
 
2083
 
 
2084
LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
 
2085
                           LLVMValueRef LHS, LLVMValueRef RHS,
 
2086
                           const char *Name) {
 
2087
  return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
 
2088
                                    unwrap(LHS), unwrap(RHS), Name));
 
2089
}
 
2090
 
 
2091
LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
 
2092
                           LLVMValueRef LHS, LLVMValueRef RHS,
 
2093
                           const char *Name) {
 
2094
  return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
 
2095
                                    unwrap(LHS), unwrap(RHS), Name));
 
2096
}
 
2097
 
 
2098
/*--.. Miscellaneous instructions ..........................................--*/
 
2099
 
 
2100
LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
 
2101
  return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
 
2102
}
 
2103
 
 
2104
LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
 
2105
                           LLVMValueRef *Args, unsigned NumArgs,
 
2106
                           const char *Name) {
 
2107
  return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
 
2108
                                    unwrap(Args) + NumArgs, Name));
 
2109
}
 
2110
 
 
2111
LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
 
2112
                             LLVMValueRef Then, LLVMValueRef Else,
 
2113
                             const char *Name) {
 
2114
  return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
 
2115
                                      Name));
 
2116
}
 
2117
 
 
2118
LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
 
2119
                            LLVMTypeRef Ty, const char *Name) {
 
2120
  return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
 
2121
}
 
2122
 
 
2123
LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
 
2124
                                      LLVMValueRef Index, const char *Name) {
 
2125
  return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
 
2126
                                              Name));
 
2127
}
 
2128
 
 
2129
LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
 
2130
                                    LLVMValueRef EltVal, LLVMValueRef Index,
 
2131
                                    const char *Name) {
 
2132
  return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
 
2133
                                             unwrap(Index), Name));
 
2134
}
 
2135
 
 
2136
LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
 
2137
                                    LLVMValueRef V2, LLVMValueRef Mask,
 
2138
                                    const char *Name) {
 
2139
  return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
 
2140
                                             unwrap(Mask), Name));
 
2141
}
 
2142
 
 
2143
LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
 
2144
                                   unsigned Index, const char *Name) {
 
2145
  return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
 
2146
}
 
2147
 
 
2148
LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
 
2149
                                  LLVMValueRef EltVal, unsigned Index,
 
2150
                                  const char *Name) {
 
2151
  return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
 
2152
                                           Index, Name));
 
2153
}
 
2154
 
 
2155
LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
 
2156
                             const char *Name) {
 
2157
  return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
 
2158
}
 
2159
 
 
2160
LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
 
2161
                                const char *Name) {
 
2162
  return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
 
2163
}
 
2164
 
 
2165
LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
 
2166
                              LLVMValueRef RHS, const char *Name) {
 
2167
  return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
 
2168
}
 
2169
 
 
2170
 
 
2171
/*===-- Module providers --------------------------------------------------===*/
 
2172
 
 
2173
LLVMModuleProviderRef
 
2174
LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
 
2175
  return reinterpret_cast<LLVMModuleProviderRef>(M);
 
2176
}
 
2177
 
 
2178
void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
 
2179
  delete unwrap(MP);
 
2180
}
 
2181
 
 
2182
 
 
2183
/*===-- Memory buffers ----------------------------------------------------===*/
 
2184
 
 
2185
LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
 
2186
    const char *Path,
 
2187
    LLVMMemoryBufferRef *OutMemBuf,
 
2188
    char **OutMessage) {
 
2189
 
 
2190
  std::string Error;
 
2191
  if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
 
2192
    *OutMemBuf = wrap(MB);
 
2193
    return 0;
 
2194
  }
 
2195
  
 
2196
  *OutMessage = strdup(Error.c_str());
 
2197
  return 1;
 
2198
}
 
2199
 
 
2200
LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
 
2201
                                         char **OutMessage) {
 
2202
  MemoryBuffer *MB = MemoryBuffer::getSTDIN();
 
2203
  if (!MB->getBufferSize()) {
 
2204
    delete MB;
 
2205
    *OutMessage = strdup("stdin is empty.");
 
2206
    return 1;
 
2207
  }
 
2208
 
 
2209
  *OutMemBuf = wrap(MB);
 
2210
  return 0;
 
2211
}
 
2212
 
 
2213
void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
 
2214
  delete unwrap(MemBuf);
 
2215
}