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