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