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