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