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