Expose parameter attributes via C bindings.
[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 "llvm/Support/CallSite.h"
24 #include <cassert>
25 #include <cstdlib>
26 #include <cstring>
27
28 using namespace llvm;
29
30
31 /*===-- Error handling ----------------------------------------------------===*/
32
33 void LLVMDisposeMessage(char *Message) {
34   free(Message);
35 }
36
37
38 /*===-- Operations on modules ---------------------------------------------===*/
39
40 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
41   return wrap(new Module(ModuleID));
42 }
43
44 void LLVMDisposeModule(LLVMModuleRef M) {
45   delete unwrap(M);
46 }
47
48 /*--.. Data layout .........................................................--*/
49 const char * LLVMGetDataLayout(LLVMModuleRef M) {
50   return unwrap(M)->getDataLayout().c_str();
51 }
52
53 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
54   unwrap(M)->setDataLayout(Triple);
55 }
56
57 /*--.. Target triple .......................................................--*/
58 const char * LLVMGetTarget(LLVMModuleRef M) {
59   return unwrap(M)->getTargetTriple().c_str();
60 }
61
62 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
63   unwrap(M)->setTargetTriple(Triple);
64 }
65
66 /*--.. Type names ..........................................................--*/
67 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
68   return unwrap(M)->addTypeName(Name, unwrap(Ty));
69 }
70
71 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
72   std::string N(Name);
73   
74   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
75   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
76     if (I->first == N)
77       TST.remove(I);
78 }
79
80 void LLVMDumpModule(LLVMModuleRef M) {
81   unwrap(M)->dump();
82 }
83
84
85 /*===-- Operations on types -----------------------------------------------===*/
86
87 /*--.. Operations on all types (mostly) ....................................--*/
88
89 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
90   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
91 }
92
93 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
94   DerivedType *Ty = unwrap<DerivedType>(AbstractType);
95   Ty->refineAbstractTypeTo(unwrap(ConcreteType));
96 }
97
98 /*--.. Operations on integer types .........................................--*/
99
100 LLVMTypeRef LLVMInt1Type()  { return (LLVMTypeRef) Type::Int1Ty;  }
101 LLVMTypeRef LLVMInt8Type()  { return (LLVMTypeRef) Type::Int8Ty;  }
102 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
103 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
104 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
105
106 LLVMTypeRef LLVMIntType(unsigned NumBits) {
107   return wrap(IntegerType::get(NumBits));
108 }
109
110 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
111   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
112 }
113
114 /*--.. Operations on real types ............................................--*/
115
116 LLVMTypeRef LLVMFloatType()    { return (LLVMTypeRef) Type::FloatTy;     }
117 LLVMTypeRef LLVMDoubleType()   { return (LLVMTypeRef) Type::DoubleTy;    }
118 LLVMTypeRef LLVMX86FP80Type()  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
119 LLVMTypeRef LLVMFP128Type()    { return (LLVMTypeRef) Type::FP128Ty;     }
120 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
121
122 /*--.. Operations on function types ........................................--*/
123
124 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
125                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
126                              int IsVarArg) {
127   std::vector<const Type*> Tys;
128   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
129     Tys.push_back(unwrap(*I));
130   
131   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
132 }
133
134 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
135   return unwrap<FunctionType>(FunctionTy)->isVarArg();
136 }
137
138 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
139   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
140 }
141
142 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
143   return unwrap<FunctionType>(FunctionTy)->getNumParams();
144 }
145
146 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
147   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
148   for (FunctionType::param_iterator I = Ty->param_begin(),
149                                     E = Ty->param_end(); I != E; ++I)
150     *Dest++ = wrap(*I);
151 }
152
153 /*--.. Operations on struct types ..........................................--*/
154
155 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
156                            unsigned ElementCount, int Packed) {
157   std::vector<const Type*> Tys;
158   for (LLVMTypeRef *I = ElementTypes,
159                    *E = ElementTypes + ElementCount; I != E; ++I)
160     Tys.push_back(unwrap(*I));
161   
162   return wrap(StructType::get(Tys, Packed != 0));
163 }
164
165 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
166   return unwrap<StructType>(StructTy)->getNumElements();
167 }
168
169 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
170   StructType *Ty = unwrap<StructType>(StructTy);
171   for (FunctionType::param_iterator I = Ty->element_begin(),
172                                     E = Ty->element_end(); I != E; ++I)
173     *Dest++ = wrap(*I);
174 }
175
176 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
177   return unwrap<StructType>(StructTy)->isPacked();
178 }
179
180 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
181
182 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
183   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
184 }
185
186 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
187   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
188 }
189
190 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
191   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
192 }
193
194 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
195   return wrap(unwrap<SequentialType>(Ty)->getElementType());
196 }
197
198 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
199   return unwrap<ArrayType>(ArrayTy)->getNumElements();
200 }
201
202 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
203   return unwrap<PointerType>(PointerTy)->getAddressSpace();
204 }
205
206 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
207   return unwrap<VectorType>(VectorTy)->getNumElements();
208 }
209
210 /*--.. Operations on other types ...........................................--*/
211
212 LLVMTypeRef LLVMVoidType()  { return (LLVMTypeRef) Type::VoidTy;  }
213 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
214
215 LLVMTypeRef LLVMOpaqueType() {
216   return wrap(llvm::OpaqueType::get());
217 }
218
219 /*--.. Operations on type handles ..........................................--*/
220
221 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
222   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
223 }
224
225 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
226   delete unwrap(TypeHandle);
227 }
228
229 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
230   return wrap(unwrap(TypeHandle)->get());
231 }
232
233 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
234   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
235 }
236
237
238 /*===-- Operations on values ----------------------------------------------===*/
239
240 /*--.. Operations on all values ............................................--*/
241
242 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
243   return wrap(unwrap(Val)->getType());
244 }
245
246 const char *LLVMGetValueName(LLVMValueRef Val) {
247   return unwrap(Val)->getNameStart();
248 }
249
250 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
251   unwrap(Val)->setName(Name);
252 }
253
254 void LLVMDumpValue(LLVMValueRef Val) {
255   unwrap(Val)->dump();
256 }
257
258 /*--.. Operations on constants of any type .................................--*/
259
260 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
261   return wrap(Constant::getNullValue(unwrap(Ty)));
262 }
263
264 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
265   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
266 }
267
268 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
269   return wrap(UndefValue::get(unwrap(Ty)));
270 }
271
272 int LLVMIsConstant(LLVMValueRef Ty) {
273   return isa<Constant>(unwrap(Ty));
274 }
275
276 int LLVMIsNull(LLVMValueRef Val) {
277   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
278     return C->isNullValue();
279   return false;
280 }
281
282 int LLVMIsUndef(LLVMValueRef Val) {
283   return isa<UndefValue>(unwrap(Val));
284 }
285
286 /*--.. Operations on scalar constants ......................................--*/
287
288 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
289                           int SignExtend) {
290   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
291 }
292
293 static const fltSemantics &SemanticsForType(Type *Ty) {
294   assert(Ty->isFloatingPoint() && "Type is not floating point!");
295   if (Ty == Type::FloatTy)
296     return APFloat::IEEEsingle;
297   if (Ty == Type::DoubleTy)
298     return APFloat::IEEEdouble;
299   if (Ty == Type::X86_FP80Ty)
300     return APFloat::x87DoubleExtended;
301   if (Ty == Type::FP128Ty)
302     return APFloat::IEEEquad;
303   if (Ty == Type::PPC_FP128Ty)
304     return APFloat::PPCDoubleDouble;
305   return APFloat::Bogus;
306 }
307
308 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
309   APFloat APN(N);
310   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
311   return wrap(ConstantFP::get(APN));
312 }
313
314 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
315   return wrap(ConstantFP::get(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_begin())
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(Function::Create(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()->begin())
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 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
771   Function *Func = unwrap<Function>(Fn);
772   Function::arg_iterator I = Func->arg_begin();
773   if (I == Func->arg_end())
774     return 0;
775   return wrap(I);
776 }
777
778 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
779   Function *Func = unwrap<Function>(Fn);
780   Function::arg_iterator I = Func->arg_end();
781   if (I == Func->arg_begin())
782     return 0;
783   return wrap(--I);
784 }
785
786 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
787   Argument *A = unwrap<Argument>(Arg);
788   Function::arg_iterator I = A;
789   if (++I == A->getParent()->arg_end())
790     return 0;
791   return wrap(I);
792 }
793
794 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
795   Argument *A = unwrap<Argument>(Arg);
796   Function::arg_iterator I = A;
797   if (I == A->getParent()->arg_begin())
798     return 0;
799   return wrap(--I);
800 }
801
802 void LLVMAddParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
803   unwrap<Argument>(Arg)->addAttr(PA);
804 }
805
806 void LLVMRemoveParamAttr(LLVMValueRef Arg, LLVMParamAttr PA) {
807   unwrap<Argument>(Arg)->removeAttr(PA);
808 }
809
810 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
811   unwrap<Argument>(Arg)->addAttr(
812           ParamAttr::constructAlignmentFromInt(align));
813 }
814
815 /*--.. Operations on basic blocks ..........................................--*/
816
817 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
818   return wrap(static_cast<Value*>(unwrap(BB)));
819 }
820
821 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
822   return isa<BasicBlock>(unwrap(Val));
823 }
824
825 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
826   return wrap(unwrap<BasicBlock>(Val));
827 }
828
829 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
830   return wrap(unwrap(BB)->getParent());
831 }
832
833 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
834   return unwrap<Function>(FnRef)->getBasicBlockList().size();
835 }
836
837 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
838   Function *Fn = unwrap<Function>(FnRef);
839   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
840     *BasicBlocksRefs++ = wrap(I);
841 }
842
843 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
844   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
845 }
846
847 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
848   Function *Func = unwrap<Function>(Fn);
849   Function::iterator I = Func->begin();
850   if (I == Func->end())
851     return 0;
852   return wrap(I);
853 }
854
855 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
856   Function *Func = unwrap<Function>(Fn);
857   Function::iterator I = Func->end();
858   if (I == Func->begin())
859     return 0;
860   return wrap(--I);
861 }
862
863 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
864   BasicBlock *Block = unwrap(BB);
865   Function::iterator I = Block;
866   if (++I == Block->getParent()->end())
867     return 0;
868   return wrap(I);
869 }
870
871 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
872   BasicBlock *Block = unwrap(BB);
873   Function::iterator I = Block;
874   if (I == Block->getParent()->begin())
875     return 0;
876   return wrap(--I);
877 }
878
879 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
880   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
881 }
882
883 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
884                                        const char *Name) {
885   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
886   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
887                                  InsertBeforeBB));
888 }
889
890 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
891   unwrap(BBRef)->eraseFromParent();
892 }
893
894 /*--.. Operations on instructions ..........................................--*/
895
896 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
897   return wrap(unwrap<Instruction>(Inst)->getParent());
898 }
899
900 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
901   BasicBlock *Block = unwrap(BB);
902   BasicBlock::iterator I = Block->begin();
903   if (I == Block->end())
904     return 0;
905   return wrap(I);
906 }
907
908 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
909   BasicBlock *Block = unwrap(BB);
910   BasicBlock::iterator I = Block->end();
911   if (I == Block->begin())
912     return 0;
913   return wrap(--I);
914 }
915
916 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
917   Instruction *Instr = unwrap<Instruction>(Inst);
918   BasicBlock::iterator I = Instr;
919   if (++I == Instr->getParent()->end())
920     return 0;
921   return wrap(I);
922 }
923
924 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
925   Instruction *Instr = unwrap<Instruction>(Inst);
926   BasicBlock::iterator I = Instr;
927   if (I == Instr->getParent()->begin())
928     return 0;
929   return wrap(--I);
930 }
931
932 /*--.. Call and invoke instructions ........................................--*/
933
934 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
935   Value *V = unwrap(Instr);
936   if (CallInst *CI = dyn_cast<CallInst>(V))
937     return CI->getCallingConv();
938   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
939     return II->getCallingConv();
940   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
941   return 0;
942 }
943
944 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
945   Value *V = unwrap(Instr);
946   if (CallInst *CI = dyn_cast<CallInst>(V))
947     return CI->setCallingConv(CC);
948   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
949     return II->setCallingConv(CC);
950   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
951 }
952
953 void LLVMAddInstrParamAttr(LLVMValueRef Instr, unsigned index, 
954                            LLVMParamAttr PA) {
955   CallSite Call = CallSite(unwrap<Instruction>(Instr));
956   Call.setParamAttrs(
957     Call.getParamAttrs().addAttr(index, PA));
958 }
959
960 void LLVMRemoveInstrParamAttr(LLVMValueRef Instr, unsigned index, 
961                               LLVMParamAttr PA) {
962   CallSite Call = CallSite(unwrap<Instruction>(Instr));
963   Call.setParamAttrs(
964     Call.getParamAttrs().removeAttr(index, PA));
965 }
966
967 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
968                                 unsigned align) {
969   CallSite Call = CallSite(unwrap<Instruction>(Instr));
970   Call.setParamAttrs(
971     Call.getParamAttrs().addAttr(index, 
972         ParamAttr::constructAlignmentFromInt(align)));
973 }
974
975 /*--.. Operations on phi nodes .............................................--*/
976
977 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
978                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
979   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
980   for (unsigned I = 0; I != Count; ++I)
981     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
982 }
983
984 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
985   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
986 }
987
988 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
989   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
990 }
991
992 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
993   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
994 }
995
996
997 /*===-- Instruction builders ----------------------------------------------===*/
998
999 LLVMBuilderRef LLVMCreateBuilder() {
1000   return wrap(new IRBuilder());
1001 }
1002
1003 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1004                          LLVMValueRef Instr) {
1005   BasicBlock *BB = unwrap(Block);
1006   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1007   unwrap(Builder)->SetInsertPoint(BB, I);
1008 }
1009
1010 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1011   Instruction *I = unwrap<Instruction>(Instr);
1012   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1013 }
1014
1015 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1016   BasicBlock *BB = unwrap(Block);
1017   unwrap(Builder)->SetInsertPoint(BB);
1018 }
1019
1020 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1021    return wrap(unwrap(Builder)->GetInsertBlock());
1022 }
1023
1024 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1025   delete unwrap(Builder);
1026 }
1027
1028 /*--.. Instruction builders ................................................--*/
1029
1030 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1031   return wrap(unwrap(B)->CreateRetVoid());
1032 }
1033
1034 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1035   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1036 }
1037
1038 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1039   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1040 }
1041
1042 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1043                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1044   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1045 }
1046
1047 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1048                              LLVMBasicBlockRef Else, unsigned NumCases) {
1049   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1050 }
1051
1052 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1053                              LLVMValueRef *Args, unsigned NumArgs,
1054                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1055                              const char *Name) {
1056   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1057                                       unwrap(Args), unwrap(Args) + NumArgs,
1058                                       Name));
1059 }
1060
1061 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1062   return wrap(unwrap(B)->CreateUnwind());
1063 }
1064
1065 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1066   return wrap(unwrap(B)->CreateUnreachable());
1067 }
1068
1069 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1070                  LLVMBasicBlockRef Dest) {
1071   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1072 }
1073
1074 /*--.. Arithmetic ..........................................................--*/
1075
1076 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1077                           const char *Name) {
1078   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1079 }
1080
1081 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1082                           const char *Name) {
1083   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1084 }
1085
1086 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1087                           const char *Name) {
1088   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1089 }
1090
1091 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1092                            const char *Name) {
1093   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1094 }
1095
1096 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1097                            const char *Name) {
1098   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1099 }
1100
1101 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1102                            const char *Name) {
1103   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1104 }
1105
1106 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1107                            const char *Name) {
1108   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1109 }
1110
1111 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1112                            const char *Name) {
1113   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1114 }
1115
1116 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1117                            const char *Name) {
1118   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1119 }
1120
1121 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1122                           const char *Name) {
1123   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1124 }
1125
1126 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1127                            const char *Name) {
1128   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1129 }
1130
1131 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1132                            const char *Name) {
1133   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1134 }
1135
1136 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1137                           const char *Name) {
1138   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1139 }
1140
1141 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1142                          const char *Name) {
1143   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1144 }
1145
1146 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1147                           const char *Name) {
1148   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1149 }
1150
1151 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1152   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1153 }
1154
1155 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1156   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1157 }
1158
1159 /*--.. Memory ..............................................................--*/
1160
1161 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1162                              const char *Name) {
1163   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1164 }
1165
1166 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1167                                   LLVMValueRef Val, const char *Name) {
1168   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1169 }
1170
1171 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1172                              const char *Name) {
1173   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1174 }
1175
1176 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1177                                   LLVMValueRef Val, const char *Name) {
1178   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1179 }
1180
1181 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1182   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1183 }
1184
1185
1186 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1187                            const char *Name) {
1188   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1189 }
1190
1191 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1192                             LLVMValueRef PointerVal) {
1193   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1194 }
1195
1196 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1197                           LLVMValueRef *Indices, unsigned NumIndices,
1198                           const char *Name) {
1199   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1200                                    unwrap(Indices) + NumIndices, Name));
1201 }
1202
1203 /*--.. Casts ...............................................................--*/
1204
1205 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1206                             LLVMTypeRef DestTy, const char *Name) {
1207   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1208 }
1209
1210 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1211                            LLVMTypeRef DestTy, const char *Name) {
1212   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1213 }
1214
1215 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1216                            LLVMTypeRef DestTy, const char *Name) {
1217   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1218 }
1219
1220 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1221                              LLVMTypeRef DestTy, const char *Name) {
1222   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1223 }
1224
1225 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1226                              LLVMTypeRef DestTy, const char *Name) {
1227   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1228 }
1229
1230 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1231                              LLVMTypeRef DestTy, const char *Name) {
1232   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1233 }
1234
1235 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1236                              LLVMTypeRef DestTy, const char *Name) {
1237   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1238 }
1239
1240 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1241                               LLVMTypeRef DestTy, const char *Name) {
1242   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1243 }
1244
1245 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1246                             LLVMTypeRef DestTy, const char *Name) {
1247   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1248 }
1249
1250 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1251                                LLVMTypeRef DestTy, const char *Name) {
1252   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1253 }
1254
1255 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1256                                LLVMTypeRef DestTy, const char *Name) {
1257   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1258 }
1259
1260 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1261                               LLVMTypeRef DestTy, const char *Name) {
1262   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1263 }
1264
1265 /*--.. Comparisons .........................................................--*/
1266
1267 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1268                            LLVMValueRef LHS, LLVMValueRef RHS,
1269                            const char *Name) {
1270   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1271                                     unwrap(LHS), unwrap(RHS), Name));
1272 }
1273
1274 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1275                            LLVMValueRef LHS, LLVMValueRef RHS,
1276                            const char *Name) {
1277   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1278                                     unwrap(LHS), unwrap(RHS), Name));
1279 }
1280
1281 /*--.. Miscellaneous instructions ..........................................--*/
1282
1283 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1284   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1285 }
1286
1287 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1288                            LLVMValueRef *Args, unsigned NumArgs,
1289                            const char *Name) {
1290   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1291                                     unwrap(Args) + NumArgs, Name));
1292 }
1293
1294 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1295                              LLVMValueRef Then, LLVMValueRef Else,
1296                              const char *Name) {
1297   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1298                                       Name));
1299 }
1300
1301 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1302                             LLVMTypeRef Ty, const char *Name) {
1303   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1304 }
1305
1306 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1307                                       LLVMValueRef Index, const char *Name) {
1308   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1309                                               Name));
1310 }
1311
1312 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1313                                     LLVMValueRef EltVal, LLVMValueRef Index,
1314                                     const char *Name) {
1315   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1316                                              unwrap(Index), Name));
1317 }
1318
1319 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1320                                     LLVMValueRef V2, LLVMValueRef Mask,
1321                                     const char *Name) {
1322   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1323                                              unwrap(Mask), Name));
1324 }
1325
1326
1327 /*===-- Module providers --------------------------------------------------===*/
1328
1329 LLVMModuleProviderRef
1330 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1331   return wrap(new ExistingModuleProvider(unwrap(M)));
1332 }
1333
1334 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1335   delete unwrap(MP);
1336 }
1337
1338
1339 /*===-- Memory buffers ----------------------------------------------------===*/
1340
1341 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1342                                              LLVMMemoryBufferRef *OutMemBuf,
1343                                              char **OutMessage) {
1344   std::string Error;
1345   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1346     *OutMemBuf = wrap(MB);
1347     return 0;
1348   }
1349   
1350   *OutMessage = strdup(Error.c_str());
1351   return 1;
1352 }
1353
1354 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1355                                     char **OutMessage) {
1356   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1357     *OutMemBuf = wrap(MB);
1358     return 0;
1359   }
1360   
1361   *OutMessage = strdup("stdin is empty.");
1362   return 1;
1363 }
1364
1365 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1366   delete unwrap(MemBuf);
1367 }