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