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