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