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