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