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