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