Add an llvm-c function that lets you insert an instruction with a name.
[oota-llvm.git] / lib / VMCore / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32
33 using namespace llvm;
34
35
36 /*===-- Error handling ----------------------------------------------------===*/
37
38 void LLVMDisposeMessage(char *Message) {
39   free(Message);
40 }
41
42
43 /*===-- Operations on contexts --------------------------------------------===*/
44
45 LLVMContextRef LLVMContextCreate() {
46   return wrap(new LLVMContext());
47 }
48
49 LLVMContextRef LLVMGetGlobalContext() {
50   return wrap(&getGlobalContext());
51 }
52
53 void LLVMContextDispose(LLVMContextRef C) {
54   delete unwrap(C);
55 }
56
57
58 /*===-- Operations on modules ---------------------------------------------===*/
59
60 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
61   return wrap(new Module(ModuleID, getGlobalContext()));
62 }
63
64 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
65                                                 LLVMContextRef C) {
66   return wrap(new Module(ModuleID, *unwrap(C)));
67 }
68
69 void LLVMDisposeModule(LLVMModuleRef M) {
70   delete unwrap(M);
71 }
72
73 /*--.. Data layout .........................................................--*/
74 const char * LLVMGetDataLayout(LLVMModuleRef M) {
75   return unwrap(M)->getDataLayout().c_str();
76 }
77
78 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
79   unwrap(M)->setDataLayout(Triple);
80 }
81
82 /*--.. Target triple .......................................................--*/
83 const char * LLVMGetTarget(LLVMModuleRef M) {
84   return unwrap(M)->getTargetTriple().c_str();
85 }
86
87 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
88   unwrap(M)->setTargetTriple(Triple);
89 }
90
91 /*--.. Type names ..........................................................--*/
92 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
93   return unwrap(M)->addTypeName(Name, unwrap(Ty));
94 }
95
96 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
97   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
98
99   TypeSymbolTable::iterator I = TST.find(Name);
100   if (I != TST.end())
101     TST.remove(I);
102 }
103
104 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
105   return wrap(unwrap(M)->getTypeByName(Name));
106 }
107
108 void LLVMDumpModule(LLVMModuleRef M) {
109   unwrap(M)->dump();
110 }
111
112
113 /*===-- Operations on types -----------------------------------------------===*/
114
115 /*--.. Operations on all types (mostly) ....................................--*/
116
117 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
118   switch (unwrap(Ty)->getTypeID()) {
119   default:
120     assert(false && "Unhandled TypeID.");
121   case Type::VoidTyID:
122     return LLVMVoidTypeKind;
123   case Type::FloatTyID:
124     return LLVMFloatTypeKind;
125   case Type::DoubleTyID:
126     return LLVMDoubleTypeKind;
127   case Type::X86_FP80TyID:
128     return LLVMX86_FP80TypeKind;
129   case Type::FP128TyID:
130     return LLVMFP128TypeKind;
131   case Type::PPC_FP128TyID:
132     return LLVMPPC_FP128TypeKind;
133   case Type::LabelTyID:
134     return LLVMLabelTypeKind;
135   case Type::MetadataTyID:
136     return LLVMMetadataTypeKind;
137   case Type::IntegerTyID:
138     return LLVMIntegerTypeKind;
139   case Type::FunctionTyID:
140     return LLVMFunctionTypeKind;
141   case Type::StructTyID:
142     return LLVMStructTypeKind;
143   case Type::ArrayTyID:
144     return LLVMArrayTypeKind;
145   case Type::PointerTyID:
146     return LLVMPointerTypeKind;
147   case Type::OpaqueTyID:
148     return LLVMOpaqueTypeKind;
149   case Type::VectorTyID:
150     return LLVMVectorTypeKind;
151   }
152 }
153
154 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
155   return wrap(&unwrap(Ty)->getContext());
156 }
157
158 /*--.. Operations on integer types .........................................--*/
159
160 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
161   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
162 }
163 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
164   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
165 }
166 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
167   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
168 }
169 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
170   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
171 }
172 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
173   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
174 }
175 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
176   return wrap(IntegerType::get(*unwrap(C), NumBits));
177 }
178
179 LLVMTypeRef LLVMInt1Type(void)  {
180   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
181 }
182 LLVMTypeRef LLVMInt8Type(void)  {
183   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
184 }
185 LLVMTypeRef LLVMInt16Type(void) {
186   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
187 }
188 LLVMTypeRef LLVMInt32Type(void) {
189   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
190 }
191 LLVMTypeRef LLVMInt64Type(void) {
192   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
193 }
194 LLVMTypeRef LLVMIntType(unsigned NumBits) {
195   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
196 }
197
198 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
199   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
200 }
201
202 /*--.. Operations on real types ............................................--*/
203
204 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
205   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
206 }
207 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
208   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
209 }
210 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
211   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
212 }
213 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
214   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
215 }
216 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
217   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
218 }
219
220 LLVMTypeRef LLVMFloatType(void) {
221   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
222 }
223 LLVMTypeRef LLVMDoubleType(void) {
224   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
225 }
226 LLVMTypeRef LLVMX86FP80Type(void) {
227   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
228 }
229 LLVMTypeRef LLVMFP128Type(void) {
230   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
231 }
232 LLVMTypeRef LLVMPPCFP128Type(void) {
233   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
234 }
235
236 /*--.. Operations on function types ........................................--*/
237
238 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
239                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
240                              int IsVarArg) {
241   std::vector<const Type*> Tys;
242   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
243     Tys.push_back(unwrap(*I));
244   
245   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
246 }
247
248 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
249   return unwrap<FunctionType>(FunctionTy)->isVarArg();
250 }
251
252 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
253   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
254 }
255
256 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
257   return unwrap<FunctionType>(FunctionTy)->getNumParams();
258 }
259
260 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
261   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
262   for (FunctionType::param_iterator I = Ty->param_begin(),
263                                     E = Ty->param_end(); I != E; ++I)
264     *Dest++ = wrap(*I);
265 }
266
267 /*--.. Operations on struct types ..........................................--*/
268
269 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
270                            unsigned ElementCount, int Packed) {
271   std::vector<const Type*> Tys;
272   for (LLVMTypeRef *I = ElementTypes,
273                    *E = ElementTypes + ElementCount; I != E; ++I)
274     Tys.push_back(unwrap(*I));
275   
276   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
277 }
278
279 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
280                            unsigned ElementCount, int Packed) {
281   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
282                                  ElementCount, Packed);
283 }
284
285
286 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
287   return unwrap<StructType>(StructTy)->getNumElements();
288 }
289
290 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
291   StructType *Ty = unwrap<StructType>(StructTy);
292   for (FunctionType::param_iterator I = Ty->element_begin(),
293                                     E = Ty->element_end(); I != E; ++I)
294     *Dest++ = wrap(*I);
295 }
296
297 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
298   return unwrap<StructType>(StructTy)->isPacked();
299 }
300
301 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
302
303 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
304   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
305 }
306
307 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
308   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
309 }
310
311 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
312   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
313 }
314
315 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
316   return wrap(unwrap<SequentialType>(Ty)->getElementType());
317 }
318
319 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
320   return unwrap<ArrayType>(ArrayTy)->getNumElements();
321 }
322
323 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
324   return unwrap<PointerType>(PointerTy)->getAddressSpace();
325 }
326
327 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
328   return unwrap<VectorType>(VectorTy)->getNumElements();
329 }
330
331 /*--.. Operations on other types ...........................................--*/
332
333 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
334   return wrap(Type::getVoidTy(*unwrap(C)));
335 }
336 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
337   return wrap(Type::getLabelTy(*unwrap(C)));
338 }
339 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
340   return wrap(OpaqueType::get(*unwrap(C)));
341 }
342
343 LLVMTypeRef LLVMVoidType(void)  {
344   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
345 }
346 LLVMTypeRef LLVMLabelType(void) {
347   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
348 }
349 LLVMTypeRef LLVMOpaqueType(void) {
350   return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
351 }
352
353 /*--.. Operations on type handles ..........................................--*/
354
355 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
356   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
357 }
358
359 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
360   delete unwrap(TypeHandle);
361 }
362
363 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
364   return wrap(unwrap(TypeHandle)->get());
365 }
366
367 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
368   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
369 }
370
371
372 /*===-- Operations on values ----------------------------------------------===*/
373
374 /*--.. Operations on all values ............................................--*/
375
376 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
377   return wrap(unwrap(Val)->getType());
378 }
379
380 const char *LLVMGetValueName(LLVMValueRef Val) {
381   return unwrap(Val)->getName().data();
382 }
383
384 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
385   unwrap(Val)->setName(Name);
386 }
387
388 void LLVMDumpValue(LLVMValueRef Val) {
389   unwrap(Val)->dump();
390 }
391
392
393 /*--.. Conversion functions ................................................--*/
394
395 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
396   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
397     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
398   }
399
400 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
401
402
403 /*--.. Operations on constants of any type .................................--*/
404
405 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
406   return wrap(Constant::getNullValue(unwrap(Ty)));
407 }
408
409 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
410   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
411 }
412
413 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
414   return wrap(UndefValue::get(unwrap(Ty)));
415 }
416
417 int LLVMIsConstant(LLVMValueRef Ty) {
418   return isa<Constant>(unwrap(Ty));
419 }
420
421 int LLVMIsNull(LLVMValueRef Val) {
422   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
423     return C->isNullValue();
424   return false;
425 }
426
427 int LLVMIsUndef(LLVMValueRef Val) {
428   return isa<UndefValue>(unwrap(Val));
429 }
430
431 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
432   return
433       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
434 }
435
436 /*--.. Operations on scalar constants ......................................--*/
437
438 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
439                           int SignExtend) {
440   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
441 }
442
443 static const fltSemantics &SemanticsForType(Type *Ty) {
444   assert(Ty->isFloatingPoint() && "Type is not floating point!");
445   if (Ty == Type::getFloatTy(Ty->getContext()))
446     return APFloat::IEEEsingle;
447   if (Ty == Type::getDoubleTy(Ty->getContext()))
448     return APFloat::IEEEdouble;
449   if (Ty == Type::getX86_FP80Ty(Ty->getContext()))
450     return APFloat::x87DoubleExtended;
451   if (Ty == Type::getFP128Ty(Ty->getContext()))
452     return APFloat::IEEEquad;
453   if (Ty == Type::getPPC_FP128Ty(Ty->getContext()))
454     return APFloat::PPCDoubleDouble;
455   return APFloat::Bogus;
456 }
457
458 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
459   APFloat APN(N);
460   bool ignored;
461   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
462               &ignored);
463   return wrap(ConstantFP::get(getGlobalContext(), APN));
464 }
465
466 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
467   return wrap(ConstantFP::get(getGlobalContext(),
468                               APFloat(SemanticsForType(unwrap(RealTy)), Text)));
469 }
470
471 /*--.. Operations on composite constants ...................................--*/
472
473 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
474                                       unsigned Length, int DontNullTerminate) {
475   /* Inverted the sense of AddNull because ', 0)' is a
476      better mnemonic for null termination than ', 1)'. */
477   return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
478                                  DontNullTerminate == 0));
479 }
480 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
481                                       LLVMValueRef *ConstantVals,
482                                       unsigned Count, int Packed) {
483   return wrap(ConstantStruct::get(*unwrap(C),
484                                   unwrap<Constant>(ConstantVals, Count),
485                                   Count, Packed != 0));
486 }
487
488 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
489                              int DontNullTerminate) {
490   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
491                                   DontNullTerminate);
492 }
493 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
494                             LLVMValueRef *ConstantVals, unsigned Length) {
495   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
496                                  unwrap<Constant>(ConstantVals, Length),
497                                  Length));
498 }
499 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
500                              int Packed) {
501   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
502                                   Packed);
503 }
504
505 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
506   return wrap(ConstantVector::get(
507                             unwrap<Constant>(ScalarConstantVals, Size), Size));
508 }
509
510 /*--.. Constant expressions ................................................--*/
511
512 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
513   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
514 }
515
516 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
517   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
518 }
519
520 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
521   return wrap(ConstantExpr::getNeg(
522                                    unwrap<Constant>(ConstantVal)));
523 }
524
525 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
526   return wrap(ConstantExpr::getFNeg(
527                                     unwrap<Constant>(ConstantVal)));
528 }
529
530 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
531   return wrap(ConstantExpr::getNot(
532                                    unwrap<Constant>(ConstantVal)));
533 }
534
535 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
536   return wrap(ConstantExpr::getAdd(
537                                    unwrap<Constant>(LHSConstant),
538                                    unwrap<Constant>(RHSConstant)));
539 }
540
541 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
542                              LLVMValueRef RHSConstant) {
543   return wrap(ConstantExpr::getNSWAdd(
544                                       unwrap<Constant>(LHSConstant),
545                                       unwrap<Constant>(RHSConstant)));
546 }
547
548 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
549   return wrap(ConstantExpr::getFAdd(
550                                     unwrap<Constant>(LHSConstant),
551                                     unwrap<Constant>(RHSConstant)));
552 }
553
554 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
555   return wrap(ConstantExpr::getSub(
556                                    unwrap<Constant>(LHSConstant),
557                                    unwrap<Constant>(RHSConstant)));
558 }
559
560 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
561   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
562                                     unwrap<Constant>(RHSConstant)));
563 }
564
565 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
566   return wrap(ConstantExpr::getMul(
567                                    unwrap<Constant>(LHSConstant),
568                                    unwrap<Constant>(RHSConstant)));
569 }
570
571 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
572   return wrap(ConstantExpr::getFMul(
573                                     unwrap<Constant>(LHSConstant),
574                                     unwrap<Constant>(RHSConstant)));
575 }
576
577 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
578   return wrap(ConstantExpr::getUDiv(
579                                     unwrap<Constant>(LHSConstant),
580                                     unwrap<Constant>(RHSConstant)));
581 }
582
583 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
584   return wrap(ConstantExpr::getSDiv(
585                                     unwrap<Constant>(LHSConstant),
586                                     unwrap<Constant>(RHSConstant)));
587 }
588
589 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
590                                 LLVMValueRef RHSConstant) {
591   return wrap(ConstantExpr::getExactSDiv(
592                                          unwrap<Constant>(LHSConstant),
593                                          unwrap<Constant>(RHSConstant)));
594 }
595
596 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
597   return wrap(ConstantExpr::getFDiv(
598                                     unwrap<Constant>(LHSConstant),
599                                     unwrap<Constant>(RHSConstant)));
600 }
601
602 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
603   return wrap(ConstantExpr::getURem(
604                                     unwrap<Constant>(LHSConstant),
605                                     unwrap<Constant>(RHSConstant)));
606 }
607
608 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
609   return wrap(ConstantExpr::getSRem(
610                                     unwrap<Constant>(LHSConstant),
611                                     unwrap<Constant>(RHSConstant)));
612 }
613
614 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
615   return wrap(ConstantExpr::getFRem(
616                                     unwrap<Constant>(LHSConstant),
617                                     unwrap<Constant>(RHSConstant)));
618 }
619
620 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
621   return wrap(ConstantExpr::getAnd(
622                                    unwrap<Constant>(LHSConstant),
623                                    unwrap<Constant>(RHSConstant)));
624 }
625
626 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
627   return wrap(ConstantExpr::getOr(
628                                   unwrap<Constant>(LHSConstant),
629                                   unwrap<Constant>(RHSConstant)));
630 }
631
632 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
633   return wrap(ConstantExpr::getXor(
634                                    unwrap<Constant>(LHSConstant),
635                                    unwrap<Constant>(RHSConstant)));
636 }
637
638 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
639                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
640   return wrap(ConstantExpr::getICmp(Predicate,
641                                     unwrap<Constant>(LHSConstant),
642                                     unwrap<Constant>(RHSConstant)));
643 }
644
645 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
646                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
647   return wrap(ConstantExpr::getFCmp(Predicate,
648                                     unwrap<Constant>(LHSConstant),
649                                     unwrap<Constant>(RHSConstant)));
650 }
651
652 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
653   return wrap(ConstantExpr::getShl(
654                                   unwrap<Constant>(LHSConstant),
655                                   unwrap<Constant>(RHSConstant)));
656 }
657
658 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
659   return wrap(ConstantExpr::getLShr(
660                                     unwrap<Constant>(LHSConstant),
661                                     unwrap<Constant>(RHSConstant)));
662 }
663
664 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
665   return wrap(ConstantExpr::getAShr(
666                                     unwrap<Constant>(LHSConstant),
667                                     unwrap<Constant>(RHSConstant)));
668 }
669
670 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
671                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
672   return wrap(ConstantExpr::getGetElementPtr(
673                                              unwrap<Constant>(ConstantVal),
674                                              unwrap<Constant>(ConstantIndices, 
675                                                               NumIndices),
676                                              NumIndices));
677 }
678
679 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
680                                   LLVMValueRef *ConstantIndices,
681                                   unsigned NumIndices) {
682   Constant* Val = unwrap<Constant>(ConstantVal);
683   Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
684   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
685 }
686
687 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
688   return wrap(ConstantExpr::getTrunc(
689                                      unwrap<Constant>(ConstantVal),
690                                      unwrap(ToType)));
691 }
692
693 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
694   return wrap(ConstantExpr::getSExt(
695                                     unwrap<Constant>(ConstantVal),
696                                     unwrap(ToType)));
697 }
698
699 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
700   return wrap(ConstantExpr::getZExt(
701                                     unwrap<Constant>(ConstantVal),
702                                     unwrap(ToType)));
703 }
704
705 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
706   return wrap(ConstantExpr::getFPTrunc(
707                                        unwrap<Constant>(ConstantVal),
708                                        unwrap(ToType)));
709 }
710
711 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
712   return wrap(ConstantExpr::getFPExtend(
713                                         unwrap<Constant>(ConstantVal),
714                                         unwrap(ToType)));
715 }
716
717 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
718   return wrap(ConstantExpr::getUIToFP(
719                                       unwrap<Constant>(ConstantVal),
720                                       unwrap(ToType)));
721 }
722
723 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
724   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
725                                       unwrap(ToType)));
726 }
727
728 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
729   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
730                                       unwrap(ToType)));
731 }
732
733 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
734   return wrap(ConstantExpr::getFPToSI(
735                                       unwrap<Constant>(ConstantVal),
736                                       unwrap(ToType)));
737 }
738
739 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
740   return wrap(ConstantExpr::getPtrToInt(
741                                         unwrap<Constant>(ConstantVal),
742                                         unwrap(ToType)));
743 }
744
745 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
746   return wrap(ConstantExpr::getIntToPtr(
747                                         unwrap<Constant>(ConstantVal),
748                                         unwrap(ToType)));
749 }
750
751 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
752   return wrap(ConstantExpr::getBitCast(
753                                        unwrap<Constant>(ConstantVal),
754                                        unwrap(ToType)));
755 }
756
757 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
758                                     LLVMTypeRef ToType) {
759   return wrap(ConstantExpr::getZExtOrBitCast(
760                                              unwrap<Constant>(ConstantVal),
761                                              unwrap(ToType)));
762 }
763
764 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
765                                     LLVMTypeRef ToType) {
766   return wrap(ConstantExpr::getSExtOrBitCast(
767                                              unwrap<Constant>(ConstantVal),
768                                              unwrap(ToType)));
769 }
770
771 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
772                                      LLVMTypeRef ToType) {
773   return wrap(ConstantExpr::getTruncOrBitCast(
774                                               unwrap<Constant>(ConstantVal),
775                                               unwrap(ToType)));
776 }
777
778 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
779                                   LLVMTypeRef ToType) {
780   return wrap(ConstantExpr::getPointerCast(
781                                            unwrap<Constant>(ConstantVal),
782                                            unwrap(ToType)));
783 }
784
785 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
786                               unsigned isSigned) {
787   return wrap(ConstantExpr::getIntegerCast(
788                                            unwrap<Constant>(ConstantVal),
789                                            unwrap(ToType),
790                                            isSigned));
791 }
792
793 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
794   return wrap(ConstantExpr::getFPCast(
795                                       unwrap<Constant>(ConstantVal),
796                                       unwrap(ToType)));
797 }
798
799 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
800                              LLVMValueRef ConstantIfTrue,
801                              LLVMValueRef ConstantIfFalse) {
802   return wrap(ConstantExpr::getSelect(
803                                       unwrap<Constant>(ConstantCondition),
804                                       unwrap<Constant>(ConstantIfTrue),
805                                       unwrap<Constant>(ConstantIfFalse)));
806 }
807
808 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
809                                      LLVMValueRef IndexConstant) {
810   return wrap(ConstantExpr::getExtractElement(
811                                               unwrap<Constant>(VectorConstant),
812                                               unwrap<Constant>(IndexConstant)));
813 }
814
815 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
816                                     LLVMValueRef ElementValueConstant,
817                                     LLVMValueRef IndexConstant) {
818   return wrap(ConstantExpr::getInsertElement(
819                                          unwrap<Constant>(VectorConstant),
820                                          unwrap<Constant>(ElementValueConstant),
821                                              unwrap<Constant>(IndexConstant)));
822 }
823
824 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
825                                     LLVMValueRef VectorBConstant,
826                                     LLVMValueRef MaskConstant) {
827   return wrap(ConstantExpr::getShuffleVector(
828                                              unwrap<Constant>(VectorAConstant),
829                                              unwrap<Constant>(VectorBConstant),
830                                              unwrap<Constant>(MaskConstant)));
831 }
832
833 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
834                                    unsigned NumIdx) {
835   return wrap(ConstantExpr::getExtractValue(
836                                             unwrap<Constant>(AggConstant),
837                                             IdxList, NumIdx));
838 }
839
840 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
841                                   LLVMValueRef ElementValueConstant,
842                                   unsigned *IdxList, unsigned NumIdx) {
843   return wrap(ConstantExpr::getInsertValue(
844                                          unwrap<Constant>(AggConstant),
845                                          unwrap<Constant>(ElementValueConstant),
846                                            IdxList, NumIdx));
847 }
848
849 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
850                                 const char *Constraints, int HasSideEffects) {
851   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
852                              Constraints, HasSideEffects));
853 }
854
855 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
856
857 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
858   return wrap(unwrap<GlobalValue>(Global)->getParent());
859 }
860
861 int LLVMIsDeclaration(LLVMValueRef Global) {
862   return unwrap<GlobalValue>(Global)->isDeclaration();
863 }
864
865 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
866   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
867   default:
868     assert(false && "Unhandled Linkage Type.");
869   case GlobalValue::ExternalLinkage:
870     return LLVMExternalLinkage;
871   case GlobalValue::AvailableExternallyLinkage:
872     return LLVMAvailableExternallyLinkage;
873   case GlobalValue::LinkOnceAnyLinkage:
874     return LLVMLinkOnceAnyLinkage;
875   case GlobalValue::LinkOnceODRLinkage:
876     return LLVMLinkOnceODRLinkage;
877   case GlobalValue::WeakAnyLinkage:
878     return LLVMWeakAnyLinkage;
879   case GlobalValue::WeakODRLinkage:
880     return LLVMWeakODRLinkage;
881   case GlobalValue::AppendingLinkage:
882     return LLVMAppendingLinkage;
883   case GlobalValue::InternalLinkage:
884     return LLVMInternalLinkage;
885   case GlobalValue::PrivateLinkage:
886     return LLVMPrivateLinkage;
887   case GlobalValue::LinkerPrivateLinkage:
888     return LLVMLinkerPrivateLinkage;
889   case GlobalValue::DLLImportLinkage:
890     return LLVMDLLImportLinkage;
891   case GlobalValue::DLLExportLinkage:
892     return LLVMDLLExportLinkage;
893   case GlobalValue::ExternalWeakLinkage:
894     return LLVMExternalWeakLinkage;
895   case GlobalValue::GhostLinkage:
896     return LLVMGhostLinkage;
897   case GlobalValue::CommonLinkage:
898     return LLVMCommonLinkage;
899   }
900
901   // Should never get here.
902   return static_cast<LLVMLinkage>(0);
903 }
904
905 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
906   GlobalValue *GV = unwrap<GlobalValue>(Global);
907
908   switch (Linkage) {
909   default:
910     assert(false && "Unhandled Linkage Type.");
911   case LLVMExternalLinkage:
912     GV->setLinkage(GlobalValue::ExternalLinkage);
913     break;
914   case LLVMAvailableExternallyLinkage:
915     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
916     break;
917   case LLVMLinkOnceAnyLinkage:
918     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
919     break;
920   case LLVMLinkOnceODRLinkage:
921     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
922     break;
923   case LLVMWeakAnyLinkage:
924     GV->setLinkage(GlobalValue::WeakAnyLinkage);
925     break;
926   case LLVMWeakODRLinkage:
927     GV->setLinkage(GlobalValue::WeakODRLinkage);
928     break;
929   case LLVMAppendingLinkage:
930     GV->setLinkage(GlobalValue::AppendingLinkage);
931     break;
932   case LLVMInternalLinkage:
933     GV->setLinkage(GlobalValue::InternalLinkage);
934     break;
935   case LLVMPrivateLinkage:
936     GV->setLinkage(GlobalValue::PrivateLinkage);
937     break;
938   case LLVMLinkerPrivateLinkage:
939     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
940     break;
941   case LLVMDLLImportLinkage:
942     GV->setLinkage(GlobalValue::DLLImportLinkage);
943     break;
944   case LLVMDLLExportLinkage:
945     GV->setLinkage(GlobalValue::DLLExportLinkage);
946     break;
947   case LLVMExternalWeakLinkage:
948     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
949     break;
950   case LLVMGhostLinkage:
951     GV->setLinkage(GlobalValue::GhostLinkage);
952     break;
953   case LLVMCommonLinkage:
954     GV->setLinkage(GlobalValue::CommonLinkage);
955     break;
956   }
957 }
958
959 const char *LLVMGetSection(LLVMValueRef Global) {
960   return unwrap<GlobalValue>(Global)->getSection().c_str();
961 }
962
963 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
964   unwrap<GlobalValue>(Global)->setSection(Section);
965 }
966
967 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
968   return static_cast<LLVMVisibility>(
969     unwrap<GlobalValue>(Global)->getVisibility());
970 }
971
972 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
973   unwrap<GlobalValue>(Global)
974     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
975 }
976
977 unsigned LLVMGetAlignment(LLVMValueRef Global) {
978   return unwrap<GlobalValue>(Global)->getAlignment();
979 }
980
981 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
982   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
983 }
984
985 /*--.. Operations on global variables ......................................--*/
986
987 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
988   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
989                                  GlobalValue::ExternalLinkage, 0, Name));
990 }
991
992 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
993   return wrap(unwrap(M)->getNamedGlobal(Name));
994 }
995
996 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
997   Module *Mod = unwrap(M);
998   Module::global_iterator I = Mod->global_begin();
999   if (I == Mod->global_end())
1000     return 0;
1001   return wrap(I);
1002 }
1003
1004 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1005   Module *Mod = unwrap(M);
1006   Module::global_iterator I = Mod->global_end();
1007   if (I == Mod->global_begin())
1008     return 0;
1009   return wrap(--I);
1010 }
1011
1012 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1013   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1014   Module::global_iterator I = GV;
1015   if (++I == GV->getParent()->global_end())
1016     return 0;
1017   return wrap(I);
1018 }
1019
1020 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1021   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1022   Module::global_iterator I = GV;
1023   if (I == GV->getParent()->global_begin())
1024     return 0;
1025   return wrap(--I);
1026 }
1027
1028 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1029   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1030 }
1031
1032 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1033   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
1034 }
1035
1036 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1037   unwrap<GlobalVariable>(GlobalVar)
1038     ->setInitializer(unwrap<Constant>(ConstantVal));
1039 }
1040
1041 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1042   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1043 }
1044
1045 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
1046   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1047 }
1048
1049 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1050   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1051 }
1052
1053 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
1054   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1055 }
1056
1057 /*--.. Operations on aliases ......................................--*/
1058
1059 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1060                           const char *Name) {
1061   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1062                               unwrap<Constant>(Aliasee), unwrap (M)));
1063 }
1064
1065 /*--.. Operations on functions .............................................--*/
1066
1067 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1068                              LLVMTypeRef FunctionTy) {
1069   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1070                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1071 }
1072
1073 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1074   return wrap(unwrap(M)->getFunction(Name));
1075 }
1076
1077 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1078   Module *Mod = unwrap(M);
1079   Module::iterator I = Mod->begin();
1080   if (I == Mod->end())
1081     return 0;
1082   return wrap(I);
1083 }
1084
1085 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1086   Module *Mod = unwrap(M);
1087   Module::iterator I = Mod->end();
1088   if (I == Mod->begin())
1089     return 0;
1090   return wrap(--I);
1091 }
1092
1093 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1094   Function *Func = unwrap<Function>(Fn);
1095   Module::iterator I = Func;
1096   if (++I == Func->getParent()->end())
1097     return 0;
1098   return wrap(I);
1099 }
1100
1101 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1102   Function *Func = unwrap<Function>(Fn);
1103   Module::iterator I = Func;
1104   if (I == Func->getParent()->begin())
1105     return 0;
1106   return wrap(--I);
1107 }
1108
1109 void LLVMDeleteFunction(LLVMValueRef Fn) {
1110   unwrap<Function>(Fn)->eraseFromParent();
1111 }
1112
1113 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1114   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1115     return F->getIntrinsicID();
1116   return 0;
1117 }
1118
1119 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1120   return unwrap<Function>(Fn)->getCallingConv();
1121 }
1122
1123 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1124   return unwrap<Function>(Fn)->setCallingConv(CC);
1125 }
1126
1127 const char *LLVMGetGC(LLVMValueRef Fn) {
1128   Function *F = unwrap<Function>(Fn);
1129   return F->hasGC()? F->getGC() : 0;
1130 }
1131
1132 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1133   Function *F = unwrap<Function>(Fn);
1134   if (GC)
1135     F->setGC(GC);
1136   else
1137     F->clearGC();
1138 }
1139
1140 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1141   Function *Func = unwrap<Function>(Fn);
1142   const AttrListPtr PAL = Func->getAttributes();
1143   const AttrListPtr PALnew = PAL.addAttr(0, PA);
1144   Func->setAttributes(PALnew);
1145 }
1146
1147 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1148   Function *Func = unwrap<Function>(Fn);
1149   const AttrListPtr PAL = Func->getAttributes();
1150   const AttrListPtr PALnew = PAL.removeAttr(0, PA);
1151   Func->setAttributes(PALnew);
1152 }
1153
1154 /*--.. Operations on parameters ............................................--*/
1155
1156 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1157   // This function is strictly redundant to
1158   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1159   return unwrap<Function>(FnRef)->arg_size();
1160 }
1161
1162 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1163   Function *Fn = unwrap<Function>(FnRef);
1164   for (Function::arg_iterator I = Fn->arg_begin(),
1165                               E = Fn->arg_end(); I != E; I++)
1166     *ParamRefs++ = wrap(I);
1167 }
1168
1169 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1170   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1171   while (index --> 0)
1172     AI++;
1173   return wrap(AI);
1174 }
1175
1176 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1177   return wrap(unwrap<Argument>(V)->getParent());
1178 }
1179
1180 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1181   Function *Func = unwrap<Function>(Fn);
1182   Function::arg_iterator I = Func->arg_begin();
1183   if (I == Func->arg_end())
1184     return 0;
1185   return wrap(I);
1186 }
1187
1188 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1189   Function *Func = unwrap<Function>(Fn);
1190   Function::arg_iterator I = Func->arg_end();
1191   if (I == Func->arg_begin())
1192     return 0;
1193   return wrap(--I);
1194 }
1195
1196 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1197   Argument *A = unwrap<Argument>(Arg);
1198   Function::arg_iterator I = A;
1199   if (++I == A->getParent()->arg_end())
1200     return 0;
1201   return wrap(I);
1202 }
1203
1204 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1205   Argument *A = unwrap<Argument>(Arg);
1206   Function::arg_iterator I = A;
1207   if (I == A->getParent()->arg_begin())
1208     return 0;
1209   return wrap(--I);
1210 }
1211
1212 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1213   unwrap<Argument>(Arg)->addAttr(PA);
1214 }
1215
1216 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1217   unwrap<Argument>(Arg)->removeAttr(PA);
1218 }
1219
1220 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1221   unwrap<Argument>(Arg)->addAttr(
1222           Attribute::constructAlignmentFromInt(align));
1223 }
1224
1225 /*--.. Operations on basic blocks ..........................................--*/
1226
1227 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1228   return wrap(static_cast<Value*>(unwrap(BB)));
1229 }
1230
1231 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
1232   return isa<BasicBlock>(unwrap(Val));
1233 }
1234
1235 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1236   return wrap(unwrap<BasicBlock>(Val));
1237 }
1238
1239 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1240   return wrap(unwrap(BB)->getParent());
1241 }
1242
1243 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1244   return unwrap<Function>(FnRef)->size();
1245 }
1246
1247 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1248   Function *Fn = unwrap<Function>(FnRef);
1249   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1250     *BasicBlocksRefs++ = wrap(I);
1251 }
1252
1253 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1254   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1255 }
1256
1257 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1258   Function *Func = unwrap<Function>(Fn);
1259   Function::iterator I = Func->begin();
1260   if (I == Func->end())
1261     return 0;
1262   return wrap(I);
1263 }
1264
1265 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1266   Function *Func = unwrap<Function>(Fn);
1267   Function::iterator I = Func->end();
1268   if (I == Func->begin())
1269     return 0;
1270   return wrap(--I);
1271 }
1272
1273 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1274   BasicBlock *Block = unwrap(BB);
1275   Function::iterator I = Block;
1276   if (++I == Block->getParent()->end())
1277     return 0;
1278   return wrap(I);
1279 }
1280
1281 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1282   BasicBlock *Block = unwrap(BB);
1283   Function::iterator I = Block;
1284   if (I == Block->getParent()->begin())
1285     return 0;
1286   return wrap(--I);
1287 }
1288
1289 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1290                                                 LLVMValueRef FnRef,
1291                                                 const char *Name) {
1292   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1293 }
1294
1295 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1296   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1297 }
1298
1299 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1300                                                 LLVMBasicBlockRef BBRef,
1301                                                 const char *Name) {
1302   BasicBlock *BB = unwrap(BBRef);
1303   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1304 }
1305
1306 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1307                                        const char *Name) {
1308   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1309 }
1310
1311 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1312   unwrap(BBRef)->eraseFromParent();
1313 }
1314
1315 /*--.. Operations on instructions ..........................................--*/
1316
1317 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1318   return wrap(unwrap<Instruction>(Inst)->getParent());
1319 }
1320
1321 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1322   BasicBlock *Block = unwrap(BB);
1323   BasicBlock::iterator I = Block->begin();
1324   if (I == Block->end())
1325     return 0;
1326   return wrap(I);
1327 }
1328
1329 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1330   BasicBlock *Block = unwrap(BB);
1331   BasicBlock::iterator I = Block->end();
1332   if (I == Block->begin())
1333     return 0;
1334   return wrap(--I);
1335 }
1336
1337 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1338   Instruction *Instr = unwrap<Instruction>(Inst);
1339   BasicBlock::iterator I = Instr;
1340   if (++I == Instr->getParent()->end())
1341     return 0;
1342   return wrap(I);
1343 }
1344
1345 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1346   Instruction *Instr = unwrap<Instruction>(Inst);
1347   BasicBlock::iterator I = Instr;
1348   if (I == Instr->getParent()->begin())
1349     return 0;
1350   return wrap(--I);
1351 }
1352
1353 /*--.. Call and invoke instructions ........................................--*/
1354
1355 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1356   Value *V = unwrap(Instr);
1357   if (CallInst *CI = dyn_cast<CallInst>(V))
1358     return CI->getCallingConv();
1359   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1360     return II->getCallingConv();
1361   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1362   return 0;
1363 }
1364
1365 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1366   Value *V = unwrap(Instr);
1367   if (CallInst *CI = dyn_cast<CallInst>(V))
1368     return CI->setCallingConv(CC);
1369   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1370     return II->setCallingConv(CC);
1371   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1372 }
1373
1374 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1375                            LLVMAttribute PA) {
1376   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1377   Call.setAttributes(
1378     Call.getAttributes().addAttr(index, PA));
1379 }
1380
1381 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1382                               LLVMAttribute PA) {
1383   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1384   Call.setAttributes(
1385     Call.getAttributes().removeAttr(index, PA));
1386 }
1387
1388 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1389                                 unsigned align) {
1390   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1391   Call.setAttributes(
1392     Call.getAttributes().addAttr(index, 
1393         Attribute::constructAlignmentFromInt(align)));
1394 }
1395
1396 /*--.. Operations on call instructions (only) ..............................--*/
1397
1398 int LLVMIsTailCall(LLVMValueRef Call) {
1399   return unwrap<CallInst>(Call)->isTailCall();
1400 }
1401
1402 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1403   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1404 }
1405
1406 /*--.. Operations on phi nodes .............................................--*/
1407
1408 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1409                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1410   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1411   for (unsigned I = 0; I != Count; ++I)
1412     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1413 }
1414
1415 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1416   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1417 }
1418
1419 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1420   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1421 }
1422
1423 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1424   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1425 }
1426
1427
1428 /*===-- Instruction builders ----------------------------------------------===*/
1429
1430 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1431   return wrap(new IRBuilder<>(*unwrap(C)));
1432 }
1433
1434 LLVMBuilderRef LLVMCreateBuilder(void) {
1435   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1436 }
1437
1438 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1439                          LLVMValueRef Instr) {
1440   BasicBlock *BB = unwrap(Block);
1441   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1442   unwrap(Builder)->SetInsertPoint(BB, I);
1443 }
1444
1445 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1446   Instruction *I = unwrap<Instruction>(Instr);
1447   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1448 }
1449
1450 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1451   BasicBlock *BB = unwrap(Block);
1452   unwrap(Builder)->SetInsertPoint(BB);
1453 }
1454
1455 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1456    return wrap(unwrap(Builder)->GetInsertBlock());
1457 }
1458
1459 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1460   unwrap(Builder)->ClearInsertionPoint ();
1461 }
1462
1463 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1464   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1465 }
1466
1467 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1468                                    const char *Name) {
1469   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1470 }
1471
1472 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1473   delete unwrap(Builder);
1474 }
1475
1476 /*--.. Instruction builders ................................................--*/
1477
1478 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1479   return wrap(unwrap(B)->CreateRetVoid());
1480 }
1481
1482 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1483   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1484 }
1485
1486 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1487                                    unsigned N) {
1488   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1489 }
1490
1491 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1492   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1493 }
1494
1495 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1496                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1497   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1498 }
1499
1500 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1501                              LLVMBasicBlockRef Else, unsigned NumCases) {
1502   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1503 }
1504
1505 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1506                              LLVMValueRef *Args, unsigned NumArgs,
1507                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1508                              const char *Name) {
1509   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1510                                       unwrap(Args), unwrap(Args) + NumArgs,
1511                                       Name));
1512 }
1513
1514 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1515   return wrap(unwrap(B)->CreateUnwind());
1516 }
1517
1518 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1519   return wrap(unwrap(B)->CreateUnreachable());
1520 }
1521
1522 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1523                  LLVMBasicBlockRef Dest) {
1524   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1525 }
1526
1527 /*--.. Arithmetic ..........................................................--*/
1528
1529 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1530                           const char *Name) {
1531   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1532 }
1533
1534 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1535                           const char *Name) {
1536   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1537 }
1538
1539 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1540                           const char *Name) {
1541   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1542 }
1543
1544 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1545                           const char *Name) {
1546   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1547 }
1548
1549 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1550                           const char *Name) {
1551   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1552 }
1553
1554 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1555                           const char *Name) {
1556   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1557 }
1558
1559 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1560                           const char *Name) {
1561   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1562 }
1563
1564 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1565                            const char *Name) {
1566   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1567 }
1568
1569 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1570                            const char *Name) {
1571   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1572 }
1573
1574 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1575                                 LLVMValueRef RHS, const char *Name) {
1576   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1577 }
1578
1579 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1580                            const char *Name) {
1581   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1582 }
1583
1584 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1585                            const char *Name) {
1586   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1587 }
1588
1589 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1590                            const char *Name) {
1591   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1592 }
1593
1594 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1595                            const char *Name) {
1596   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1597 }
1598
1599 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1600                           const char *Name) {
1601   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1602 }
1603
1604 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1605                            const char *Name) {
1606   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1607 }
1608
1609 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1610                            const char *Name) {
1611   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1612 }
1613
1614 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1615                           const char *Name) {
1616   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1617 }
1618
1619 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1620                          const char *Name) {
1621   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1622 }
1623
1624 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1625                           const char *Name) {
1626   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1627 }
1628
1629 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1630   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1631 }
1632
1633 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1634   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1635 }
1636
1637 /*--.. Memory ..............................................................--*/
1638
1639 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1640                              const char *Name) {
1641   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1642 }
1643
1644 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1645                                   LLVMValueRef Val, const char *Name) {
1646   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1647 }
1648
1649 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1650                              const char *Name) {
1651   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1652 }
1653
1654 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1655                                   LLVMValueRef Val, const char *Name) {
1656   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1657 }
1658
1659 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1660   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1661 }
1662
1663
1664 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1665                            const char *Name) {
1666   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1667 }
1668
1669 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1670                             LLVMValueRef PointerVal) {
1671   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1672 }
1673
1674 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1675                           LLVMValueRef *Indices, unsigned NumIndices,
1676                           const char *Name) {
1677   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1678                                    unwrap(Indices) + NumIndices, Name));
1679 }
1680
1681 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1682                                   LLVMValueRef *Indices, unsigned NumIndices,
1683                                   const char *Name) {
1684   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1685                                            unwrap(Indices) + NumIndices, Name));
1686 }
1687
1688 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1689                                 unsigned Idx, const char *Name) {
1690   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1691 }
1692
1693 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1694                                    const char *Name) {
1695   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1696 }
1697
1698 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1699                                       const char *Name) {
1700   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1701 }
1702
1703 /*--.. Casts ...............................................................--*/
1704
1705 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1706                             LLVMTypeRef DestTy, const char *Name) {
1707   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1708 }
1709
1710 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1711                            LLVMTypeRef DestTy, const char *Name) {
1712   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1713 }
1714
1715 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1716                            LLVMTypeRef DestTy, const char *Name) {
1717   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1718 }
1719
1720 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1721                              LLVMTypeRef DestTy, const char *Name) {
1722   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1723 }
1724
1725 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1726                              LLVMTypeRef DestTy, const char *Name) {
1727   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1728 }
1729
1730 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1731                              LLVMTypeRef DestTy, const char *Name) {
1732   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1733 }
1734
1735 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1736                              LLVMTypeRef DestTy, const char *Name) {
1737   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1738 }
1739
1740 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1741                               LLVMTypeRef DestTy, const char *Name) {
1742   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1743 }
1744
1745 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1746                             LLVMTypeRef DestTy, const char *Name) {
1747   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1748 }
1749
1750 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1751                                LLVMTypeRef DestTy, const char *Name) {
1752   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1753 }
1754
1755 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1756                                LLVMTypeRef DestTy, const char *Name) {
1757   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1758 }
1759
1760 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1761                               LLVMTypeRef DestTy, const char *Name) {
1762   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1763 }
1764
1765 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1766                                     LLVMTypeRef DestTy, const char *Name) {
1767   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1768                                              Name));
1769 }
1770
1771 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1772                                     LLVMTypeRef DestTy, const char *Name) {
1773   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1774                                              Name));
1775 }
1776
1777 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1778                                      LLVMTypeRef DestTy, const char *Name) {
1779   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1780                                               Name));
1781 }
1782
1783 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1784                                   LLVMTypeRef DestTy, const char *Name) {
1785   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1786 }
1787
1788 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1789                               LLVMTypeRef DestTy, const char *Name) {
1790   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), Name));
1791 }
1792
1793 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1794                              LLVMTypeRef DestTy, const char *Name) {
1795   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
1796 }
1797
1798 /*--.. Comparisons .........................................................--*/
1799
1800 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1801                            LLVMValueRef LHS, LLVMValueRef RHS,
1802                            const char *Name) {
1803   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1804                                     unwrap(LHS), unwrap(RHS), Name));
1805 }
1806
1807 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1808                            LLVMValueRef LHS, LLVMValueRef RHS,
1809                            const char *Name) {
1810   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1811                                     unwrap(LHS), unwrap(RHS), Name));
1812 }
1813
1814 /*--.. Miscellaneous instructions ..........................................--*/
1815
1816 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1817   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1818 }
1819
1820 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1821                            LLVMValueRef *Args, unsigned NumArgs,
1822                            const char *Name) {
1823   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1824                                     unwrap(Args) + NumArgs, Name));
1825 }
1826
1827 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1828                              LLVMValueRef Then, LLVMValueRef Else,
1829                              const char *Name) {
1830   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1831                                       Name));
1832 }
1833
1834 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1835                             LLVMTypeRef Ty, const char *Name) {
1836   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1837 }
1838
1839 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1840                                       LLVMValueRef Index, const char *Name) {
1841   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1842                                               Name));
1843 }
1844
1845 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1846                                     LLVMValueRef EltVal, LLVMValueRef Index,
1847                                     const char *Name) {
1848   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1849                                              unwrap(Index), Name));
1850 }
1851
1852 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1853                                     LLVMValueRef V2, LLVMValueRef Mask,
1854                                     const char *Name) {
1855   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1856                                              unwrap(Mask), Name));
1857 }
1858
1859 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1860                                    unsigned Index, const char *Name) {
1861   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1862 }
1863
1864 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1865                                   LLVMValueRef EltVal, unsigned Index,
1866                                   const char *Name) {
1867   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1868                                            Index, Name));
1869 }
1870
1871 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
1872                              const char *Name) {
1873   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
1874 }
1875
1876 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
1877                                 const char *Name) {
1878   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
1879 }
1880
1881 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
1882                               LLVMValueRef RHS, const char *Name) {
1883   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
1884 }
1885
1886
1887 /*===-- Module providers --------------------------------------------------===*/
1888
1889 LLVMModuleProviderRef
1890 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1891   return wrap(new ExistingModuleProvider(unwrap(M)));
1892 }
1893
1894 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1895   delete unwrap(MP);
1896 }
1897
1898
1899 /*===-- Memory buffers ----------------------------------------------------===*/
1900
1901 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1902                                              LLVMMemoryBufferRef *OutMemBuf,
1903                                              char **OutMessage) {
1904   std::string Error;
1905   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1906     *OutMemBuf = wrap(MB);
1907     return 0;
1908   }
1909   
1910   *OutMessage = strdup(Error.c_str());
1911   return 1;
1912 }
1913
1914 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1915                                     char **OutMessage) {
1916   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1917     *OutMemBuf = wrap(MB);
1918     return 0;
1919   }
1920   
1921   *OutMessage = strdup("stdin is empty.");
1922   return 1;
1923 }
1924
1925 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1926   delete unwrap(MemBuf);
1927 }