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