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