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