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