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