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