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