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