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