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