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