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