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