Auto-upgrade malloc instructions to malloc calls.
[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 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
444                                   uint8_t Radix) {
445   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
446                                Radix));
447 }
448
449 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
450                                          unsigned SLen, uint8_t Radix) {
451   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
452                                Radix));
453 }
454
455 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
456   return wrap(ConstantFP::get(unwrap(RealTy), N));
457 }
458
459 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
460   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
461 }
462
463 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
464                                           unsigned SLen) {
465   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
466 }
467
468 /*--.. Operations on composite constants ...................................--*/
469
470 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
471                                       unsigned Length, int DontNullTerminate) {
472   /* Inverted the sense of AddNull because ', 0)' is a
473      better mnemonic for null termination than ', 1)'. */
474   return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
475                                  DontNullTerminate == 0));
476 }
477 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
478                                       LLVMValueRef *ConstantVals,
479                                       unsigned Count, int Packed) {
480   return wrap(ConstantStruct::get(*unwrap(C),
481                                   unwrap<Constant>(ConstantVals, Count),
482                                   Count, Packed != 0));
483 }
484
485 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
486                              int DontNullTerminate) {
487   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
488                                   DontNullTerminate);
489 }
490 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
491                             LLVMValueRef *ConstantVals, unsigned Length) {
492   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
493                                  unwrap<Constant>(ConstantVals, Length),
494                                  Length));
495 }
496 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
497                              int Packed) {
498   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
499                                   Packed);
500 }
501
502 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
503   return wrap(ConstantVector::get(
504                             unwrap<Constant>(ScalarConstantVals, Size), Size));
505 }
506
507 /*--.. Constant expressions ................................................--*/
508
509 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
510   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
511 }
512
513 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
514   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
515 }
516
517 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
518   return wrap(ConstantExpr::getNeg(
519                                    unwrap<Constant>(ConstantVal)));
520 }
521
522 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
523   return wrap(ConstantExpr::getFNeg(
524                                     unwrap<Constant>(ConstantVal)));
525 }
526
527 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
528   return wrap(ConstantExpr::getNot(
529                                    unwrap<Constant>(ConstantVal)));
530 }
531
532 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
533   return wrap(ConstantExpr::getAdd(
534                                    unwrap<Constant>(LHSConstant),
535                                    unwrap<Constant>(RHSConstant)));
536 }
537
538 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
539                              LLVMValueRef RHSConstant) {
540   return wrap(ConstantExpr::getNSWAdd(
541                                       unwrap<Constant>(LHSConstant),
542                                       unwrap<Constant>(RHSConstant)));
543 }
544
545 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
546   return wrap(ConstantExpr::getFAdd(
547                                     unwrap<Constant>(LHSConstant),
548                                     unwrap<Constant>(RHSConstant)));
549 }
550
551 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
552   return wrap(ConstantExpr::getSub(
553                                    unwrap<Constant>(LHSConstant),
554                                    unwrap<Constant>(RHSConstant)));
555 }
556
557 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
558   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
559                                     unwrap<Constant>(RHSConstant)));
560 }
561
562 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
563   return wrap(ConstantExpr::getMul(
564                                    unwrap<Constant>(LHSConstant),
565                                    unwrap<Constant>(RHSConstant)));
566 }
567
568 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
569   return wrap(ConstantExpr::getFMul(
570                                     unwrap<Constant>(LHSConstant),
571                                     unwrap<Constant>(RHSConstant)));
572 }
573
574 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
575   return wrap(ConstantExpr::getUDiv(
576                                     unwrap<Constant>(LHSConstant),
577                                     unwrap<Constant>(RHSConstant)));
578 }
579
580 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
581   return wrap(ConstantExpr::getSDiv(
582                                     unwrap<Constant>(LHSConstant),
583                                     unwrap<Constant>(RHSConstant)));
584 }
585
586 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
587                                 LLVMValueRef RHSConstant) {
588   return wrap(ConstantExpr::getExactSDiv(
589                                          unwrap<Constant>(LHSConstant),
590                                          unwrap<Constant>(RHSConstant)));
591 }
592
593 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
594   return wrap(ConstantExpr::getFDiv(
595                                     unwrap<Constant>(LHSConstant),
596                                     unwrap<Constant>(RHSConstant)));
597 }
598
599 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
600   return wrap(ConstantExpr::getURem(
601                                     unwrap<Constant>(LHSConstant),
602                                     unwrap<Constant>(RHSConstant)));
603 }
604
605 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
606   return wrap(ConstantExpr::getSRem(
607                                     unwrap<Constant>(LHSConstant),
608                                     unwrap<Constant>(RHSConstant)));
609 }
610
611 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
612   return wrap(ConstantExpr::getFRem(
613                                     unwrap<Constant>(LHSConstant),
614                                     unwrap<Constant>(RHSConstant)));
615 }
616
617 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
618   return wrap(ConstantExpr::getAnd(
619                                    unwrap<Constant>(LHSConstant),
620                                    unwrap<Constant>(RHSConstant)));
621 }
622
623 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
624   return wrap(ConstantExpr::getOr(
625                                   unwrap<Constant>(LHSConstant),
626                                   unwrap<Constant>(RHSConstant)));
627 }
628
629 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
630   return wrap(ConstantExpr::getXor(
631                                    unwrap<Constant>(LHSConstant),
632                                    unwrap<Constant>(RHSConstant)));
633 }
634
635 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
636                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
637   return wrap(ConstantExpr::getICmp(Predicate,
638                                     unwrap<Constant>(LHSConstant),
639                                     unwrap<Constant>(RHSConstant)));
640 }
641
642 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
643                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
644   return wrap(ConstantExpr::getFCmp(Predicate,
645                                     unwrap<Constant>(LHSConstant),
646                                     unwrap<Constant>(RHSConstant)));
647 }
648
649 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
650   return wrap(ConstantExpr::getShl(
651                                   unwrap<Constant>(LHSConstant),
652                                   unwrap<Constant>(RHSConstant)));
653 }
654
655 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
656   return wrap(ConstantExpr::getLShr(
657                                     unwrap<Constant>(LHSConstant),
658                                     unwrap<Constant>(RHSConstant)));
659 }
660
661 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
662   return wrap(ConstantExpr::getAShr(
663                                     unwrap<Constant>(LHSConstant),
664                                     unwrap<Constant>(RHSConstant)));
665 }
666
667 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
668                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
669   return wrap(ConstantExpr::getGetElementPtr(
670                                              unwrap<Constant>(ConstantVal),
671                                              unwrap<Constant>(ConstantIndices, 
672                                                               NumIndices),
673                                              NumIndices));
674 }
675
676 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
677                                   LLVMValueRef *ConstantIndices,
678                                   unsigned NumIndices) {
679   Constant* Val = unwrap<Constant>(ConstantVal);
680   Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
681   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
682 }
683
684 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
685   return wrap(ConstantExpr::getTrunc(
686                                      unwrap<Constant>(ConstantVal),
687                                      unwrap(ToType)));
688 }
689
690 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
691   return wrap(ConstantExpr::getSExt(
692                                     unwrap<Constant>(ConstantVal),
693                                     unwrap(ToType)));
694 }
695
696 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
697   return wrap(ConstantExpr::getZExt(
698                                     unwrap<Constant>(ConstantVal),
699                                     unwrap(ToType)));
700 }
701
702 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
703   return wrap(ConstantExpr::getFPTrunc(
704                                        unwrap<Constant>(ConstantVal),
705                                        unwrap(ToType)));
706 }
707
708 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
709   return wrap(ConstantExpr::getFPExtend(
710                                         unwrap<Constant>(ConstantVal),
711                                         unwrap(ToType)));
712 }
713
714 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
715   return wrap(ConstantExpr::getUIToFP(
716                                       unwrap<Constant>(ConstantVal),
717                                       unwrap(ToType)));
718 }
719
720 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
721   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
722                                       unwrap(ToType)));
723 }
724
725 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
726   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
727                                       unwrap(ToType)));
728 }
729
730 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
731   return wrap(ConstantExpr::getFPToSI(
732                                       unwrap<Constant>(ConstantVal),
733                                       unwrap(ToType)));
734 }
735
736 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
737   return wrap(ConstantExpr::getPtrToInt(
738                                         unwrap<Constant>(ConstantVal),
739                                         unwrap(ToType)));
740 }
741
742 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
743   return wrap(ConstantExpr::getIntToPtr(
744                                         unwrap<Constant>(ConstantVal),
745                                         unwrap(ToType)));
746 }
747
748 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
749   return wrap(ConstantExpr::getBitCast(
750                                        unwrap<Constant>(ConstantVal),
751                                        unwrap(ToType)));
752 }
753
754 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
755                                     LLVMTypeRef ToType) {
756   return wrap(ConstantExpr::getZExtOrBitCast(
757                                              unwrap<Constant>(ConstantVal),
758                                              unwrap(ToType)));
759 }
760
761 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
762                                     LLVMTypeRef ToType) {
763   return wrap(ConstantExpr::getSExtOrBitCast(
764                                              unwrap<Constant>(ConstantVal),
765                                              unwrap(ToType)));
766 }
767
768 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
769                                      LLVMTypeRef ToType) {
770   return wrap(ConstantExpr::getTruncOrBitCast(
771                                               unwrap<Constant>(ConstantVal),
772                                               unwrap(ToType)));
773 }
774
775 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
776                                   LLVMTypeRef ToType) {
777   return wrap(ConstantExpr::getPointerCast(
778                                            unwrap<Constant>(ConstantVal),
779                                            unwrap(ToType)));
780 }
781
782 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
783                               unsigned isSigned) {
784   return wrap(ConstantExpr::getIntegerCast(
785                                            unwrap<Constant>(ConstantVal),
786                                            unwrap(ToType),
787                                            isSigned));
788 }
789
790 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
791   return wrap(ConstantExpr::getFPCast(
792                                       unwrap<Constant>(ConstantVal),
793                                       unwrap(ToType)));
794 }
795
796 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
797                              LLVMValueRef ConstantIfTrue,
798                              LLVMValueRef ConstantIfFalse) {
799   return wrap(ConstantExpr::getSelect(
800                                       unwrap<Constant>(ConstantCondition),
801                                       unwrap<Constant>(ConstantIfTrue),
802                                       unwrap<Constant>(ConstantIfFalse)));
803 }
804
805 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
806                                      LLVMValueRef IndexConstant) {
807   return wrap(ConstantExpr::getExtractElement(
808                                               unwrap<Constant>(VectorConstant),
809                                               unwrap<Constant>(IndexConstant)));
810 }
811
812 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
813                                     LLVMValueRef ElementValueConstant,
814                                     LLVMValueRef IndexConstant) {
815   return wrap(ConstantExpr::getInsertElement(
816                                          unwrap<Constant>(VectorConstant),
817                                          unwrap<Constant>(ElementValueConstant),
818                                              unwrap<Constant>(IndexConstant)));
819 }
820
821 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
822                                     LLVMValueRef VectorBConstant,
823                                     LLVMValueRef MaskConstant) {
824   return wrap(ConstantExpr::getShuffleVector(
825                                              unwrap<Constant>(VectorAConstant),
826                                              unwrap<Constant>(VectorBConstant),
827                                              unwrap<Constant>(MaskConstant)));
828 }
829
830 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
831                                    unsigned NumIdx) {
832   return wrap(ConstantExpr::getExtractValue(
833                                             unwrap<Constant>(AggConstant),
834                                             IdxList, NumIdx));
835 }
836
837 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
838                                   LLVMValueRef ElementValueConstant,
839                                   unsigned *IdxList, unsigned NumIdx) {
840   return wrap(ConstantExpr::getInsertValue(
841                                          unwrap<Constant>(AggConstant),
842                                          unwrap<Constant>(ElementValueConstant),
843                                            IdxList, NumIdx));
844 }
845
846 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
847                                 const char *Constraints, int HasSideEffects) {
848   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
849                              Constraints, HasSideEffects));
850 }
851
852 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
853
854 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
855   return wrap(unwrap<GlobalValue>(Global)->getParent());
856 }
857
858 int LLVMIsDeclaration(LLVMValueRef Global) {
859   return unwrap<GlobalValue>(Global)->isDeclaration();
860 }
861
862 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
863   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
864   default:
865     assert(false && "Unhandled Linkage Type.");
866   case GlobalValue::ExternalLinkage:
867     return LLVMExternalLinkage;
868   case GlobalValue::AvailableExternallyLinkage:
869     return LLVMAvailableExternallyLinkage;
870   case GlobalValue::LinkOnceAnyLinkage:
871     return LLVMLinkOnceAnyLinkage;
872   case GlobalValue::LinkOnceODRLinkage:
873     return LLVMLinkOnceODRLinkage;
874   case GlobalValue::WeakAnyLinkage:
875     return LLVMWeakAnyLinkage;
876   case GlobalValue::WeakODRLinkage:
877     return LLVMWeakODRLinkage;
878   case GlobalValue::AppendingLinkage:
879     return LLVMAppendingLinkage;
880   case GlobalValue::InternalLinkage:
881     return LLVMInternalLinkage;
882   case GlobalValue::PrivateLinkage:
883     return LLVMPrivateLinkage;
884   case GlobalValue::LinkerPrivateLinkage:
885     return LLVMLinkerPrivateLinkage;
886   case GlobalValue::DLLImportLinkage:
887     return LLVMDLLImportLinkage;
888   case GlobalValue::DLLExportLinkage:
889     return LLVMDLLExportLinkage;
890   case GlobalValue::ExternalWeakLinkage:
891     return LLVMExternalWeakLinkage;
892   case GlobalValue::GhostLinkage:
893     return LLVMGhostLinkage;
894   case GlobalValue::CommonLinkage:
895     return LLVMCommonLinkage;
896   }
897
898   // Should never get here.
899   return static_cast<LLVMLinkage>(0);
900 }
901
902 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
903   GlobalValue *GV = unwrap<GlobalValue>(Global);
904
905   switch (Linkage) {
906   default:
907     assert(false && "Unhandled Linkage Type.");
908   case LLVMExternalLinkage:
909     GV->setLinkage(GlobalValue::ExternalLinkage);
910     break;
911   case LLVMAvailableExternallyLinkage:
912     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
913     break;
914   case LLVMLinkOnceAnyLinkage:
915     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
916     break;
917   case LLVMLinkOnceODRLinkage:
918     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
919     break;
920   case LLVMWeakAnyLinkage:
921     GV->setLinkage(GlobalValue::WeakAnyLinkage);
922     break;
923   case LLVMWeakODRLinkage:
924     GV->setLinkage(GlobalValue::WeakODRLinkage);
925     break;
926   case LLVMAppendingLinkage:
927     GV->setLinkage(GlobalValue::AppendingLinkage);
928     break;
929   case LLVMInternalLinkage:
930     GV->setLinkage(GlobalValue::InternalLinkage);
931     break;
932   case LLVMPrivateLinkage:
933     GV->setLinkage(GlobalValue::PrivateLinkage);
934     break;
935   case LLVMLinkerPrivateLinkage:
936     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
937     break;
938   case LLVMDLLImportLinkage:
939     GV->setLinkage(GlobalValue::DLLImportLinkage);
940     break;
941   case LLVMDLLExportLinkage:
942     GV->setLinkage(GlobalValue::DLLExportLinkage);
943     break;
944   case LLVMExternalWeakLinkage:
945     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
946     break;
947   case LLVMGhostLinkage:
948     GV->setLinkage(GlobalValue::GhostLinkage);
949     break;
950   case LLVMCommonLinkage:
951     GV->setLinkage(GlobalValue::CommonLinkage);
952     break;
953   }
954 }
955
956 const char *LLVMGetSection(LLVMValueRef Global) {
957   return unwrap<GlobalValue>(Global)->getSection().c_str();
958 }
959
960 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
961   unwrap<GlobalValue>(Global)->setSection(Section);
962 }
963
964 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
965   return static_cast<LLVMVisibility>(
966     unwrap<GlobalValue>(Global)->getVisibility());
967 }
968
969 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
970   unwrap<GlobalValue>(Global)
971     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
972 }
973
974 unsigned LLVMGetAlignment(LLVMValueRef Global) {
975   return unwrap<GlobalValue>(Global)->getAlignment();
976 }
977
978 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
979   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
980 }
981
982 /*--.. Operations on global variables ......................................--*/
983
984 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
985   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
986                                  GlobalValue::ExternalLinkage, 0, Name));
987 }
988
989 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
990   return wrap(unwrap(M)->getNamedGlobal(Name));
991 }
992
993 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
994   Module *Mod = unwrap(M);
995   Module::global_iterator I = Mod->global_begin();
996   if (I == Mod->global_end())
997     return 0;
998   return wrap(I);
999 }
1000
1001 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1002   Module *Mod = unwrap(M);
1003   Module::global_iterator I = Mod->global_end();
1004   if (I == Mod->global_begin())
1005     return 0;
1006   return wrap(--I);
1007 }
1008
1009 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1010   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1011   Module::global_iterator I = GV;
1012   if (++I == GV->getParent()->global_end())
1013     return 0;
1014   return wrap(I);
1015 }
1016
1017 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1018   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1019   Module::global_iterator I = GV;
1020   if (I == GV->getParent()->global_begin())
1021     return 0;
1022   return wrap(--I);
1023 }
1024
1025 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1026   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1027 }
1028
1029 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1030   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
1031 }
1032
1033 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1034   unwrap<GlobalVariable>(GlobalVar)
1035     ->setInitializer(unwrap<Constant>(ConstantVal));
1036 }
1037
1038 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1039   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1040 }
1041
1042 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
1043   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1044 }
1045
1046 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1047   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1048 }
1049
1050 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
1051   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1052 }
1053
1054 /*--.. Operations on aliases ......................................--*/
1055
1056 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1057                           const char *Name) {
1058   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1059                               unwrap<Constant>(Aliasee), unwrap (M)));
1060 }
1061
1062 /*--.. Operations on functions .............................................--*/
1063
1064 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1065                              LLVMTypeRef FunctionTy) {
1066   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1067                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1068 }
1069
1070 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1071   return wrap(unwrap(M)->getFunction(Name));
1072 }
1073
1074 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1075   Module *Mod = unwrap(M);
1076   Module::iterator I = Mod->begin();
1077   if (I == Mod->end())
1078     return 0;
1079   return wrap(I);
1080 }
1081
1082 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1083   Module *Mod = unwrap(M);
1084   Module::iterator I = Mod->end();
1085   if (I == Mod->begin())
1086     return 0;
1087   return wrap(--I);
1088 }
1089
1090 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1091   Function *Func = unwrap<Function>(Fn);
1092   Module::iterator I = Func;
1093   if (++I == Func->getParent()->end())
1094     return 0;
1095   return wrap(I);
1096 }
1097
1098 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1099   Function *Func = unwrap<Function>(Fn);
1100   Module::iterator I = Func;
1101   if (I == Func->getParent()->begin())
1102     return 0;
1103   return wrap(--I);
1104 }
1105
1106 void LLVMDeleteFunction(LLVMValueRef Fn) {
1107   unwrap<Function>(Fn)->eraseFromParent();
1108 }
1109
1110 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1111   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1112     return F->getIntrinsicID();
1113   return 0;
1114 }
1115
1116 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1117   return unwrap<Function>(Fn)->getCallingConv();
1118 }
1119
1120 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1121   return unwrap<Function>(Fn)->setCallingConv(
1122     static_cast<CallingConv::ID>(CC));
1123 }
1124
1125 const char *LLVMGetGC(LLVMValueRef Fn) {
1126   Function *F = unwrap<Function>(Fn);
1127   return F->hasGC()? F->getGC() : 0;
1128 }
1129
1130 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1131   Function *F = unwrap<Function>(Fn);
1132   if (GC)
1133     F->setGC(GC);
1134   else
1135     F->clearGC();
1136 }
1137
1138 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1139   Function *Func = unwrap<Function>(Fn);
1140   const AttrListPtr PAL = Func->getAttributes();
1141   const AttrListPtr PALnew = PAL.addAttr(0, PA);
1142   Func->setAttributes(PALnew);
1143 }
1144
1145 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1146   Function *Func = unwrap<Function>(Fn);
1147   const AttrListPtr PAL = Func->getAttributes();
1148   const AttrListPtr PALnew = PAL.removeAttr(0, PA);
1149   Func->setAttributes(PALnew);
1150 }
1151
1152 /*--.. Operations on parameters ............................................--*/
1153
1154 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1155   // This function is strictly redundant to
1156   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1157   return unwrap<Function>(FnRef)->arg_size();
1158 }
1159
1160 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1161   Function *Fn = unwrap<Function>(FnRef);
1162   for (Function::arg_iterator I = Fn->arg_begin(),
1163                               E = Fn->arg_end(); I != E; I++)
1164     *ParamRefs++ = wrap(I);
1165 }
1166
1167 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1168   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1169   while (index --> 0)
1170     AI++;
1171   return wrap(AI);
1172 }
1173
1174 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1175   return wrap(unwrap<Argument>(V)->getParent());
1176 }
1177
1178 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1179   Function *Func = unwrap<Function>(Fn);
1180   Function::arg_iterator I = Func->arg_begin();
1181   if (I == Func->arg_end())
1182     return 0;
1183   return wrap(I);
1184 }
1185
1186 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1187   Function *Func = unwrap<Function>(Fn);
1188   Function::arg_iterator I = Func->arg_end();
1189   if (I == Func->arg_begin())
1190     return 0;
1191   return wrap(--I);
1192 }
1193
1194 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1195   Argument *A = unwrap<Argument>(Arg);
1196   Function::arg_iterator I = A;
1197   if (++I == A->getParent()->arg_end())
1198     return 0;
1199   return wrap(I);
1200 }
1201
1202 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1203   Argument *A = unwrap<Argument>(Arg);
1204   Function::arg_iterator I = A;
1205   if (I == A->getParent()->arg_begin())
1206     return 0;
1207   return wrap(--I);
1208 }
1209
1210 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1211   unwrap<Argument>(Arg)->addAttr(PA);
1212 }
1213
1214 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1215   unwrap<Argument>(Arg)->removeAttr(PA);
1216 }
1217
1218 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1219   unwrap<Argument>(Arg)->addAttr(
1220           Attribute::constructAlignmentFromInt(align));
1221 }
1222
1223 /*--.. Operations on basic blocks ..........................................--*/
1224
1225 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1226   return wrap(static_cast<Value*>(unwrap(BB)));
1227 }
1228
1229 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
1230   return isa<BasicBlock>(unwrap(Val));
1231 }
1232
1233 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1234   return wrap(unwrap<BasicBlock>(Val));
1235 }
1236
1237 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1238   return wrap(unwrap(BB)->getParent());
1239 }
1240
1241 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1242   return unwrap<Function>(FnRef)->size();
1243 }
1244
1245 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1246   Function *Fn = unwrap<Function>(FnRef);
1247   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1248     *BasicBlocksRefs++ = wrap(I);
1249 }
1250
1251 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1252   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1253 }
1254
1255 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1256   Function *Func = unwrap<Function>(Fn);
1257   Function::iterator I = Func->begin();
1258   if (I == Func->end())
1259     return 0;
1260   return wrap(I);
1261 }
1262
1263 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1264   Function *Func = unwrap<Function>(Fn);
1265   Function::iterator I = Func->end();
1266   if (I == Func->begin())
1267     return 0;
1268   return wrap(--I);
1269 }
1270
1271 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1272   BasicBlock *Block = unwrap(BB);
1273   Function::iterator I = Block;
1274   if (++I == Block->getParent()->end())
1275     return 0;
1276   return wrap(I);
1277 }
1278
1279 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1280   BasicBlock *Block = unwrap(BB);
1281   Function::iterator I = Block;
1282   if (I == Block->getParent()->begin())
1283     return 0;
1284   return wrap(--I);
1285 }
1286
1287 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1288                                                 LLVMValueRef FnRef,
1289                                                 const char *Name) {
1290   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1291 }
1292
1293 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1294   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1295 }
1296
1297 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1298                                                 LLVMBasicBlockRef BBRef,
1299                                                 const char *Name) {
1300   BasicBlock *BB = unwrap(BBRef);
1301   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1302 }
1303
1304 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1305                                        const char *Name) {
1306   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1307 }
1308
1309 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1310   unwrap(BBRef)->eraseFromParent();
1311 }
1312
1313 /*--.. Operations on instructions ..........................................--*/
1314
1315 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1316   return wrap(unwrap<Instruction>(Inst)->getParent());
1317 }
1318
1319 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1320   BasicBlock *Block = unwrap(BB);
1321   BasicBlock::iterator I = Block->begin();
1322   if (I == Block->end())
1323     return 0;
1324   return wrap(I);
1325 }
1326
1327 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1328   BasicBlock *Block = unwrap(BB);
1329   BasicBlock::iterator I = Block->end();
1330   if (I == Block->begin())
1331     return 0;
1332   return wrap(--I);
1333 }
1334
1335 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1336   Instruction *Instr = unwrap<Instruction>(Inst);
1337   BasicBlock::iterator I = Instr;
1338   if (++I == Instr->getParent()->end())
1339     return 0;
1340   return wrap(I);
1341 }
1342
1343 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1344   Instruction *Instr = unwrap<Instruction>(Inst);
1345   BasicBlock::iterator I = Instr;
1346   if (I == Instr->getParent()->begin())
1347     return 0;
1348   return wrap(--I);
1349 }
1350
1351 /*--.. Call and invoke instructions ........................................--*/
1352
1353 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1354   Value *V = unwrap(Instr);
1355   if (CallInst *CI = dyn_cast<CallInst>(V))
1356     return CI->getCallingConv();
1357   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1358     return II->getCallingConv();
1359   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1360   return 0;
1361 }
1362
1363 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1364   Value *V = unwrap(Instr);
1365   if (CallInst *CI = dyn_cast<CallInst>(V))
1366     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1367   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1368     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1369   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1370 }
1371
1372 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1373                            LLVMAttribute PA) {
1374   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1375   Call.setAttributes(
1376     Call.getAttributes().addAttr(index, PA));
1377 }
1378
1379 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1380                               LLVMAttribute PA) {
1381   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1382   Call.setAttributes(
1383     Call.getAttributes().removeAttr(index, PA));
1384 }
1385
1386 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1387                                 unsigned align) {
1388   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1389   Call.setAttributes(
1390     Call.getAttributes().addAttr(index, 
1391         Attribute::constructAlignmentFromInt(align)));
1392 }
1393
1394 /*--.. Operations on call instructions (only) ..............................--*/
1395
1396 int LLVMIsTailCall(LLVMValueRef Call) {
1397   return unwrap<CallInst>(Call)->isTailCall();
1398 }
1399
1400 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1401   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1402 }
1403
1404 /*--.. Operations on phi nodes .............................................--*/
1405
1406 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1407                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1408   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1409   for (unsigned I = 0; I != Count; ++I)
1410     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1411 }
1412
1413 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1414   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1415 }
1416
1417 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1418   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1419 }
1420
1421 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1422   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1423 }
1424
1425
1426 /*===-- Instruction builders ----------------------------------------------===*/
1427
1428 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1429   return wrap(new IRBuilder<>(*unwrap(C)));
1430 }
1431
1432 LLVMBuilderRef LLVMCreateBuilder(void) {
1433   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1434 }
1435
1436 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1437                          LLVMValueRef Instr) {
1438   BasicBlock *BB = unwrap(Block);
1439   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1440   unwrap(Builder)->SetInsertPoint(BB, I);
1441 }
1442
1443 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1444   Instruction *I = unwrap<Instruction>(Instr);
1445   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1446 }
1447
1448 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1449   BasicBlock *BB = unwrap(Block);
1450   unwrap(Builder)->SetInsertPoint(BB);
1451 }
1452
1453 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1454    return wrap(unwrap(Builder)->GetInsertBlock());
1455 }
1456
1457 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1458   unwrap(Builder)->ClearInsertionPoint ();
1459 }
1460
1461 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1462   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1463 }
1464
1465 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1466                                    const char *Name) {
1467   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1468 }
1469
1470 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1471   delete unwrap(Builder);
1472 }
1473
1474 /*--.. Instruction builders ................................................--*/
1475
1476 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1477   return wrap(unwrap(B)->CreateRetVoid());
1478 }
1479
1480 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1481   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1482 }
1483
1484 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1485                                    unsigned N) {
1486   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1487 }
1488
1489 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1490   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1491 }
1492
1493 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1494                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1495   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1496 }
1497
1498 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1499                              LLVMBasicBlockRef Else, unsigned NumCases) {
1500   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1501 }
1502
1503 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1504                              LLVMValueRef *Args, unsigned NumArgs,
1505                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1506                              const char *Name) {
1507   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1508                                       unwrap(Args), unwrap(Args) + NumArgs,
1509                                       Name));
1510 }
1511
1512 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1513   return wrap(unwrap(B)->CreateUnwind());
1514 }
1515
1516 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1517   return wrap(unwrap(B)->CreateUnreachable());
1518 }
1519
1520 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1521                  LLVMBasicBlockRef Dest) {
1522   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1523 }
1524
1525 /*--.. Arithmetic ..........................................................--*/
1526
1527 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1528                           const char *Name) {
1529   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1530 }
1531
1532 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1533                           const char *Name) {
1534   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1535 }
1536
1537 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1538                           const char *Name) {
1539   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1540 }
1541
1542 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1543                           const char *Name) {
1544   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1545 }
1546
1547 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1548                           const char *Name) {
1549   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1550 }
1551
1552 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1553                           const char *Name) {
1554   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1555 }
1556
1557 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1558                           const char *Name) {
1559   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1560 }
1561
1562 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1563                            const char *Name) {
1564   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1565 }
1566
1567 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1568                            const char *Name) {
1569   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1570 }
1571
1572 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1573                                 LLVMValueRef RHS, const char *Name) {
1574   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1575 }
1576
1577 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1578                            const char *Name) {
1579   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1580 }
1581
1582 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1583                            const char *Name) {
1584   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1585 }
1586
1587 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1588                            const char *Name) {
1589   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1590 }
1591
1592 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1593                            const char *Name) {
1594   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1595 }
1596
1597 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1598                           const char *Name) {
1599   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1600 }
1601
1602 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1603                            const char *Name) {
1604   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1605 }
1606
1607 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1608                            const char *Name) {
1609   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1610 }
1611
1612 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1613                           const char *Name) {
1614   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1615 }
1616
1617 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1618                          const char *Name) {
1619   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1620 }
1621
1622 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1623                           const char *Name) {
1624   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1625 }
1626
1627 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1628   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1629 }
1630
1631 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1632   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1633 }
1634
1635 /*--.. Memory ..............................................................--*/
1636
1637 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1638                              const char *Name) {
1639   const Type* IntPtrT = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1640   return wrap(CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), IntPtrT,
1641                                      unwrap(Ty), 0, 0, Twine(Name)));
1642 }
1643
1644 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1645                                   LLVMValueRef Val, const char *Name) {
1646   const Type* IntPtrT = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1647   return wrap(CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), IntPtrT,
1648                                      unwrap(Ty), unwrap(Val), 0, Twine(Name)));
1649 }
1650
1651 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1652                              const char *Name) {
1653   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1654 }
1655
1656 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1657                                   LLVMValueRef Val, const char *Name) {
1658   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1659 }
1660
1661 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1662   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1663 }
1664
1665
1666 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1667                            const char *Name) {
1668   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1669 }
1670
1671 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1672                             LLVMValueRef PointerVal) {
1673   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1674 }
1675
1676 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1677                           LLVMValueRef *Indices, unsigned NumIndices,
1678                           const char *Name) {
1679   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1680                                    unwrap(Indices) + NumIndices, Name));
1681 }
1682
1683 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1684                                   LLVMValueRef *Indices, unsigned NumIndices,
1685                                   const char *Name) {
1686   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1687                                            unwrap(Indices) + NumIndices, Name));
1688 }
1689
1690 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1691                                 unsigned Idx, const char *Name) {
1692   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1693 }
1694
1695 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1696                                    const char *Name) {
1697   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1698 }
1699
1700 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1701                                       const char *Name) {
1702   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1703 }
1704
1705 /*--.. Casts ...............................................................--*/
1706
1707 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1708                             LLVMTypeRef DestTy, const char *Name) {
1709   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1710 }
1711
1712 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1713                            LLVMTypeRef DestTy, const char *Name) {
1714   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1715 }
1716
1717 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1718                            LLVMTypeRef DestTy, const char *Name) {
1719   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1720 }
1721
1722 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1723                              LLVMTypeRef DestTy, const char *Name) {
1724   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1725 }
1726
1727 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1728                              LLVMTypeRef DestTy, const char *Name) {
1729   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1730 }
1731
1732 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1733                              LLVMTypeRef DestTy, const char *Name) {
1734   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1735 }
1736
1737 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1738                              LLVMTypeRef DestTy, const char *Name) {
1739   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1740 }
1741
1742 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1743                               LLVMTypeRef DestTy, const char *Name) {
1744   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1745 }
1746
1747 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1748                             LLVMTypeRef DestTy, const char *Name) {
1749   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1750 }
1751
1752 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1753                                LLVMTypeRef DestTy, const char *Name) {
1754   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1755 }
1756
1757 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1758                                LLVMTypeRef DestTy, const char *Name) {
1759   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1760 }
1761
1762 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1763                               LLVMTypeRef DestTy, const char *Name) {
1764   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1765 }
1766
1767 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1768                                     LLVMTypeRef DestTy, const char *Name) {
1769   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1770                                              Name));
1771 }
1772
1773 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1774                                     LLVMTypeRef DestTy, const char *Name) {
1775   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1776                                              Name));
1777 }
1778
1779 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1780                                      LLVMTypeRef DestTy, const char *Name) {
1781   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1782                                               Name));
1783 }
1784
1785 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1786                                   LLVMTypeRef DestTy, const char *Name) {
1787   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1788 }
1789
1790 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1791                               LLVMTypeRef DestTy, const char *Name) {
1792   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), Name));
1793 }
1794
1795 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1796                              LLVMTypeRef DestTy, const char *Name) {
1797   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
1798 }
1799
1800 /*--.. Comparisons .........................................................--*/
1801
1802 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1803                            LLVMValueRef LHS, LLVMValueRef RHS,
1804                            const char *Name) {
1805   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1806                                     unwrap(LHS), unwrap(RHS), Name));
1807 }
1808
1809 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1810                            LLVMValueRef LHS, LLVMValueRef RHS,
1811                            const char *Name) {
1812   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1813                                     unwrap(LHS), unwrap(RHS), Name));
1814 }
1815
1816 /*--.. Miscellaneous instructions ..........................................--*/
1817
1818 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1819   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1820 }
1821
1822 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1823                            LLVMValueRef *Args, unsigned NumArgs,
1824                            const char *Name) {
1825   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1826                                     unwrap(Args) + NumArgs, Name));
1827 }
1828
1829 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1830                              LLVMValueRef Then, LLVMValueRef Else,
1831                              const char *Name) {
1832   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1833                                       Name));
1834 }
1835
1836 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1837                             LLVMTypeRef Ty, const char *Name) {
1838   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1839 }
1840
1841 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1842                                       LLVMValueRef Index, const char *Name) {
1843   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1844                                               Name));
1845 }
1846
1847 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1848                                     LLVMValueRef EltVal, LLVMValueRef Index,
1849                                     const char *Name) {
1850   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1851                                              unwrap(Index), Name));
1852 }
1853
1854 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1855                                     LLVMValueRef V2, LLVMValueRef Mask,
1856                                     const char *Name) {
1857   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1858                                              unwrap(Mask), Name));
1859 }
1860
1861 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1862                                    unsigned Index, const char *Name) {
1863   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1864 }
1865
1866 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1867                                   LLVMValueRef EltVal, unsigned Index,
1868                                   const char *Name) {
1869   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1870                                            Index, Name));
1871 }
1872
1873 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
1874                              const char *Name) {
1875   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
1876 }
1877
1878 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
1879                                 const char *Name) {
1880   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
1881 }
1882
1883 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
1884                               LLVMValueRef RHS, const char *Name) {
1885   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
1886 }
1887
1888
1889 /*===-- Module providers --------------------------------------------------===*/
1890
1891 LLVMModuleProviderRef
1892 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1893   return wrap(new ExistingModuleProvider(unwrap(M)));
1894 }
1895
1896 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1897   delete unwrap(MP);
1898 }
1899
1900
1901 /*===-- Memory buffers ----------------------------------------------------===*/
1902
1903 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1904                                              LLVMMemoryBufferRef *OutMemBuf,
1905                                              char **OutMessage) {
1906   std::string Error;
1907   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1908     *OutMemBuf = wrap(MB);
1909     return 0;
1910   }
1911   
1912   *OutMessage = strdup(Error.c_str());
1913   return 1;
1914 }
1915
1916 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1917                                     char **OutMessage) {
1918   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1919     *OutMemBuf = wrap(MB);
1920     return 0;
1921   }
1922   
1923   *OutMessage = strdup("stdin is empty.");
1924   return 1;
1925 }
1926
1927 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1928   delete unwrap(MemBuf);
1929 }