assert(0) -> LLVM_UNREACHABLE.
[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 C bindings for libLLVMCore.a, which implements
11 // 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/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32
33 using namespace llvm;
34
35
36 /*===-- Error handling ----------------------------------------------------===*/
37
38 void LLVMDisposeMessage(char *Message) {
39   free(Message);
40 }
41
42
43 /*===-- Operations on contexts --------------------------------------------===*/
44
45 LLVMContextRef LLVMContextCreate() {
46   return wrap(new LLVMContext());
47 }
48
49 LLVMContextRef LLVMGetGlobalContext() {
50   return wrap(&getGlobalContext());
51 }
52
53 void LLVMContextDispose(LLVMContextRef C) {
54   delete unwrap(C);
55 }
56
57
58 /*===-- Operations on modules ---------------------------------------------===*/
59
60 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
61   return wrap(new Module(ModuleID, getGlobalContext()));
62 }
63
64 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
65                                                 LLVMContextRef C) {
66   return wrap(new Module(ModuleID, *unwrap(C)));
67 }
68
69 void LLVMDisposeModule(LLVMModuleRef M) {
70   delete unwrap(M);
71 }
72
73 /*--.. Data layout .........................................................--*/
74 const char * LLVMGetDataLayout(LLVMModuleRef M) {
75   return unwrap(M)->getDataLayout().c_str();
76 }
77
78 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
79   unwrap(M)->setDataLayout(Triple);
80 }
81
82 /*--.. Target triple .......................................................--*/
83 const char * LLVMGetTarget(LLVMModuleRef M) {
84   return unwrap(M)->getTargetTriple().c_str();
85 }
86
87 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
88   unwrap(M)->setTargetTriple(Triple);
89 }
90
91 /*--.. Type names ..........................................................--*/
92 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
93   return unwrap(M)->addTypeName(Name, unwrap(Ty));
94 }
95
96 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
97   std::string N(Name);
98   
99   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
100   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
101     if (I->first == N)
102       TST.remove(I);
103 }
104
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106   std::string N(Name);
107   return wrap(unwrap(M)->getTypeByName(N));
108 }
109
110 void LLVMDumpModule(LLVMModuleRef M) {
111   unwrap(M)->dump();
112 }
113
114
115 /*===-- Operations on types -----------------------------------------------===*/
116
117 /*--.. Operations on all types (mostly) ....................................--*/
118
119 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
120   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
121 }
122
123 /*--.. Operations on integer types .........................................--*/
124
125 LLVMTypeRef LLVMInt1Type(void)  { return (LLVMTypeRef) Type::Int1Ty;  }
126 LLVMTypeRef LLVMInt8Type(void)  { return (LLVMTypeRef) Type::Int8Ty;  }
127 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
128 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
129 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
130
131 LLVMTypeRef LLVMIntType(unsigned NumBits) {
132   return wrap(getGlobalContext().getIntegerType(NumBits));
133 }
134
135 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
136   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
137 }
138
139 /*--.. Operations on real types ............................................--*/
140
141 LLVMTypeRef LLVMFloatType(void)    { return (LLVMTypeRef) Type::FloatTy;     }
142 LLVMTypeRef LLVMDoubleType(void)   { return (LLVMTypeRef) Type::DoubleTy;    }
143 LLVMTypeRef LLVMX86FP80Type(void)  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
144 LLVMTypeRef LLVMFP128Type(void)    { return (LLVMTypeRef) Type::FP128Ty;     }
145 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
146
147 /*--.. Operations on function types ........................................--*/
148
149 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
150                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
151                              int IsVarArg) {
152   std::vector<const Type*> Tys;
153   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
154     Tys.push_back(unwrap(*I));
155   
156   return wrap(getGlobalContext().getFunctionType(unwrap(ReturnType), Tys,
157                                                  IsVarArg != 0));
158 }
159
160 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
161   return unwrap<FunctionType>(FunctionTy)->isVarArg();
162 }
163
164 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
165   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
166 }
167
168 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
169   return unwrap<FunctionType>(FunctionTy)->getNumParams();
170 }
171
172 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
173   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
174   for (FunctionType::param_iterator I = Ty->param_begin(),
175                                     E = Ty->param_end(); I != E; ++I)
176     *Dest++ = wrap(*I);
177 }
178
179 /*--.. Operations on struct types ..........................................--*/
180
181 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
182                            unsigned ElementCount, int Packed) {
183   std::vector<const Type*> Tys;
184   for (LLVMTypeRef *I = ElementTypes,
185                    *E = ElementTypes + ElementCount; I != E; ++I)
186     Tys.push_back(unwrap(*I));
187   
188   return wrap(getGlobalContext().getStructType(Tys, Packed != 0));
189 }
190
191 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
192   return unwrap<StructType>(StructTy)->getNumElements();
193 }
194
195 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
196   StructType *Ty = unwrap<StructType>(StructTy);
197   for (FunctionType::param_iterator I = Ty->element_begin(),
198                                     E = Ty->element_end(); I != E; ++I)
199     *Dest++ = wrap(*I);
200 }
201
202 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
203   return unwrap<StructType>(StructTy)->isPacked();
204 }
205
206 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
207
208 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
209   return wrap(getGlobalContext().getArrayType(unwrap(ElementType), 
210                                                ElementCount));
211 }
212
213 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
214   return wrap(getGlobalContext().getPointerType(unwrap(ElementType), 
215                                                  AddressSpace));
216 }
217
218 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
219   return wrap(getGlobalContext().getVectorType(unwrap(ElementType), 
220                                                 ElementCount));
221 }
222
223 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
224   return wrap(unwrap<SequentialType>(Ty)->getElementType());
225 }
226
227 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
228   return unwrap<ArrayType>(ArrayTy)->getNumElements();
229 }
230
231 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
232   return unwrap<PointerType>(PointerTy)->getAddressSpace();
233 }
234
235 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
236   return unwrap<VectorType>(VectorTy)->getNumElements();
237 }
238
239 /*--.. Operations on other types ...........................................--*/
240
241 LLVMTypeRef LLVMVoidType(void)  { return (LLVMTypeRef) Type::VoidTy;  }
242 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
243
244 LLVMTypeRef LLVMOpaqueType(void) {
245   return wrap(getGlobalContext().getOpaqueType());
246 }
247
248 /*--.. Operations on type handles ..........................................--*/
249
250 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
251   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
252 }
253
254 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
255   delete unwrap(TypeHandle);
256 }
257
258 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
259   return wrap(unwrap(TypeHandle)->get());
260 }
261
262 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
263   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
264 }
265
266
267 /*===-- Operations on values ----------------------------------------------===*/
268
269 /*--.. Operations on all values ............................................--*/
270
271 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
272   return wrap(unwrap(Val)->getType());
273 }
274
275 const char *LLVMGetValueName(LLVMValueRef Val) {
276   return unwrap(Val)->getNameStart();
277 }
278
279 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
280   unwrap(Val)->setName(Name);
281 }
282
283 void LLVMDumpValue(LLVMValueRef Val) {
284   unwrap(Val)->dump();
285 }
286
287
288 /*--.. Conversion functions ................................................--*/
289
290 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
291   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
292     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
293   }
294
295 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
296
297
298 /*--.. Operations on constants of any type .................................--*/
299
300 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
301   return wrap(getGlobalContext().getNullValue(unwrap(Ty)));
302 }
303
304 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
305   return wrap(getGlobalContext().getAllOnesValue(unwrap(Ty)));
306 }
307
308 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
309   return wrap(getGlobalContext().getUndef(unwrap(Ty)));
310 }
311
312 int LLVMIsConstant(LLVMValueRef Ty) {
313   return isa<Constant>(unwrap(Ty));
314 }
315
316 int LLVMIsNull(LLVMValueRef Val) {
317   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
318     return C->isNullValue();
319   return false;
320 }
321
322 int LLVMIsUndef(LLVMValueRef Val) {
323   return isa<UndefValue>(unwrap(Val));
324 }
325
326 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
327   return
328       wrap(getGlobalContext().getConstantPointerNull(unwrap<PointerType>(Ty)));
329 }
330
331 /*--.. Operations on scalar constants ......................................--*/
332
333 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
334                           int SignExtend) {
335   return wrap(getGlobalContext().getConstantInt(unwrap<IntegerType>(IntTy), N,
336                                                  SignExtend != 0));
337 }
338
339 static const fltSemantics &SemanticsForType(Type *Ty) {
340   assert(Ty->isFloatingPoint() && "Type is not floating point!");
341   if (Ty == Type::FloatTy)
342     return APFloat::IEEEsingle;
343   if (Ty == Type::DoubleTy)
344     return APFloat::IEEEdouble;
345   if (Ty == Type::X86_FP80Ty)
346     return APFloat::x87DoubleExtended;
347   if (Ty == Type::FP128Ty)
348     return APFloat::IEEEquad;
349   if (Ty == Type::PPC_FP128Ty)
350     return APFloat::PPCDoubleDouble;
351   return APFloat::Bogus;
352 }
353
354 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
355   APFloat APN(N);
356   bool ignored;
357   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
358               &ignored);
359   return wrap(getGlobalContext().getConstantFP(APN));
360 }
361
362 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
363   return wrap(getGlobalContext().getConstantFP(
364                               APFloat(SemanticsForType(unwrap(RealTy)), Text)));
365 }
366
367 /*--.. Operations on composite constants ...................................--*/
368
369 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
370                              int DontNullTerminate) {
371   /* Inverted the sense of AddNull because ', 0)' is a
372      better mnemonic for null termination than ', 1)'. */
373   return wrap(getGlobalContext().getConstantArray(std::string(Str, Length),
374                                  DontNullTerminate == 0));
375 }
376
377 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
378                             LLVMValueRef *ConstantVals, unsigned Length) {
379   return wrap(getGlobalContext().getConstantArray(
380                     getGlobalContext().getArrayType(unwrap(ElementTy), Length),
381                                  unwrap<Constant>(ConstantVals, Length),
382                                  Length));
383 }
384
385 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
386                              int Packed) {
387   return wrap(getGlobalContext().getConstantStruct(
388                                   unwrap<Constant>(ConstantVals, Count),
389                                   Count, Packed != 0));
390 }
391
392 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
393   return wrap(getGlobalContext().getConstantVector(
394                             unwrap<Constant>(ScalarConstantVals, Size), Size));
395 }
396
397 /*--.. Constant expressions ................................................--*/
398
399 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
400   return wrap(getGlobalContext().getConstantExprAlignOf(unwrap(Ty)));
401 }
402
403 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
404   return wrap(getGlobalContext().getConstantExprSizeOf(unwrap(Ty)));
405 }
406
407 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
408   return wrap(getGlobalContext().getConstantExprNeg(
409                                                 unwrap<Constant>(ConstantVal)));
410 }
411
412 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
413   return wrap(getGlobalContext().getConstantExprNot(
414                                                 unwrap<Constant>(ConstantVal)));
415 }
416
417 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
418   return wrap(getGlobalContext().getConstantExprAdd(
419                                    unwrap<Constant>(LHSConstant),
420                                    unwrap<Constant>(RHSConstant)));
421 }
422
423 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
424   return wrap(getGlobalContext().getConstantExprSub(
425                                    unwrap<Constant>(LHSConstant),
426                                    unwrap<Constant>(RHSConstant)));
427 }
428
429 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
430   return wrap(getGlobalContext().getConstantExprMul(
431                                    unwrap<Constant>(LHSConstant),
432                                    unwrap<Constant>(RHSConstant)));
433 }
434
435 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
436   return wrap(getGlobalContext().getConstantExprUDiv(
437                                     unwrap<Constant>(LHSConstant),
438                                     unwrap<Constant>(RHSConstant)));
439 }
440
441 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
442   return wrap(getGlobalContext().getConstantExprSDiv(
443                                     unwrap<Constant>(LHSConstant),
444                                     unwrap<Constant>(RHSConstant)));
445 }
446
447 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
448   return wrap(getGlobalContext().getConstantExprFDiv(
449                                     unwrap<Constant>(LHSConstant),
450                                     unwrap<Constant>(RHSConstant)));
451 }
452
453 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
454   return wrap(getGlobalContext().getConstantExprURem(
455                                     unwrap<Constant>(LHSConstant),
456                                     unwrap<Constant>(RHSConstant)));
457 }
458
459 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
460   return wrap(getGlobalContext().getConstantExprSRem(
461                                     unwrap<Constant>(LHSConstant),
462                                     unwrap<Constant>(RHSConstant)));
463 }
464
465 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
466   return wrap(getGlobalContext().getConstantExprFRem(
467                                     unwrap<Constant>(LHSConstant),
468                                     unwrap<Constant>(RHSConstant)));
469 }
470
471 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
472   return wrap(getGlobalContext().getConstantExprAnd(
473                                    unwrap<Constant>(LHSConstant),
474                                    unwrap<Constant>(RHSConstant)));
475 }
476
477 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
478   return wrap(getGlobalContext().getConstantExprOr(
479                                   unwrap<Constant>(LHSConstant),
480                                   unwrap<Constant>(RHSConstant)));
481 }
482
483 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
484   return wrap(getGlobalContext().getConstantExprXor(
485                                    unwrap<Constant>(LHSConstant),
486                                    unwrap<Constant>(RHSConstant)));
487 }
488
489 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
490                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
491   return wrap(getGlobalContext().getConstantExprICmp(Predicate,
492                                     unwrap<Constant>(LHSConstant),
493                                     unwrap<Constant>(RHSConstant)));
494 }
495
496 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
497                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
498   return wrap(getGlobalContext().getConstantExprFCmp(Predicate,
499                                     unwrap<Constant>(LHSConstant),
500                                     unwrap<Constant>(RHSConstant)));
501 }
502
503 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
504   return wrap(getGlobalContext().getConstantExprShl(
505                                   unwrap<Constant>(LHSConstant),
506                                   unwrap<Constant>(RHSConstant)));
507 }
508
509 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
510   return wrap(getGlobalContext().getConstantExprLShr(
511                                     unwrap<Constant>(LHSConstant),
512                                     unwrap<Constant>(RHSConstant)));
513 }
514
515 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
516   return wrap(getGlobalContext().getConstantExprAShr(
517                                     unwrap<Constant>(LHSConstant),
518                                     unwrap<Constant>(RHSConstant)));
519 }
520
521 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
522                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
523   return wrap(getGlobalContext().getConstantExprGetElementPtr(
524                                              unwrap<Constant>(ConstantVal),
525                                              unwrap<Constant>(ConstantIndices, 
526                                                               NumIndices),
527                                              NumIndices));
528 }
529
530 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
531   return wrap(getGlobalContext().getConstantExprTrunc(
532                                      unwrap<Constant>(ConstantVal),
533                                      unwrap(ToType)));
534 }
535
536 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
537   return wrap(getGlobalContext().getConstantExprSExt(
538                                     unwrap<Constant>(ConstantVal),
539                                     unwrap(ToType)));
540 }
541
542 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
543   return wrap(getGlobalContext().getConstantExprZExt(
544                                     unwrap<Constant>(ConstantVal),
545                                     unwrap(ToType)));
546 }
547
548 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
549   return wrap(getGlobalContext().getConstantExprFPTrunc(
550                                        unwrap<Constant>(ConstantVal),
551                                        unwrap(ToType)));
552 }
553
554 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
555   return wrap(getGlobalContext().getConstantExprFPExtend(
556                                         unwrap<Constant>(ConstantVal),
557                                         unwrap(ToType)));
558 }
559
560 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
561   return wrap(getGlobalContext().getConstantExprUIToFP(
562                                       unwrap<Constant>(ConstantVal),
563                                       unwrap(ToType)));
564 }
565
566 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
567   return wrap(getGlobalContext().getConstantExprSIToFP(unwrap<Constant>(ConstantVal),
568                                       unwrap(ToType)));
569 }
570
571 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
572   return wrap(getGlobalContext().getConstantExprFPToUI(unwrap<Constant>(ConstantVal),
573                                       unwrap(ToType)));
574 }
575
576 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
577   return wrap(getGlobalContext().getConstantExprFPToSI(
578                                       unwrap<Constant>(ConstantVal),
579                                       unwrap(ToType)));
580 }
581
582 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
583   return wrap(getGlobalContext().getConstantExprPtrToInt(
584                                         unwrap<Constant>(ConstantVal),
585                                         unwrap(ToType)));
586 }
587
588 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
589   return wrap(getGlobalContext().getConstantExprIntToPtr(
590                                         unwrap<Constant>(ConstantVal),
591                                         unwrap(ToType)));
592 }
593
594 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
595   return wrap(getGlobalContext().getConstantExprBitCast(
596                                        unwrap<Constant>(ConstantVal),
597                                        unwrap(ToType)));
598 }
599
600 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
601                              LLVMValueRef ConstantIfTrue,
602                              LLVMValueRef ConstantIfFalse) {
603   return wrap(getGlobalContext().getConstantExprSelect(
604                                       unwrap<Constant>(ConstantCondition),
605                                       unwrap<Constant>(ConstantIfTrue),
606                                       unwrap<Constant>(ConstantIfFalse)));
607 }
608
609 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
610                                      LLVMValueRef IndexConstant) {
611   return wrap(getGlobalContext().getConstantExprExtractElement(
612                                               unwrap<Constant>(VectorConstant),
613                                               unwrap<Constant>(IndexConstant)));
614 }
615
616 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
617                                     LLVMValueRef ElementValueConstant,
618                                     LLVMValueRef IndexConstant) {
619   return wrap(getGlobalContext().getConstantExprInsertElement(
620                                          unwrap<Constant>(VectorConstant),
621                                          unwrap<Constant>(ElementValueConstant),
622                                              unwrap<Constant>(IndexConstant)));
623 }
624
625 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
626                                     LLVMValueRef VectorBConstant,
627                                     LLVMValueRef MaskConstant) {
628   return wrap(getGlobalContext().getConstantExprShuffleVector(
629                                              unwrap<Constant>(VectorAConstant),
630                                              unwrap<Constant>(VectorBConstant),
631                                              unwrap<Constant>(MaskConstant)));
632 }
633
634 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
635                                    unsigned NumIdx) {
636   return wrap(getGlobalContext().getConstantExprExtractValue(
637                                             unwrap<Constant>(AggConstant),
638                                             IdxList, NumIdx));
639 }
640
641 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
642                                   LLVMValueRef ElementValueConstant,
643                                   unsigned *IdxList, unsigned NumIdx) {
644   return wrap(getGlobalContext().getConstantExprInsertValue(
645                                          unwrap<Constant>(AggConstant),
646                                          unwrap<Constant>(ElementValueConstant),
647                                            IdxList, NumIdx));
648 }
649
650 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
651                                 const char *Constraints, int HasSideEffects) {
652   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
653                              Constraints, HasSideEffects));
654 }
655
656 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
657
658 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
659   return wrap(unwrap<GlobalValue>(Global)->getParent());
660 }
661
662 int LLVMIsDeclaration(LLVMValueRef Global) {
663   return unwrap<GlobalValue>(Global)->isDeclaration();
664 }
665
666 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
667   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
668 }
669
670 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
671   unwrap<GlobalValue>(Global)
672     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
673 }
674
675 const char *LLVMGetSection(LLVMValueRef Global) {
676   return unwrap<GlobalValue>(Global)->getSection().c_str();
677 }
678
679 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
680   unwrap<GlobalValue>(Global)->setSection(Section);
681 }
682
683 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
684   return static_cast<LLVMVisibility>(
685     unwrap<GlobalValue>(Global)->getVisibility());
686 }
687
688 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
689   unwrap<GlobalValue>(Global)
690     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
691 }
692
693 unsigned LLVMGetAlignment(LLVMValueRef Global) {
694   return unwrap<GlobalValue>(Global)->getAlignment();
695 }
696
697 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
698   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
699 }
700
701 /*--.. Operations on global variables ......................................--*/
702
703 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
704   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
705                                  GlobalValue::ExternalLinkage, 0, Name));
706 }
707
708 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
709   return wrap(unwrap(M)->getNamedGlobal(Name));
710 }
711
712 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
713   Module *Mod = unwrap(M);
714   Module::global_iterator I = Mod->global_begin();
715   if (I == Mod->global_end())
716     return 0;
717   return wrap(I);
718 }
719
720 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
721   Module *Mod = unwrap(M);
722   Module::global_iterator I = Mod->global_end();
723   if (I == Mod->global_begin())
724     return 0;
725   return wrap(--I);
726 }
727
728 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
729   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
730   Module::global_iterator I = GV;
731   if (++I == GV->getParent()->global_end())
732     return 0;
733   return wrap(I);
734 }
735
736 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
737   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
738   Module::global_iterator I = GV;
739   if (I == GV->getParent()->global_begin())
740     return 0;
741   return wrap(--I);
742 }
743
744 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
745   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
746 }
747
748 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
749   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
750 }
751
752 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
753   unwrap<GlobalVariable>(GlobalVar)
754     ->setInitializer(unwrap<Constant>(ConstantVal));
755 }
756
757 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
758   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
759 }
760
761 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
762   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
763 }
764
765 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
766   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
767 }
768
769 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
770   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
771 }
772
773 /*--.. Operations on aliases ......................................--*/
774
775 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
776                           const char *Name) {
777   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
778                               unwrap<Constant>(Aliasee), unwrap (M)));
779 }
780
781 /*--.. Operations on functions .............................................--*/
782
783 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
784                              LLVMTypeRef FunctionTy) {
785   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
786                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
787 }
788
789 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
790   return wrap(unwrap(M)->getFunction(Name));
791 }
792
793 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
794   Module *Mod = unwrap(M);
795   Module::iterator I = Mod->begin();
796   if (I == Mod->end())
797     return 0;
798   return wrap(I);
799 }
800
801 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
802   Module *Mod = unwrap(M);
803   Module::iterator I = Mod->end();
804   if (I == Mod->begin())
805     return 0;
806   return wrap(--I);
807 }
808
809 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
810   Function *Func = unwrap<Function>(Fn);
811   Module::iterator I = Func;
812   if (++I == Func->getParent()->end())
813     return 0;
814   return wrap(I);
815 }
816
817 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
818   Function *Func = unwrap<Function>(Fn);
819   Module::iterator I = Func;
820   if (I == Func->getParent()->begin())
821     return 0;
822   return wrap(--I);
823 }
824
825 void LLVMDeleteFunction(LLVMValueRef Fn) {
826   unwrap<Function>(Fn)->eraseFromParent();
827 }
828
829 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
830   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
831     return F->getIntrinsicID();
832   return 0;
833 }
834
835 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
836   return unwrap<Function>(Fn)->getCallingConv();
837 }
838
839 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
840   return unwrap<Function>(Fn)->setCallingConv(CC);
841 }
842
843 const char *LLVMGetGC(LLVMValueRef Fn) {
844   Function *F = unwrap<Function>(Fn);
845   return F->hasGC()? F->getGC() : 0;
846 }
847
848 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
849   Function *F = unwrap<Function>(Fn);
850   if (GC)
851     F->setGC(GC);
852   else
853     F->clearGC();
854 }
855
856 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
857   Function *Func = unwrap<Function>(Fn);
858   const AttrListPtr PAL = Func->getAttributes();
859   const AttrListPtr PALnew = PAL.addAttr(0, PA);
860   Func->setAttributes(PALnew);
861 }
862
863 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
864   Function *Func = unwrap<Function>(Fn);
865   const AttrListPtr PAL = Func->getAttributes();
866   const AttrListPtr PALnew = PAL.removeAttr(0, PA);
867   Func->setAttributes(PALnew);
868 }
869
870 /*--.. Operations on parameters ............................................--*/
871
872 unsigned LLVMCountParams(LLVMValueRef FnRef) {
873   // This function is strictly redundant to
874   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
875   return unwrap<Function>(FnRef)->arg_size();
876 }
877
878 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
879   Function *Fn = unwrap<Function>(FnRef);
880   for (Function::arg_iterator I = Fn->arg_begin(),
881                               E = Fn->arg_end(); I != E; I++)
882     *ParamRefs++ = wrap(I);
883 }
884
885 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
886   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
887   while (index --> 0)
888     AI++;
889   return wrap(AI);
890 }
891
892 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
893   return wrap(unwrap<Argument>(V)->getParent());
894 }
895
896 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
897   Function *Func = unwrap<Function>(Fn);
898   Function::arg_iterator I = Func->arg_begin();
899   if (I == Func->arg_end())
900     return 0;
901   return wrap(I);
902 }
903
904 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
905   Function *Func = unwrap<Function>(Fn);
906   Function::arg_iterator I = Func->arg_end();
907   if (I == Func->arg_begin())
908     return 0;
909   return wrap(--I);
910 }
911
912 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
913   Argument *A = unwrap<Argument>(Arg);
914   Function::arg_iterator I = A;
915   if (++I == A->getParent()->arg_end())
916     return 0;
917   return wrap(I);
918 }
919
920 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
921   Argument *A = unwrap<Argument>(Arg);
922   Function::arg_iterator I = A;
923   if (I == A->getParent()->arg_begin())
924     return 0;
925   return wrap(--I);
926 }
927
928 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
929   unwrap<Argument>(Arg)->addAttr(PA);
930 }
931
932 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
933   unwrap<Argument>(Arg)->removeAttr(PA);
934 }
935
936 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
937   unwrap<Argument>(Arg)->addAttr(
938           Attribute::constructAlignmentFromInt(align));
939 }
940
941 /*--.. Operations on basic blocks ..........................................--*/
942
943 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
944   return wrap(static_cast<Value*>(unwrap(BB)));
945 }
946
947 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
948   return isa<BasicBlock>(unwrap(Val));
949 }
950
951 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
952   return wrap(unwrap<BasicBlock>(Val));
953 }
954
955 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
956   return wrap(unwrap(BB)->getParent());
957 }
958
959 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
960   return unwrap<Function>(FnRef)->size();
961 }
962
963 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
964   Function *Fn = unwrap<Function>(FnRef);
965   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
966     *BasicBlocksRefs++ = wrap(I);
967 }
968
969 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
970   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
971 }
972
973 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
974   Function *Func = unwrap<Function>(Fn);
975   Function::iterator I = Func->begin();
976   if (I == Func->end())
977     return 0;
978   return wrap(I);
979 }
980
981 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
982   Function *Func = unwrap<Function>(Fn);
983   Function::iterator I = Func->end();
984   if (I == Func->begin())
985     return 0;
986   return wrap(--I);
987 }
988
989 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
990   BasicBlock *Block = unwrap(BB);
991   Function::iterator I = Block;
992   if (++I == Block->getParent()->end())
993     return 0;
994   return wrap(I);
995 }
996
997 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
998   BasicBlock *Block = unwrap(BB);
999   Function::iterator I = Block;
1000   if (I == Block->getParent()->begin())
1001     return 0;
1002   return wrap(--I);
1003 }
1004
1005 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1006   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
1007 }
1008
1009 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
1010                                        const char *Name) {
1011   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
1012   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
1013                                  InsertBeforeBB));
1014 }
1015
1016 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1017   unwrap(BBRef)->eraseFromParent();
1018 }
1019
1020 /*--.. Operations on instructions ..........................................--*/
1021
1022 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1023   return wrap(unwrap<Instruction>(Inst)->getParent());
1024 }
1025
1026 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1027   BasicBlock *Block = unwrap(BB);
1028   BasicBlock::iterator I = Block->begin();
1029   if (I == Block->end())
1030     return 0;
1031   return wrap(I);
1032 }
1033
1034 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1035   BasicBlock *Block = unwrap(BB);
1036   BasicBlock::iterator I = Block->end();
1037   if (I == Block->begin())
1038     return 0;
1039   return wrap(--I);
1040 }
1041
1042 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1043   Instruction *Instr = unwrap<Instruction>(Inst);
1044   BasicBlock::iterator I = Instr;
1045   if (++I == Instr->getParent()->end())
1046     return 0;
1047   return wrap(I);
1048 }
1049
1050 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1051   Instruction *Instr = unwrap<Instruction>(Inst);
1052   BasicBlock::iterator I = Instr;
1053   if (I == Instr->getParent()->begin())
1054     return 0;
1055   return wrap(--I);
1056 }
1057
1058 /*--.. Call and invoke instructions ........................................--*/
1059
1060 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1061   Value *V = unwrap(Instr);
1062   if (CallInst *CI = dyn_cast<CallInst>(V))
1063     return CI->getCallingConv();
1064   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1065     return II->getCallingConv();
1066   LLVM_UNREACHABLE("LLVMGetInstructionCallConv applies only to call and invoke!");
1067   return 0;
1068 }
1069
1070 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1071   Value *V = unwrap(Instr);
1072   if (CallInst *CI = dyn_cast<CallInst>(V))
1073     return CI->setCallingConv(CC);
1074   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1075     return II->setCallingConv(CC);
1076   LLVM_UNREACHABLE("LLVMSetInstructionCallConv applies only to call and invoke!");
1077 }
1078
1079 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1080                            LLVMAttribute PA) {
1081   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1082   Call.setAttributes(
1083     Call.getAttributes().addAttr(index, PA));
1084 }
1085
1086 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1087                               LLVMAttribute PA) {
1088   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1089   Call.setAttributes(
1090     Call.getAttributes().removeAttr(index, PA));
1091 }
1092
1093 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1094                                 unsigned align) {
1095   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1096   Call.setAttributes(
1097     Call.getAttributes().addAttr(index, 
1098         Attribute::constructAlignmentFromInt(align)));
1099 }
1100
1101 /*--.. Operations on call instructions (only) ..............................--*/
1102
1103 int LLVMIsTailCall(LLVMValueRef Call) {
1104   return unwrap<CallInst>(Call)->isTailCall();
1105 }
1106
1107 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1108   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1109 }
1110
1111 /*--.. Operations on phi nodes .............................................--*/
1112
1113 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1114                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1115   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1116   for (unsigned I = 0; I != Count; ++I)
1117     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1118 }
1119
1120 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1121   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1122 }
1123
1124 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1125   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1126 }
1127
1128 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1129   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1130 }
1131
1132
1133 /*===-- Instruction builders ----------------------------------------------===*/
1134
1135 LLVMBuilderRef LLVMCreateBuilder(void) {
1136   return wrap(new IRBuilder<>(getGlobalContext()));
1137 }
1138
1139 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1140                          LLVMValueRef Instr) {
1141   BasicBlock *BB = unwrap(Block);
1142   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1143   unwrap(Builder)->SetInsertPoint(BB, I);
1144 }
1145
1146 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1147   Instruction *I = unwrap<Instruction>(Instr);
1148   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1149 }
1150
1151 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1152   BasicBlock *BB = unwrap(Block);
1153   unwrap(Builder)->SetInsertPoint(BB);
1154 }
1155
1156 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1157    return wrap(unwrap(Builder)->GetInsertBlock());
1158 }
1159
1160 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1161   unwrap(Builder)->ClearInsertionPoint ();
1162 }
1163
1164 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1165   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1166 }
1167
1168 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1169   delete unwrap(Builder);
1170 }
1171
1172 /*--.. Instruction builders ................................................--*/
1173
1174 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1175   return wrap(unwrap(B)->CreateRetVoid());
1176 }
1177
1178 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1179   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1180 }
1181
1182 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1183   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1184 }
1185
1186 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1187                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1188   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1189 }
1190
1191 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1192                              LLVMBasicBlockRef Else, unsigned NumCases) {
1193   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1194 }
1195
1196 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1197                              LLVMValueRef *Args, unsigned NumArgs,
1198                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1199                              const char *Name) {
1200   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1201                                       unwrap(Args), unwrap(Args) + NumArgs,
1202                                       Name));
1203 }
1204
1205 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1206   return wrap(unwrap(B)->CreateUnwind());
1207 }
1208
1209 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1210   return wrap(unwrap(B)->CreateUnreachable());
1211 }
1212
1213 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1214                  LLVMBasicBlockRef Dest) {
1215   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1216 }
1217
1218 /*--.. Arithmetic ..........................................................--*/
1219
1220 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1221                           const char *Name) {
1222   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1223 }
1224
1225 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1226                           const char *Name) {
1227   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1228 }
1229
1230 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1231                           const char *Name) {
1232   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1233 }
1234
1235 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1236                            const char *Name) {
1237   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1238 }
1239
1240 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1241                            const char *Name) {
1242   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1243 }
1244
1245 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1246                            const char *Name) {
1247   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1248 }
1249
1250 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1251                            const char *Name) {
1252   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1253 }
1254
1255 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1256                            const char *Name) {
1257   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1258 }
1259
1260 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1261                            const char *Name) {
1262   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1263 }
1264
1265 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1266                           const char *Name) {
1267   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1268 }
1269
1270 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1271                            const char *Name) {
1272   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1273 }
1274
1275 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1276                            const char *Name) {
1277   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1278 }
1279
1280 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1281                           const char *Name) {
1282   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1283 }
1284
1285 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1286                          const char *Name) {
1287   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1288 }
1289
1290 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1291                           const char *Name) {
1292   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1293 }
1294
1295 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1296   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1297 }
1298
1299 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1300   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1301 }
1302
1303 /*--.. Memory ..............................................................--*/
1304
1305 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1306                              const char *Name) {
1307   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1308 }
1309
1310 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1311                                   LLVMValueRef Val, const char *Name) {
1312   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1313 }
1314
1315 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1316                              const char *Name) {
1317   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1318 }
1319
1320 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1321                                   LLVMValueRef Val, const char *Name) {
1322   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1323 }
1324
1325 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1326   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1327 }
1328
1329
1330 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1331                            const char *Name) {
1332   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1333 }
1334
1335 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1336                             LLVMValueRef PointerVal) {
1337   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1338 }
1339
1340 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1341                           LLVMValueRef *Indices, unsigned NumIndices,
1342                           const char *Name) {
1343   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1344                                    unwrap(Indices) + NumIndices, Name));
1345 }
1346
1347 /*--.. Casts ...............................................................--*/
1348
1349 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1350                             LLVMTypeRef DestTy, const char *Name) {
1351   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1352 }
1353
1354 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1355                            LLVMTypeRef DestTy, const char *Name) {
1356   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1357 }
1358
1359 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1360                            LLVMTypeRef DestTy, const char *Name) {
1361   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1362 }
1363
1364 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1365                              LLVMTypeRef DestTy, const char *Name) {
1366   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1367 }
1368
1369 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1370                              LLVMTypeRef DestTy, const char *Name) {
1371   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1372 }
1373
1374 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1375                              LLVMTypeRef DestTy, const char *Name) {
1376   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1377 }
1378
1379 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1380                              LLVMTypeRef DestTy, const char *Name) {
1381   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1382 }
1383
1384 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1385                               LLVMTypeRef DestTy, const char *Name) {
1386   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1387 }
1388
1389 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1390                             LLVMTypeRef DestTy, const char *Name) {
1391   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1392 }
1393
1394 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1395                                LLVMTypeRef DestTy, const char *Name) {
1396   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1397 }
1398
1399 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1400                                LLVMTypeRef DestTy, const char *Name) {
1401   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1402 }
1403
1404 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1405                               LLVMTypeRef DestTy, const char *Name) {
1406   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1407 }
1408
1409 /*--.. Comparisons .........................................................--*/
1410
1411 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1412                            LLVMValueRef LHS, LLVMValueRef RHS,
1413                            const char *Name) {
1414   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1415                                     unwrap(LHS), unwrap(RHS), Name));
1416 }
1417
1418 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1419                            LLVMValueRef LHS, LLVMValueRef RHS,
1420                            const char *Name) {
1421   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1422                                     unwrap(LHS), unwrap(RHS), Name));
1423 }
1424
1425 /*--.. Miscellaneous instructions ..........................................--*/
1426
1427 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1428   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1429 }
1430
1431 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1432                            LLVMValueRef *Args, unsigned NumArgs,
1433                            const char *Name) {
1434   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1435                                     unwrap(Args) + NumArgs, Name));
1436 }
1437
1438 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1439                              LLVMValueRef Then, LLVMValueRef Else,
1440                              const char *Name) {
1441   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1442                                       Name));
1443 }
1444
1445 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1446                             LLVMTypeRef Ty, const char *Name) {
1447   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1448 }
1449
1450 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1451                                       LLVMValueRef Index, const char *Name) {
1452   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1453                                               Name));
1454 }
1455
1456 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1457                                     LLVMValueRef EltVal, LLVMValueRef Index,
1458                                     const char *Name) {
1459   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1460                                              unwrap(Index), Name));
1461 }
1462
1463 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1464                                     LLVMValueRef V2, LLVMValueRef Mask,
1465                                     const char *Name) {
1466   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1467                                              unwrap(Mask), Name));
1468 }
1469
1470 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1471                                    unsigned Index, const char *Name) {
1472   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1473 }
1474
1475 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1476                                   LLVMValueRef EltVal, unsigned Index,
1477                                   const char *Name) {
1478   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1479                                            Index, Name));
1480 }
1481
1482
1483 /*===-- Module providers --------------------------------------------------===*/
1484
1485 LLVMModuleProviderRef
1486 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1487   return wrap(new ExistingModuleProvider(unwrap(M)));
1488 }
1489
1490 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1491   delete unwrap(MP);
1492 }
1493
1494
1495 /*===-- Memory buffers ----------------------------------------------------===*/
1496
1497 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1498                                              LLVMMemoryBufferRef *OutMemBuf,
1499                                              char **OutMessage) {
1500   std::string Error;
1501   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1502     *OutMemBuf = wrap(MB);
1503     return 0;
1504   }
1505   
1506   *OutMessage = strdup(Error.c_str());
1507   return 1;
1508 }
1509
1510 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1511                                     char **OutMessage) {
1512   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1513     *OutMemBuf = wrap(MB);
1514     return 0;
1515   }
1516   
1517   *OutMessage = strdup("stdin is empty.");
1518   return 1;
1519 }
1520
1521 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1522   delete unwrap(MemBuf);
1523 }