bc7137b9d00a7392cff09a8ff1379d4abbe091c6
[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   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
306   return wrap(ConstantFP::get(APN));
307 }
308
309 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
310   return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
311 }
312
313 /*--.. Operations on composite constants ...................................--*/
314
315 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
316                              int DontNullTerminate) {
317   /* Inverted the sense of AddNull because ', 0)' is a
318      better mnemonic for null termination than ', 1)'. */
319   return wrap(ConstantArray::get(std::string(Str, Length),
320                                  DontNullTerminate == 0));
321 }
322
323 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
324                             LLVMValueRef *ConstantVals, unsigned Length) {
325   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
326                                  unwrap<Constant>(ConstantVals, Length),
327                                  Length));
328 }
329
330 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
331                              int Packed) {
332   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
333                                   Count, Packed != 0));
334 }
335
336 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
337   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
338                                   Size));
339 }
340
341 /*--.. Constant expressions ................................................--*/
342
343 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
344   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
345 }
346
347 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
348   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
349 }
350
351 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
352   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
353 }
354
355 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
356   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
357                                    unwrap<Constant>(RHSConstant)));
358 }
359
360 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
361   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
362                                    unwrap<Constant>(RHSConstant)));
363 }
364
365 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
366   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
367                                    unwrap<Constant>(RHSConstant)));
368 }
369
370 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
371   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
372                                     unwrap<Constant>(RHSConstant)));
373 }
374
375 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
377                                     unwrap<Constant>(RHSConstant)));
378 }
379
380 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
381   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
382                                     unwrap<Constant>(RHSConstant)));
383 }
384
385 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
386   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
387                                     unwrap<Constant>(RHSConstant)));
388 }
389
390 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
391   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
392                                     unwrap<Constant>(RHSConstant)));
393 }
394
395 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
396   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
397                                     unwrap<Constant>(RHSConstant)));
398 }
399
400 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
401   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
402                                    unwrap<Constant>(RHSConstant)));
403 }
404
405 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
407                                   unwrap<Constant>(RHSConstant)));
408 }
409
410 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
412                                    unwrap<Constant>(RHSConstant)));
413 }
414
415 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
416                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417   return wrap(ConstantExpr::getICmp(Predicate,
418                                     unwrap<Constant>(LHSConstant),
419                                     unwrap<Constant>(RHSConstant)));
420 }
421
422 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
423                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
424   return wrap(ConstantExpr::getFCmp(Predicate,
425                                     unwrap<Constant>(LHSConstant),
426                                     unwrap<Constant>(RHSConstant)));
427 }
428
429 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
430   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
431                                   unwrap<Constant>(RHSConstant)));
432 }
433
434 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
435   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
436                                     unwrap<Constant>(RHSConstant)));
437 }
438
439 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
441                                     unwrap<Constant>(RHSConstant)));
442 }
443
444 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
445                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
446   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
447                                              unwrap<Constant>(ConstantIndices, 
448                                                               NumIndices),
449                                              NumIndices));
450 }
451
452 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
453   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
454                                      unwrap(ToType)));
455 }
456
457 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
458   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
459                                     unwrap(ToType)));
460 }
461
462 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
463   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
464                                     unwrap(ToType)));
465 }
466
467 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
468   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
469                                        unwrap(ToType)));
470 }
471
472 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
473   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
474                                         unwrap(ToType)));
475 }
476
477 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
478   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
479                                       unwrap(ToType)));
480 }
481
482 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
483   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
484                                       unwrap(ToType)));
485 }
486
487 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
488   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
489                                       unwrap(ToType)));
490 }
491
492 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
493   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
494                                       unwrap(ToType)));
495 }
496
497 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
498   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
499                                         unwrap(ToType)));
500 }
501
502 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
504                                         unwrap(ToType)));
505 }
506
507 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
509                                        unwrap(ToType)));
510 }
511
512 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
513                              LLVMValueRef ConstantIfTrue,
514                              LLVMValueRef ConstantIfFalse) {
515   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
516                                       unwrap<Constant>(ConstantIfTrue),
517                                       unwrap<Constant>(ConstantIfFalse)));
518 }
519
520 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
521                                      LLVMValueRef IndexConstant) {
522   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
523                                               unwrap<Constant>(IndexConstant)));
524 }
525
526 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
527                                     LLVMValueRef ElementValueConstant,
528                                     LLVMValueRef IndexConstant) {
529   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
530                                          unwrap<Constant>(ElementValueConstant),
531                                              unwrap<Constant>(IndexConstant)));
532 }
533
534 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
535                                     LLVMValueRef VectorBConstant,
536                                     LLVMValueRef MaskConstant) {
537   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
538                                              unwrap<Constant>(VectorBConstant),
539                                              unwrap<Constant>(MaskConstant)));
540 }
541
542 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
543
544 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
545   return wrap(unwrap<GlobalValue>(Global)->getParent());
546 }
547
548 int LLVMIsDeclaration(LLVMValueRef Global) {
549   return unwrap<GlobalValue>(Global)->isDeclaration();
550 }
551
552 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
553   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
554 }
555
556 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
557   unwrap<GlobalValue>(Global)
558     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
559 }
560
561 const char *LLVMGetSection(LLVMValueRef Global) {
562   return unwrap<GlobalValue>(Global)->getSection().c_str();
563 }
564
565 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
566   unwrap<GlobalValue>(Global)->setSection(Section);
567 }
568
569 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
570   return static_cast<LLVMVisibility>(
571     unwrap<GlobalValue>(Global)->getVisibility());
572 }
573
574 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
575   unwrap<GlobalValue>(Global)
576     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
577 }
578
579 unsigned LLVMGetAlignment(LLVMValueRef Global) {
580   return unwrap<GlobalValue>(Global)->getAlignment();
581 }
582
583 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
584   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
585 }
586
587 /*--.. Operations on global variables ......................................--*/
588
589 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
590   return wrap(new GlobalVariable(unwrap(Ty), false,
591                                  GlobalValue::ExternalLinkage, 0, Name,
592                                  unwrap(M)));
593 }
594
595 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
596   return wrap(unwrap(M)->getNamedGlobal(Name));
597 }
598
599 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
600   Module *Mod = unwrap(M);
601   Module::global_iterator I = Mod->global_begin();
602   if (I == Mod->global_end())
603     return 0;
604   return wrap(I);
605 }
606
607 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
608   Module *Mod = unwrap(M);
609   Module::global_iterator I = Mod->global_end();
610   if (I == Mod->global_begin())
611     return 0;
612   return wrap(--I);
613 }
614
615 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
616   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
617   Module::global_iterator I = GV;
618   if (++I == GV->getParent()->global_end())
619     return 0;
620   return wrap(I);
621 }
622
623 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
624   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
625   Module::global_iterator I = GV;
626   if (I == GV->getParent()->global_begin())
627     return 0;
628   return wrap(--I);
629 }
630
631 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
632   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
633 }
634
635 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
636   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
637 }
638
639 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
640   unwrap<GlobalVariable>(GlobalVar)
641     ->setInitializer(unwrap<Constant>(ConstantVal));
642 }
643
644 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
645   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
646 }
647
648 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
649   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
650 }
651
652 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
653   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
654 }
655
656 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
657   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
658 }
659
660 /*--.. Operations on functions .............................................--*/
661
662 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
663                              LLVMTypeRef FunctionTy) {
664   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
665                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
666 }
667
668 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
669   return wrap(unwrap(M)->getFunction(Name));
670 }
671
672 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
673   Module *Mod = unwrap(M);
674   Module::iterator I = Mod->begin();
675   if (I == Mod->end())
676     return 0;
677   return wrap(I);
678 }
679
680 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
681   Module *Mod = unwrap(M);
682   Module::iterator I = Mod->end();
683   if (I == Mod->begin())
684     return 0;
685   return wrap(--I);
686 }
687
688 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
689   Function *Func = unwrap<Function>(Fn);
690   Module::iterator I = Func;
691   if (++I == Func->getParent()->end())
692     return 0;
693   return wrap(I);
694 }
695
696 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
697   Function *Func = unwrap<Function>(Fn);
698   Module::iterator I = Func;
699   if (I == Func->getParent()->begin())
700     return 0;
701   return wrap(--I);
702 }
703
704 void LLVMDeleteFunction(LLVMValueRef Fn) {
705   unwrap<Function>(Fn)->eraseFromParent();
706 }
707
708 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
709   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
710     return F->getIntrinsicID();
711   return 0;
712 }
713
714 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
715   return unwrap<Function>(Fn)->getCallingConv();
716 }
717
718 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
719   return unwrap<Function>(Fn)->setCallingConv(CC);
720 }
721
722 const char *LLVMGetCollector(LLVMValueRef Fn) {
723   Function *F = unwrap<Function>(Fn);
724   return F->hasCollector()? F->getCollector() : 0;
725 }
726
727 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
728   Function *F = unwrap<Function>(Fn);
729   if (Coll)
730     F->setCollector(Coll);
731   else
732     F->clearCollector();
733 }
734
735 /*--.. Operations on parameters ............................................--*/
736
737 unsigned LLVMCountParams(LLVMValueRef FnRef) {
738   // This function is strictly redundant to
739   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
740   return unwrap<Function>(FnRef)->arg_size();
741 }
742
743 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
744   Function *Fn = unwrap<Function>(FnRef);
745   for (Function::arg_iterator I = Fn->arg_begin(),
746                               E = Fn->arg_end(); I != E; I++)
747     *ParamRefs++ = wrap(I);
748 }
749
750 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
751   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
752   while (index --> 0)
753     AI++;
754   return wrap(AI);
755 }
756
757 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
758   return wrap(unwrap<Argument>(V)->getParent());
759 }
760
761 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
762   Function *Func = unwrap<Function>(Fn);
763   Function::arg_iterator I = Func->arg_begin();
764   if (I == Func->arg_end())
765     return 0;
766   return wrap(I);
767 }
768
769 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
770   Function *Func = unwrap<Function>(Fn);
771   Function::arg_iterator I = Func->arg_end();
772   if (I == Func->arg_begin())
773     return 0;
774   return wrap(--I);
775 }
776
777 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
778   Argument *A = unwrap<Argument>(Arg);
779   Function::arg_iterator I = A;
780   if (++I == A->getParent()->arg_end())
781     return 0;
782   return wrap(I);
783 }
784
785 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
786   Argument *A = unwrap<Argument>(Arg);
787   Function::arg_iterator I = A;
788   if (I == A->getParent()->arg_begin())
789     return 0;
790   return wrap(--I);
791 }
792
793 void LLVMAddParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
794   unwrap<Argument>(Arg)->addAttr(PA);
795 }
796
797 void LLVMRemoveParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
798   unwrap<Argument>(Arg)->removeAttr(PA);
799 }
800
801 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
802   unwrap<Argument>(Arg)->addAttr(
803           ParamAttr::constructAlignmentFromInt(align));
804 }
805
806 /*--.. Operations on basic blocks ..........................................--*/
807
808 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
809   return wrap(static_cast<Value*>(unwrap(BB)));
810 }
811
812 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
813   return isa<BasicBlock>(unwrap(Val));
814 }
815
816 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
817   return wrap(unwrap<BasicBlock>(Val));
818 }
819
820 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
821   return wrap(unwrap(BB)->getParent());
822 }
823
824 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
825   return unwrap<Function>(FnRef)->size();
826 }
827
828 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
829   Function *Fn = unwrap<Function>(FnRef);
830   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
831     *BasicBlocksRefs++ = wrap(I);
832 }
833
834 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
835   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
836 }
837
838 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
839   Function *Func = unwrap<Function>(Fn);
840   Function::iterator I = Func->begin();
841   if (I == Func->end())
842     return 0;
843   return wrap(I);
844 }
845
846 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
847   Function *Func = unwrap<Function>(Fn);
848   Function::iterator I = Func->end();
849   if (I == Func->begin())
850     return 0;
851   return wrap(--I);
852 }
853
854 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
855   BasicBlock *Block = unwrap(BB);
856   Function::iterator I = Block;
857   if (++I == Block->getParent()->end())
858     return 0;
859   return wrap(I);
860 }
861
862 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
863   BasicBlock *Block = unwrap(BB);
864   Function::iterator I = Block;
865   if (I == Block->getParent()->begin())
866     return 0;
867   return wrap(--I);
868 }
869
870 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
871   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
872 }
873
874 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
875                                        const char *Name) {
876   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
877   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
878                                  InsertBeforeBB));
879 }
880
881 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
882   unwrap(BBRef)->eraseFromParent();
883 }
884
885 /*--.. Operations on instructions ..........................................--*/
886
887 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
888   return wrap(unwrap<Instruction>(Inst)->getParent());
889 }
890
891 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
892   BasicBlock *Block = unwrap(BB);
893   BasicBlock::iterator I = Block->begin();
894   if (I == Block->end())
895     return 0;
896   return wrap(I);
897 }
898
899 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
900   BasicBlock *Block = unwrap(BB);
901   BasicBlock::iterator I = Block->end();
902   if (I == Block->begin())
903     return 0;
904   return wrap(--I);
905 }
906
907 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
908   Instruction *Instr = unwrap<Instruction>(Inst);
909   BasicBlock::iterator I = Instr;
910   if (++I == Instr->getParent()->end())
911     return 0;
912   return wrap(I);
913 }
914
915 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
916   Instruction *Instr = unwrap<Instruction>(Inst);
917   BasicBlock::iterator I = Instr;
918   if (I == Instr->getParent()->begin())
919     return 0;
920   return wrap(--I);
921 }
922
923 /*--.. Call and invoke instructions ........................................--*/
924
925 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
926   Value *V = unwrap(Instr);
927   if (CallInst *CI = dyn_cast<CallInst>(V))
928     return CI->getCallingConv();
929   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
930     return II->getCallingConv();
931   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
932   return 0;
933 }
934
935 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
936   Value *V = unwrap(Instr);
937   if (CallInst *CI = dyn_cast<CallInst>(V))
938     return CI->setCallingConv(CC);
939   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
940     return II->setCallingConv(CC);
941   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
942 }
943
944 void LLVMAddInstrParamAttr(LLVMValueRef Instr, unsigned index, 
945                            LLVMParamAttr PA) {
946   CallSite Call = CallSite(unwrap<Instruction>(Instr));
947   Call.setParamAttrs(
948     Call.getParamAttrs().addAttr(index, PA));
949 }
950
951 void LLVMRemoveInstrParamAttr(LLVMValueRef Instr, unsigned index, 
952                               LLVMParamAttr PA) {
953   CallSite Call = CallSite(unwrap<Instruction>(Instr));
954   Call.setParamAttrs(
955     Call.getParamAttrs().removeAttr(index, PA));
956 }
957
958 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
959                                 unsigned align) {
960   CallSite Call = CallSite(unwrap<Instruction>(Instr));
961   Call.setParamAttrs(
962     Call.getParamAttrs().addAttr(index, 
963         ParamAttr::constructAlignmentFromInt(align)));
964 }
965
966 /*--.. Operations on phi nodes .............................................--*/
967
968 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
969                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
970   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
971   for (unsigned I = 0; I != Count; ++I)
972     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
973 }
974
975 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
976   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
977 }
978
979 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
980   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
981 }
982
983 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
984   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
985 }
986
987
988 /*===-- Instruction builders ----------------------------------------------===*/
989
990 LLVMBuilderRef LLVMCreateBuilder(void) {
991   return wrap(new IRBuilder<>());
992 }
993
994 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
995                          LLVMValueRef Instr) {
996   BasicBlock *BB = unwrap(Block);
997   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
998   unwrap(Builder)->SetInsertPoint(BB, I);
999 }
1000
1001 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1002   Instruction *I = unwrap<Instruction>(Instr);
1003   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1004 }
1005
1006 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1007   BasicBlock *BB = unwrap(Block);
1008   unwrap(Builder)->SetInsertPoint(BB);
1009 }
1010
1011 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1012    return wrap(unwrap(Builder)->GetInsertBlock());
1013 }
1014
1015 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1016   delete unwrap(Builder);
1017 }
1018
1019 /*--.. Instruction builders ................................................--*/
1020
1021 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1022   return wrap(unwrap(B)->CreateRetVoid());
1023 }
1024
1025 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1026   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1027 }
1028
1029 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1030   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1031 }
1032
1033 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1034                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1035   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1036 }
1037
1038 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1039                              LLVMBasicBlockRef Else, unsigned NumCases) {
1040   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1041 }
1042
1043 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1044                              LLVMValueRef *Args, unsigned NumArgs,
1045                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1046                              const char *Name) {
1047   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1048                                       unwrap(Args), unwrap(Args) + NumArgs,
1049                                       Name));
1050 }
1051
1052 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1053   return wrap(unwrap(B)->CreateUnwind());
1054 }
1055
1056 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1057   return wrap(unwrap(B)->CreateUnreachable());
1058 }
1059
1060 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1061                  LLVMBasicBlockRef Dest) {
1062   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1063 }
1064
1065 /*--.. Arithmetic ..........................................................--*/
1066
1067 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1068                           const char *Name) {
1069   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1070 }
1071
1072 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1073                           const char *Name) {
1074   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1075 }
1076
1077 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1078                           const char *Name) {
1079   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1080 }
1081
1082 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1083                            const char *Name) {
1084   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1085 }
1086
1087 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1088                            const char *Name) {
1089   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1090 }
1091
1092 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1093                            const char *Name) {
1094   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1095 }
1096
1097 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1098                            const char *Name) {
1099   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1100 }
1101
1102 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1103                            const char *Name) {
1104   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1105 }
1106
1107 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1108                            const char *Name) {
1109   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1110 }
1111
1112 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1113                           const char *Name) {
1114   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1115 }
1116
1117 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1118                            const char *Name) {
1119   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1120 }
1121
1122 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1123                            const char *Name) {
1124   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1125 }
1126
1127 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1128                           const char *Name) {
1129   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1130 }
1131
1132 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1133                          const char *Name) {
1134   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1135 }
1136
1137 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1138                           const char *Name) {
1139   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1140 }
1141
1142 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1143   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1144 }
1145
1146 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1147   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1148 }
1149
1150 /*--.. Memory ..............................................................--*/
1151
1152 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1153                              const char *Name) {
1154   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1155 }
1156
1157 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1158                                   LLVMValueRef Val, const char *Name) {
1159   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1160 }
1161
1162 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1163                              const char *Name) {
1164   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1165 }
1166
1167 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1168                                   LLVMValueRef Val, const char *Name) {
1169   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1170 }
1171
1172 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1173   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1174 }
1175
1176
1177 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1178                            const char *Name) {
1179   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1180 }
1181
1182 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1183                             LLVMValueRef PointerVal) {
1184   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1185 }
1186
1187 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1188                           LLVMValueRef *Indices, unsigned NumIndices,
1189                           const char *Name) {
1190   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1191                                    unwrap(Indices) + NumIndices, Name));
1192 }
1193
1194 /*--.. Casts ...............................................................--*/
1195
1196 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1197                             LLVMTypeRef DestTy, const char *Name) {
1198   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1199 }
1200
1201 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1202                            LLVMTypeRef DestTy, const char *Name) {
1203   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1204 }
1205
1206 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1207                            LLVMTypeRef DestTy, const char *Name) {
1208   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1209 }
1210
1211 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1212                              LLVMTypeRef DestTy, const char *Name) {
1213   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1214 }
1215
1216 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1217                              LLVMTypeRef DestTy, const char *Name) {
1218   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1219 }
1220
1221 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1222                              LLVMTypeRef DestTy, const char *Name) {
1223   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1224 }
1225
1226 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1227                              LLVMTypeRef DestTy, const char *Name) {
1228   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1229 }
1230
1231 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1232                               LLVMTypeRef DestTy, const char *Name) {
1233   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1234 }
1235
1236 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1237                             LLVMTypeRef DestTy, const char *Name) {
1238   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1239 }
1240
1241 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1242                                LLVMTypeRef DestTy, const char *Name) {
1243   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1244 }
1245
1246 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1247                                LLVMTypeRef DestTy, const char *Name) {
1248   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1249 }
1250
1251 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1252                               LLVMTypeRef DestTy, const char *Name) {
1253   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1254 }
1255
1256 /*--.. Comparisons .........................................................--*/
1257
1258 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1259                            LLVMValueRef LHS, LLVMValueRef RHS,
1260                            const char *Name) {
1261   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1262                                     unwrap(LHS), unwrap(RHS), Name));
1263 }
1264
1265 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1266                            LLVMValueRef LHS, LLVMValueRef RHS,
1267                            const char *Name) {
1268   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1269                                     unwrap(LHS), unwrap(RHS), Name));
1270 }
1271
1272 /*--.. Miscellaneous instructions ..........................................--*/
1273
1274 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1275   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1276 }
1277
1278 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1279                            LLVMValueRef *Args, unsigned NumArgs,
1280                            const char *Name) {
1281   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1282                                     unwrap(Args) + NumArgs, Name));
1283 }
1284
1285 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1286                              LLVMValueRef Then, LLVMValueRef Else,
1287                              const char *Name) {
1288   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1289                                       Name));
1290 }
1291
1292 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1293                             LLVMTypeRef Ty, const char *Name) {
1294   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1295 }
1296
1297 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1298                                       LLVMValueRef Index, const char *Name) {
1299   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1300                                               Name));
1301 }
1302
1303 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1304                                     LLVMValueRef EltVal, LLVMValueRef Index,
1305                                     const char *Name) {
1306   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1307                                              unwrap(Index), Name));
1308 }
1309
1310 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1311                                     LLVMValueRef V2, LLVMValueRef Mask,
1312                                     const char *Name) {
1313   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1314                                              unwrap(Mask), Name));
1315 }
1316
1317
1318 /*===-- Module providers --------------------------------------------------===*/
1319
1320 LLVMModuleProviderRef
1321 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1322   return wrap(new ExistingModuleProvider(unwrap(M)));
1323 }
1324
1325 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1326   delete unwrap(MP);
1327 }
1328
1329
1330 /*===-- Memory buffers ----------------------------------------------------===*/
1331
1332 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1333                                              LLVMMemoryBufferRef *OutMemBuf,
1334                                              char **OutMessage) {
1335   std::string Error;
1336   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1337     *OutMemBuf = wrap(MB);
1338     return 0;
1339   }
1340   
1341   *OutMessage = strdup(Error.c_str());
1342   return 1;
1343 }
1344
1345 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1346                                     char **OutMessage) {
1347   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1348     *OutMemBuf = wrap(MB);
1349     return 0;
1350   }
1351   
1352   *OutMessage = strdup("stdin is empty.");
1353   return 1;
1354 }
1355
1356 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1357   delete unwrap(MemBuf);
1358 }