1fa83ebd1c0220158e328ee450b59fbfb7dd0daf
[oota-llvm.git] / lib / VMCore / Core.cpp
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/TypeSymbolTable.h"
22 #include "llvm/ModuleProvider.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/CallSite.h"
27 #include <cassert>
28 #include <cstdlib>
29 #include <cstring>
30
31 using namespace llvm;
32
33
34 /*===-- Error handling ----------------------------------------------------===*/
35
36 void LLVMDisposeMessage(char *Message) {
37   free(Message);
38 }
39
40
41 /*===-- Operations on modules ---------------------------------------------===*/
42
43 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
44   return wrap(new Module(ModuleID));
45 }
46
47 void LLVMDisposeModule(LLVMModuleRef M) {
48   delete unwrap(M);
49 }
50
51 /*--.. Data layout .........................................................--*/
52 const char * LLVMGetDataLayout(LLVMModuleRef M) {
53   return unwrap(M)->getDataLayout().c_str();
54 }
55
56 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
57   unwrap(M)->setDataLayout(Triple);
58 }
59
60 /*--.. Target triple .......................................................--*/
61 const char * LLVMGetTarget(LLVMModuleRef M) {
62   return unwrap(M)->getTargetTriple().c_str();
63 }
64
65 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
66   unwrap(M)->setTargetTriple(Triple);
67 }
68
69 /*--.. Type names ..........................................................--*/
70 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
71   return unwrap(M)->addTypeName(Name, unwrap(Ty));
72 }
73
74 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
75   std::string N(Name);
76   
77   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
78   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
79     if (I->first == N)
80       TST.remove(I);
81 }
82
83 void LLVMDumpModule(LLVMModuleRef M) {
84   unwrap(M)->dump();
85 }
86
87
88 /*===-- Operations on types -----------------------------------------------===*/
89
90 /*--.. Operations on all types (mostly) ....................................--*/
91
92 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
93   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
94 }
95
96 /*--.. Operations on integer types .........................................--*/
97
98 LLVMTypeRef LLVMInt1Type(void)  { return (LLVMTypeRef) Type::Int1Ty;  }
99 LLVMTypeRef LLVMInt8Type(void)  { return (LLVMTypeRef) Type::Int8Ty;  }
100 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
101 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
102 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
103
104 LLVMTypeRef LLVMIntType(unsigned NumBits) {
105   return wrap(IntegerType::get(NumBits));
106 }
107
108 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
109   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
110 }
111
112 /*--.. Operations on real types ............................................--*/
113
114 LLVMTypeRef LLVMFloatType(void)    { return (LLVMTypeRef) Type::FloatTy;     }
115 LLVMTypeRef LLVMDoubleType(void)   { return (LLVMTypeRef) Type::DoubleTy;    }
116 LLVMTypeRef LLVMX86FP80Type(void)  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
117 LLVMTypeRef LLVMFP128Type(void)    { return (LLVMTypeRef) Type::FP128Ty;     }
118 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
119
120 /*--.. Operations on function types ........................................--*/
121
122 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
123                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
124                              int IsVarArg) {
125   std::vector<const Type*> Tys;
126   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
127     Tys.push_back(unwrap(*I));
128   
129   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
130 }
131
132 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
133   return unwrap<FunctionType>(FunctionTy)->isVarArg();
134 }
135
136 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
137   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
138 }
139
140 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
141   return unwrap<FunctionType>(FunctionTy)->getNumParams();
142 }
143
144 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
145   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
146   for (FunctionType::param_iterator I = Ty->param_begin(),
147                                     E = Ty->param_end(); I != E; ++I)
148     *Dest++ = wrap(*I);
149 }
150
151 /*--.. Operations on struct types ..........................................--*/
152
153 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
154                            unsigned ElementCount, int Packed) {
155   std::vector<const Type*> Tys;
156   for (LLVMTypeRef *I = ElementTypes,
157                    *E = ElementTypes + ElementCount; I != E; ++I)
158     Tys.push_back(unwrap(*I));
159   
160   return wrap(StructType::get(Tys, Packed != 0));
161 }
162
163 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
164   return unwrap<StructType>(StructTy)->getNumElements();
165 }
166
167 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
168   StructType *Ty = unwrap<StructType>(StructTy);
169   for (FunctionType::param_iterator I = Ty->element_begin(),
170                                     E = Ty->element_end(); I != E; ++I)
171     *Dest++ = wrap(*I);
172 }
173
174 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
175   return unwrap<StructType>(StructTy)->isPacked();
176 }
177
178 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
179
180 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
181   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
182 }
183
184 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
185   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
186 }
187
188 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
189   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
190 }
191
192 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
193   return wrap(unwrap<SequentialType>(Ty)->getElementType());
194 }
195
196 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
197   return unwrap<ArrayType>(ArrayTy)->getNumElements();
198 }
199
200 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
201   return unwrap<PointerType>(PointerTy)->getAddressSpace();
202 }
203
204 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
205   return unwrap<VectorType>(VectorTy)->getNumElements();
206 }
207
208 /*--.. Operations on other types ...........................................--*/
209
210 LLVMTypeRef LLVMVoidType(void)  { return (LLVMTypeRef) Type::VoidTy;  }
211 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
212
213 LLVMTypeRef LLVMOpaqueType(void) {
214   return wrap(llvm::OpaqueType::get());
215 }
216
217 /*--.. Operations on type handles ..........................................--*/
218
219 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
220   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
221 }
222
223 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
224   delete unwrap(TypeHandle);
225 }
226
227 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
228   return wrap(unwrap(TypeHandle)->get());
229 }
230
231 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
232   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
233 }
234
235
236 /*===-- Operations on values ----------------------------------------------===*/
237
238 /*--.. Operations on all values ............................................--*/
239
240 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
241   return wrap(unwrap(Val)->getType());
242 }
243
244 const char *LLVMGetValueName(LLVMValueRef Val) {
245   return unwrap(Val)->getNameStart();
246 }
247
248 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
249   unwrap(Val)->setName(Name);
250 }
251
252 void LLVMDumpValue(LLVMValueRef Val) {
253   unwrap(Val)->dump();
254 }
255
256
257 /*--.. Conversion functions ................................................--*/
258
259 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
260   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
261     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
262   }
263
264 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
265
266
267 /*--.. Operations on constants of any type .................................--*/
268
269 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
270   return wrap(Constant::getNullValue(unwrap(Ty)));
271 }
272
273 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
274   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
275 }
276
277 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
278   return wrap(UndefValue::get(unwrap(Ty)));
279 }
280
281 int LLVMIsConstant(LLVMValueRef Ty) {
282   return isa<Constant>(unwrap(Ty));
283 }
284
285 int LLVMIsNull(LLVMValueRef Val) {
286   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
287     return C->isNullValue();
288   return false;
289 }
290
291 int LLVMIsUndef(LLVMValueRef Val) {
292   return isa<UndefValue>(unwrap(Val));
293 }
294
295 /*--.. Operations on scalar constants ......................................--*/
296
297 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
298                           int SignExtend) {
299   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
300 }
301
302 static const fltSemantics &SemanticsForType(Type *Ty) {
303   assert(Ty->isFloatingPoint() && "Type is not floating point!");
304   if (Ty == Type::FloatTy)
305     return APFloat::IEEEsingle;
306   if (Ty == Type::DoubleTy)
307     return APFloat::IEEEdouble;
308   if (Ty == Type::X86_FP80Ty)
309     return APFloat::x87DoubleExtended;
310   if (Ty == Type::FP128Ty)
311     return APFloat::IEEEquad;
312   if (Ty == Type::PPC_FP128Ty)
313     return APFloat::PPCDoubleDouble;
314   return APFloat::Bogus;
315 }
316
317 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
318   APFloat APN(N);
319   bool ignored;
320   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
321               &ignored);
322   return wrap(ConstantFP::get(APN));
323 }
324
325 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
326   return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
327 }
328
329 /*--.. Operations on composite constants ...................................--*/
330
331 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
332                              int DontNullTerminate) {
333   /* Inverted the sense of AddNull because ', 0)' is a
334      better mnemonic for null termination than ', 1)'. */
335   return wrap(ConstantArray::get(std::string(Str, Length),
336                                  DontNullTerminate == 0));
337 }
338
339 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
340                             LLVMValueRef *ConstantVals, unsigned Length) {
341   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
342                                  unwrap<Constant>(ConstantVals, Length),
343                                  Length));
344 }
345
346 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
347                              int Packed) {
348   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
349                                   Count, Packed != 0));
350 }
351
352 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
353   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
354                                   Size));
355 }
356
357 /*--.. Constant expressions ................................................--*/
358
359 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
360   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
361 }
362
363 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
364   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
365 }
366
367 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
368   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
369 }
370
371 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
372   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
373                                    unwrap<Constant>(RHSConstant)));
374 }
375
376 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
377   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
378                                    unwrap<Constant>(RHSConstant)));
379 }
380
381 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
382   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
383                                    unwrap<Constant>(RHSConstant)));
384 }
385
386 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
387   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
388                                     unwrap<Constant>(RHSConstant)));
389 }
390
391 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
392   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
393                                     unwrap<Constant>(RHSConstant)));
394 }
395
396 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
397   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
398                                     unwrap<Constant>(RHSConstant)));
399 }
400
401 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
402   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
403                                     unwrap<Constant>(RHSConstant)));
404 }
405
406 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
407   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
408                                     unwrap<Constant>(RHSConstant)));
409 }
410
411 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
412   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
413                                     unwrap<Constant>(RHSConstant)));
414 }
415
416 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
418                                    unwrap<Constant>(RHSConstant)));
419 }
420
421 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
422   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
423                                   unwrap<Constant>(RHSConstant)));
424 }
425
426 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
427   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
428                                    unwrap<Constant>(RHSConstant)));
429 }
430
431 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
432                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
433   return wrap(ConstantExpr::getICmp(Predicate,
434                                     unwrap<Constant>(LHSConstant),
435                                     unwrap<Constant>(RHSConstant)));
436 }
437
438 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
439                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440   return wrap(ConstantExpr::getFCmp(Predicate,
441                                     unwrap<Constant>(LHSConstant),
442                                     unwrap<Constant>(RHSConstant)));
443 }
444
445 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
446   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
447                                   unwrap<Constant>(RHSConstant)));
448 }
449
450 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
451   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
452                                     unwrap<Constant>(RHSConstant)));
453 }
454
455 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
456   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
457                                     unwrap<Constant>(RHSConstant)));
458 }
459
460 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
461                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
462   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
463                                              unwrap<Constant>(ConstantIndices, 
464                                                               NumIndices),
465                                              NumIndices));
466 }
467
468 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
469   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
470                                      unwrap(ToType)));
471 }
472
473 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
474   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
475                                     unwrap(ToType)));
476 }
477
478 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
479   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
480                                     unwrap(ToType)));
481 }
482
483 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
484   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
485                                        unwrap(ToType)));
486 }
487
488 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
489   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
490                                         unwrap(ToType)));
491 }
492
493 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
494   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
495                                       unwrap(ToType)));
496 }
497
498 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
499   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
500                                       unwrap(ToType)));
501 }
502
503 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
504   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
505                                       unwrap(ToType)));
506 }
507
508 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
509   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
510                                       unwrap(ToType)));
511 }
512
513 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
514   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
515                                         unwrap(ToType)));
516 }
517
518 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
519   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
520                                         unwrap(ToType)));
521 }
522
523 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
524   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
525                                        unwrap(ToType)));
526 }
527
528 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
529                              LLVMValueRef ConstantIfTrue,
530                              LLVMValueRef ConstantIfFalse) {
531   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
532                                       unwrap<Constant>(ConstantIfTrue),
533                                       unwrap<Constant>(ConstantIfFalse)));
534 }
535
536 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
537                                      LLVMValueRef IndexConstant) {
538   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
539                                               unwrap<Constant>(IndexConstant)));
540 }
541
542 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
543                                     LLVMValueRef ElementValueConstant,
544                                     LLVMValueRef IndexConstant) {
545   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
546                                          unwrap<Constant>(ElementValueConstant),
547                                              unwrap<Constant>(IndexConstant)));
548 }
549
550 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
551                                     LLVMValueRef VectorBConstant,
552                                     LLVMValueRef MaskConstant) {
553   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
554                                              unwrap<Constant>(VectorBConstant),
555                                              unwrap<Constant>(MaskConstant)));
556 }
557
558 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
559                                    unsigned NumIdx) {
560   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
561                                             IdxList, NumIdx));
562 }
563
564 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
565                                   LLVMValueRef ElementValueConstant,
566                                   unsigned *IdxList, unsigned NumIdx) {
567   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
568                                          unwrap<Constant>(ElementValueConstant),
569                                            IdxList, NumIdx));
570 }
571
572 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
573                                 const char *Constraints, int HasSideEffects) {
574   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
575                              Constraints, HasSideEffects));
576 }
577
578 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
579
580 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
581   return wrap(unwrap<GlobalValue>(Global)->getParent());
582 }
583
584 int LLVMIsDeclaration(LLVMValueRef Global) {
585   return unwrap<GlobalValue>(Global)->isDeclaration();
586 }
587
588 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
589   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
590 }
591
592 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
593   unwrap<GlobalValue>(Global)
594     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
595 }
596
597 const char *LLVMGetSection(LLVMValueRef Global) {
598   return unwrap<GlobalValue>(Global)->getSection().c_str();
599 }
600
601 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
602   unwrap<GlobalValue>(Global)->setSection(Section);
603 }
604
605 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
606   return static_cast<LLVMVisibility>(
607     unwrap<GlobalValue>(Global)->getVisibility());
608 }
609
610 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
611   unwrap<GlobalValue>(Global)
612     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
613 }
614
615 unsigned LLVMGetAlignment(LLVMValueRef Global) {
616   return unwrap<GlobalValue>(Global)->getAlignment();
617 }
618
619 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
620   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
621 }
622
623 /*--.. Operations on global variables ......................................--*/
624
625 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
626   return wrap(new GlobalVariable(unwrap(Ty), false,
627                                  GlobalValue::ExternalLinkage, 0, Name,
628                                  unwrap(M)));
629 }
630
631 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
632   return wrap(unwrap(M)->getNamedGlobal(Name));
633 }
634
635 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
636   Module *Mod = unwrap(M);
637   Module::global_iterator I = Mod->global_begin();
638   if (I == Mod->global_end())
639     return 0;
640   return wrap(I);
641 }
642
643 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
644   Module *Mod = unwrap(M);
645   Module::global_iterator I = Mod->global_end();
646   if (I == Mod->global_begin())
647     return 0;
648   return wrap(--I);
649 }
650
651 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
652   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
653   Module::global_iterator I = GV;
654   if (++I == GV->getParent()->global_end())
655     return 0;
656   return wrap(I);
657 }
658
659 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
660   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
661   Module::global_iterator I = GV;
662   if (I == GV->getParent()->global_begin())
663     return 0;
664   return wrap(--I);
665 }
666
667 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
668   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
669 }
670
671 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
672   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
673 }
674
675 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
676   unwrap<GlobalVariable>(GlobalVar)
677     ->setInitializer(unwrap<Constant>(ConstantVal));
678 }
679
680 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
681   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
682 }
683
684 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
685   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
686 }
687
688 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
689   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
690 }
691
692 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
693   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
694 }
695
696 /*--.. Operations on aliases ......................................--*/
697
698 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
699                           const char *Name) {
700   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
701                               unwrap<Constant>(Aliasee), unwrap (M)));
702 }
703
704 /*--.. Operations on functions .............................................--*/
705
706 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
707                              LLVMTypeRef FunctionTy) {
708   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
709                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
710 }
711
712 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
713   return wrap(unwrap(M)->getFunction(Name));
714 }
715
716 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
717   Module *Mod = unwrap(M);
718   Module::iterator I = Mod->begin();
719   if (I == Mod->end())
720     return 0;
721   return wrap(I);
722 }
723
724 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
725   Module *Mod = unwrap(M);
726   Module::iterator I = Mod->end();
727   if (I == Mod->begin())
728     return 0;
729   return wrap(--I);
730 }
731
732 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
733   Function *Func = unwrap<Function>(Fn);
734   Module::iterator I = Func;
735   if (++I == Func->getParent()->end())
736     return 0;
737   return wrap(I);
738 }
739
740 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
741   Function *Func = unwrap<Function>(Fn);
742   Module::iterator I = Func;
743   if (I == Func->getParent()->begin())
744     return 0;
745   return wrap(--I);
746 }
747
748 void LLVMDeleteFunction(LLVMValueRef Fn) {
749   unwrap<Function>(Fn)->eraseFromParent();
750 }
751
752 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
753   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
754     return F->getIntrinsicID();
755   return 0;
756 }
757
758 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
759   return unwrap<Function>(Fn)->getCallingConv();
760 }
761
762 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
763   return unwrap<Function>(Fn)->setCallingConv(CC);
764 }
765
766 const char *LLVMGetGC(LLVMValueRef Fn) {
767   Function *F = unwrap<Function>(Fn);
768   return F->hasGC()? F->getGC() : 0;
769 }
770
771 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
772   Function *F = unwrap<Function>(Fn);
773   if (GC)
774     F->setGC(GC);
775   else
776     F->clearGC();
777 }
778
779 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
780   Function *Func = unwrap<Function>(Fn);
781   const AttrListPtr PAL = Func->getAttributes();
782   const AttrListPtr PALnew = PAL.addAttr(0, PA);
783   Func->setAttributes(PALnew);
784 }
785
786 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
787   Function *Func = unwrap<Function>(Fn);
788   const AttrListPtr PAL = Func->getAttributes();
789   const AttrListPtr PALnew = PAL.removeAttr(0, PA);
790   Func->setAttributes(PALnew);
791 }
792
793 /*--.. Operations on parameters ............................................--*/
794
795 unsigned LLVMCountParams(LLVMValueRef FnRef) {
796   // This function is strictly redundant to
797   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
798   return unwrap<Function>(FnRef)->arg_size();
799 }
800
801 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
802   Function *Fn = unwrap<Function>(FnRef);
803   for (Function::arg_iterator I = Fn->arg_begin(),
804                               E = Fn->arg_end(); I != E; I++)
805     *ParamRefs++ = wrap(I);
806 }
807
808 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
809   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
810   while (index --> 0)
811     AI++;
812   return wrap(AI);
813 }
814
815 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
816   return wrap(unwrap<Argument>(V)->getParent());
817 }
818
819 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
820   Function *Func = unwrap<Function>(Fn);
821   Function::arg_iterator I = Func->arg_begin();
822   if (I == Func->arg_end())
823     return 0;
824   return wrap(I);
825 }
826
827 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
828   Function *Func = unwrap<Function>(Fn);
829   Function::arg_iterator I = Func->arg_end();
830   if (I == Func->arg_begin())
831     return 0;
832   return wrap(--I);
833 }
834
835 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
836   Argument *A = unwrap<Argument>(Arg);
837   Function::arg_iterator I = A;
838   if (++I == A->getParent()->arg_end())
839     return 0;
840   return wrap(I);
841 }
842
843 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
844   Argument *A = unwrap<Argument>(Arg);
845   Function::arg_iterator I = A;
846   if (I == A->getParent()->arg_begin())
847     return 0;
848   return wrap(--I);
849 }
850
851 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
852   unwrap<Argument>(Arg)->addAttr(PA);
853 }
854
855 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
856   unwrap<Argument>(Arg)->removeAttr(PA);
857 }
858
859 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
860   unwrap<Argument>(Arg)->addAttr(
861           Attribute::constructAlignmentFromInt(align));
862 }
863
864 /*--.. Operations on basic blocks ..........................................--*/
865
866 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
867   return wrap(static_cast<Value*>(unwrap(BB)));
868 }
869
870 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
871   return isa<BasicBlock>(unwrap(Val));
872 }
873
874 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
875   return wrap(unwrap<BasicBlock>(Val));
876 }
877
878 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
879   return wrap(unwrap(BB)->getParent());
880 }
881
882 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
883   return unwrap<Function>(FnRef)->size();
884 }
885
886 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
887   Function *Fn = unwrap<Function>(FnRef);
888   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
889     *BasicBlocksRefs++ = wrap(I);
890 }
891
892 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
893   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
894 }
895
896 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
897   Function *Func = unwrap<Function>(Fn);
898   Function::iterator I = Func->begin();
899   if (I == Func->end())
900     return 0;
901   return wrap(I);
902 }
903
904 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
905   Function *Func = unwrap<Function>(Fn);
906   Function::iterator I = Func->end();
907   if (I == Func->begin())
908     return 0;
909   return wrap(--I);
910 }
911
912 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
913   BasicBlock *Block = unwrap(BB);
914   Function::iterator I = Block;
915   if (++I == Block->getParent()->end())
916     return 0;
917   return wrap(I);
918 }
919
920 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
921   BasicBlock *Block = unwrap(BB);
922   Function::iterator I = Block;
923   if (I == Block->getParent()->begin())
924     return 0;
925   return wrap(--I);
926 }
927
928 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
929   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
930 }
931
932 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
933                                        const char *Name) {
934   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
935   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
936                                  InsertBeforeBB));
937 }
938
939 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
940   unwrap(BBRef)->eraseFromParent();
941 }
942
943 /*--.. Operations on instructions ..........................................--*/
944
945 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
946   return wrap(unwrap<Instruction>(Inst)->getParent());
947 }
948
949 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
950   BasicBlock *Block = unwrap(BB);
951   BasicBlock::iterator I = Block->begin();
952   if (I == Block->end())
953     return 0;
954   return wrap(I);
955 }
956
957 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
958   BasicBlock *Block = unwrap(BB);
959   BasicBlock::iterator I = Block->end();
960   if (I == Block->begin())
961     return 0;
962   return wrap(--I);
963 }
964
965 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
966   Instruction *Instr = unwrap<Instruction>(Inst);
967   BasicBlock::iterator I = Instr;
968   if (++I == Instr->getParent()->end())
969     return 0;
970   return wrap(I);
971 }
972
973 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
974   Instruction *Instr = unwrap<Instruction>(Inst);
975   BasicBlock::iterator I = Instr;
976   if (I == Instr->getParent()->begin())
977     return 0;
978   return wrap(--I);
979 }
980
981 /*--.. Call and invoke instructions ........................................--*/
982
983 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
984   Value *V = unwrap(Instr);
985   if (CallInst *CI = dyn_cast<CallInst>(V))
986     return CI->getCallingConv();
987   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
988     return II->getCallingConv();
989   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
990   return 0;
991 }
992
993 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
994   Value *V = unwrap(Instr);
995   if (CallInst *CI = dyn_cast<CallInst>(V))
996     return CI->setCallingConv(CC);
997   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
998     return II->setCallingConv(CC);
999   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
1000 }
1001
1002 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1003                            LLVMAttribute PA) {
1004   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1005   Call.setAttributes(
1006     Call.getAttributes().addAttr(index, PA));
1007 }
1008
1009 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1010                               LLVMAttribute PA) {
1011   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1012   Call.setAttributes(
1013     Call.getAttributes().removeAttr(index, PA));
1014 }
1015
1016 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1017                                 unsigned align) {
1018   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1019   Call.setAttributes(
1020     Call.getAttributes().addAttr(index, 
1021         Attribute::constructAlignmentFromInt(align)));
1022 }
1023
1024 /*--.. Operations on call instructions (only) ..............................--*/
1025
1026 int LLVMIsTailCall(LLVMValueRef Call) {
1027   return unwrap<CallInst>(Call)->isTailCall();
1028 }
1029
1030 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1031   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1032 }
1033
1034 /*--.. Operations on phi nodes .............................................--*/
1035
1036 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1037                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1038   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1039   for (unsigned I = 0; I != Count; ++I)
1040     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1041 }
1042
1043 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1044   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1045 }
1046
1047 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1048   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1049 }
1050
1051 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1052   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1053 }
1054
1055
1056 /*===-- Instruction builders ----------------------------------------------===*/
1057
1058 LLVMBuilderRef LLVMCreateBuilder(void) {
1059   return wrap(new IRBuilder<>());
1060 }
1061
1062 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1063                          LLVMValueRef Instr) {
1064   BasicBlock *BB = unwrap(Block);
1065   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1066   unwrap(Builder)->SetInsertPoint(BB, I);
1067 }
1068
1069 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1070   Instruction *I = unwrap<Instruction>(Instr);
1071   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1072 }
1073
1074 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1075   BasicBlock *BB = unwrap(Block);
1076   unwrap(Builder)->SetInsertPoint(BB);
1077 }
1078
1079 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1080    return wrap(unwrap(Builder)->GetInsertBlock());
1081 }
1082
1083 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1084   unwrap(Builder)->ClearInsertionPoint ();
1085 }
1086
1087 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1088   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1089 }
1090
1091 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1092   delete unwrap(Builder);
1093 }
1094
1095 /*--.. Instruction builders ................................................--*/
1096
1097 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1098   return wrap(unwrap(B)->CreateRetVoid());
1099 }
1100
1101 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1102   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1103 }
1104
1105 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1106   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1107 }
1108
1109 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1110                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1111   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1112 }
1113
1114 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1115                              LLVMBasicBlockRef Else, unsigned NumCases) {
1116   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1117 }
1118
1119 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1120                              LLVMValueRef *Args, unsigned NumArgs,
1121                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1122                              const char *Name) {
1123   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1124                                       unwrap(Args), unwrap(Args) + NumArgs,
1125                                       Name));
1126 }
1127
1128 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1129   return wrap(unwrap(B)->CreateUnwind());
1130 }
1131
1132 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1133   return wrap(unwrap(B)->CreateUnreachable());
1134 }
1135
1136 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1137                  LLVMBasicBlockRef Dest) {
1138   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1139 }
1140
1141 /*--.. Arithmetic ..........................................................--*/
1142
1143 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1144                           const char *Name) {
1145   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1146 }
1147
1148 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1149                           const char *Name) {
1150   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1151 }
1152
1153 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1154                           const char *Name) {
1155   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1156 }
1157
1158 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1159                            const char *Name) {
1160   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1161 }
1162
1163 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1164                            const char *Name) {
1165   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1166 }
1167
1168 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1169                            const char *Name) {
1170   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1171 }
1172
1173 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1174                            const char *Name) {
1175   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1176 }
1177
1178 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1179                            const char *Name) {
1180   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1181 }
1182
1183 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1184                            const char *Name) {
1185   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1186 }
1187
1188 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1189                           const char *Name) {
1190   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1191 }
1192
1193 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1194                            const char *Name) {
1195   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1196 }
1197
1198 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1199                            const char *Name) {
1200   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1201 }
1202
1203 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1204                           const char *Name) {
1205   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1206 }
1207
1208 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1209                          const char *Name) {
1210   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1211 }
1212
1213 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1214                           const char *Name) {
1215   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1216 }
1217
1218 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1219   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1220 }
1221
1222 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1223   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1224 }
1225
1226 /*--.. Memory ..............................................................--*/
1227
1228 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1229                              const char *Name) {
1230   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1231 }
1232
1233 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1234                                   LLVMValueRef Val, const char *Name) {
1235   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1236 }
1237
1238 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1239                              const char *Name) {
1240   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1241 }
1242
1243 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1244                                   LLVMValueRef Val, const char *Name) {
1245   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1246 }
1247
1248 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1249   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1250 }
1251
1252
1253 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1254                            const char *Name) {
1255   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1256 }
1257
1258 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1259                             LLVMValueRef PointerVal) {
1260   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1261 }
1262
1263 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1264                           LLVMValueRef *Indices, unsigned NumIndices,
1265                           const char *Name) {
1266   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1267                                    unwrap(Indices) + NumIndices, Name));
1268 }
1269
1270 /*--.. Casts ...............................................................--*/
1271
1272 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1273                             LLVMTypeRef DestTy, const char *Name) {
1274   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1275 }
1276
1277 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1278                            LLVMTypeRef DestTy, const char *Name) {
1279   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1280 }
1281
1282 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1283                            LLVMTypeRef DestTy, const char *Name) {
1284   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1285 }
1286
1287 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1288                              LLVMTypeRef DestTy, const char *Name) {
1289   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1290 }
1291
1292 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1293                              LLVMTypeRef DestTy, const char *Name) {
1294   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1295 }
1296
1297 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1298                              LLVMTypeRef DestTy, const char *Name) {
1299   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1300 }
1301
1302 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1303                              LLVMTypeRef DestTy, const char *Name) {
1304   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1305 }
1306
1307 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1308                               LLVMTypeRef DestTy, const char *Name) {
1309   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1310 }
1311
1312 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1313                             LLVMTypeRef DestTy, const char *Name) {
1314   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1315 }
1316
1317 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1318                                LLVMTypeRef DestTy, const char *Name) {
1319   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1320 }
1321
1322 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1323                                LLVMTypeRef DestTy, const char *Name) {
1324   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1325 }
1326
1327 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1328                               LLVMTypeRef DestTy, const char *Name) {
1329   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1330 }
1331
1332 /*--.. Comparisons .........................................................--*/
1333
1334 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1335                            LLVMValueRef LHS, LLVMValueRef RHS,
1336                            const char *Name) {
1337   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1338                                     unwrap(LHS), unwrap(RHS), Name));
1339 }
1340
1341 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1342                            LLVMValueRef LHS, LLVMValueRef RHS,
1343                            const char *Name) {
1344   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1345                                     unwrap(LHS), unwrap(RHS), Name));
1346 }
1347
1348 /*--.. Miscellaneous instructions ..........................................--*/
1349
1350 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1351   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1352 }
1353
1354 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1355                            LLVMValueRef *Args, unsigned NumArgs,
1356                            const char *Name) {
1357   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1358                                     unwrap(Args) + NumArgs, Name));
1359 }
1360
1361 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1362                              LLVMValueRef Then, LLVMValueRef Else,
1363                              const char *Name) {
1364   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1365                                       Name));
1366 }
1367
1368 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1369                             LLVMTypeRef Ty, const char *Name) {
1370   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1371 }
1372
1373 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1374                                       LLVMValueRef Index, const char *Name) {
1375   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1376                                               Name));
1377 }
1378
1379 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1380                                     LLVMValueRef EltVal, LLVMValueRef Index,
1381                                     const char *Name) {
1382   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1383                                              unwrap(Index), Name));
1384 }
1385
1386 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1387                                     LLVMValueRef V2, LLVMValueRef Mask,
1388                                     const char *Name) {
1389   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1390                                              unwrap(Mask), Name));
1391 }
1392
1393 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1394                                    unsigned Index, const char *Name) {
1395   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1396 }
1397
1398 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1399                                   LLVMValueRef EltVal, unsigned Index,
1400                                   const char *Name) {
1401   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1402                                            Index, Name));
1403 }
1404
1405
1406 /*===-- Module providers --------------------------------------------------===*/
1407
1408 LLVMModuleProviderRef
1409 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1410   return wrap(new ExistingModuleProvider(unwrap(M)));
1411 }
1412
1413 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1414   delete unwrap(MP);
1415 }
1416
1417
1418 /*===-- Memory buffers ----------------------------------------------------===*/
1419
1420 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1421                                              LLVMMemoryBufferRef *OutMemBuf,
1422                                              char **OutMessage) {
1423   std::string Error;
1424   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1425     *OutMemBuf = wrap(MB);
1426     return 0;
1427   }
1428   
1429   *OutMessage = strdup(Error.c_str());
1430   return 1;
1431 }
1432
1433 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1434                                     char **OutMessage) {
1435   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1436     *OutMemBuf = wrap(MB);
1437     return 0;
1438   }
1439   
1440   *OutMessage = strdup("stdin is empty.");
1441   return 1;
1442 }
1443
1444 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1445   delete unwrap(MemBuf);
1446 }