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