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