Use simplified ConstantFP::get method, fix a bug handling frem x, 0 with long doubles.
[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(APN));
311 }
312
313 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
314   return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
315 }
316
317 /*--.. Operations on composite constants ...................................--*/
318
319 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
320                              int DontNullTerminate) {
321   /* Inverted the sense of AddNull because ', 0)' is a
322      better mnemonic for null termination than ', 1)'. */
323   return wrap(ConstantArray::get(std::string(Str, Length),
324                                  DontNullTerminate == 0));
325 }
326
327 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
328                             LLVMValueRef *ConstantVals, unsigned Length) {
329   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
330                                  unwrap<Constant>(ConstantVals, Length),
331                                  Length));
332 }
333
334 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
335                              int Packed) {
336   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
337                                   Count, Packed != 0));
338 }
339
340 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
341   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
342                                   Size));
343 }
344
345 /*--.. Constant expressions ................................................--*/
346
347 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
348   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
349 }
350
351 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
352   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
353 }
354
355 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
356   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
357 }
358
359 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
360   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
361                                    unwrap<Constant>(RHSConstant)));
362 }
363
364 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
365   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
366                                    unwrap<Constant>(RHSConstant)));
367 }
368
369 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
370   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
371                                    unwrap<Constant>(RHSConstant)));
372 }
373
374 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
375   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
376                                     unwrap<Constant>(RHSConstant)));
377 }
378
379 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
380   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
381                                     unwrap<Constant>(RHSConstant)));
382 }
383
384 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
385   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
386                                     unwrap<Constant>(RHSConstant)));
387 }
388
389 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
390   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
391                                     unwrap<Constant>(RHSConstant)));
392 }
393
394 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
395   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
396                                     unwrap<Constant>(RHSConstant)));
397 }
398
399 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
400   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
401                                     unwrap<Constant>(RHSConstant)));
402 }
403
404 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
405   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
406                                    unwrap<Constant>(RHSConstant)));
407 }
408
409 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
410   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
411                                   unwrap<Constant>(RHSConstant)));
412 }
413
414 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
415   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
416                                    unwrap<Constant>(RHSConstant)));
417 }
418
419 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
420                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
421   return wrap(ConstantExpr::getICmp(Predicate,
422                                     unwrap<Constant>(LHSConstant),
423                                     unwrap<Constant>(RHSConstant)));
424 }
425
426 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
427                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
428   return wrap(ConstantExpr::getFCmp(Predicate,
429                                     unwrap<Constant>(LHSConstant),
430                                     unwrap<Constant>(RHSConstant)));
431 }
432
433 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
434   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
435                                   unwrap<Constant>(RHSConstant)));
436 }
437
438 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
439   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
440                                     unwrap<Constant>(RHSConstant)));
441 }
442
443 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
444   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
445                                     unwrap<Constant>(RHSConstant)));
446 }
447
448 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
449                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
450   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
451                                              unwrap<Constant>(ConstantIndices, 
452                                                               NumIndices),
453                                              NumIndices));
454 }
455
456 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
457   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
458                                      unwrap(ToType)));
459 }
460
461 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
462   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
463                                     unwrap(ToType)));
464 }
465
466 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
467   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
468                                     unwrap(ToType)));
469 }
470
471 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
472   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
473                                        unwrap(ToType)));
474 }
475
476 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
477   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
478                                         unwrap(ToType)));
479 }
480
481 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
482   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
483                                       unwrap(ToType)));
484 }
485
486 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
487   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
488                                       unwrap(ToType)));
489 }
490
491 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
492   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
493                                       unwrap(ToType)));
494 }
495
496 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
497   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
498                                       unwrap(ToType)));
499 }
500
501 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
502   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
503                                         unwrap(ToType)));
504 }
505
506 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
507   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
508                                         unwrap(ToType)));
509 }
510
511 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
512   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
513                                        unwrap(ToType)));
514 }
515
516 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
517                              LLVMValueRef ConstantIfTrue,
518                              LLVMValueRef ConstantIfFalse) {
519   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
520                                       unwrap<Constant>(ConstantIfTrue),
521                                       unwrap<Constant>(ConstantIfFalse)));
522 }
523
524 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
525                                      LLVMValueRef IndexConstant) {
526   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
527                                               unwrap<Constant>(IndexConstant)));
528 }
529
530 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
531                                     LLVMValueRef ElementValueConstant,
532                                     LLVMValueRef IndexConstant) {
533   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
534                                          unwrap<Constant>(ElementValueConstant),
535                                              unwrap<Constant>(IndexConstant)));
536 }
537
538 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
539                                     LLVMValueRef VectorBConstant,
540                                     LLVMValueRef MaskConstant) {
541   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
542                                              unwrap<Constant>(VectorBConstant),
543                                              unwrap<Constant>(MaskConstant)));
544 }
545
546 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
547
548 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
549   return wrap(unwrap<GlobalValue>(Global)->getParent());
550 }
551
552 int LLVMIsDeclaration(LLVMValueRef Global) {
553   return unwrap<GlobalValue>(Global)->isDeclaration();
554 }
555
556 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
557   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
558 }
559
560 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
561   unwrap<GlobalValue>(Global)
562     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
563 }
564
565 const char *LLVMGetSection(LLVMValueRef Global) {
566   return unwrap<GlobalValue>(Global)->getSection().c_str();
567 }
568
569 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
570   unwrap<GlobalValue>(Global)->setSection(Section);
571 }
572
573 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
574   return static_cast<LLVMVisibility>(
575     unwrap<GlobalValue>(Global)->getVisibility());
576 }
577
578 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
579   unwrap<GlobalValue>(Global)
580     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
581 }
582
583 unsigned LLVMGetAlignment(LLVMValueRef Global) {
584   return unwrap<GlobalValue>(Global)->getAlignment();
585 }
586
587 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
588   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
589 }
590
591 /*--.. Operations on global variables ......................................--*/
592
593 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
594   return wrap(new GlobalVariable(unwrap(Ty), false,
595                                  GlobalValue::ExternalLinkage, 0, Name,
596                                  unwrap(M)));
597 }
598
599 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
600   return wrap(unwrap(M)->getNamedGlobal(Name));
601 }
602
603 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
604   Module *Mod = unwrap(M);
605   Module::global_iterator I = Mod->global_begin();
606   if (I == Mod->global_end())
607     return 0;
608   return wrap(I);
609 }
610
611 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
612   Module *Mod = unwrap(M);
613   Module::global_iterator I = Mod->global_end();
614   if (I == Mod->global_begin())
615     return 0;
616   return wrap(--I);
617 }
618
619 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
620   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
621   Module::global_iterator I = GV;
622   if (++I == GV->getParent()->global_end())
623     return 0;
624   return wrap(I);
625 }
626
627 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
628   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
629   Module::global_iterator I = GV;
630   if (I == GV->getParent()->global_begin())
631     return 0;
632   return wrap(--I);
633 }
634
635 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
636   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
637 }
638
639 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
640   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
641 }
642
643 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
644   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
645 }
646
647 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
648   unwrap<GlobalVariable>(GlobalVar)
649     ->setInitializer(unwrap<Constant>(ConstantVal));
650 }
651
652 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
653   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
654 }
655
656 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
657   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
658 }
659
660 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
661   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
662 }
663
664 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
665   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
666 }
667
668 /*--.. Operations on functions .............................................--*/
669
670 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
671                              LLVMTypeRef FunctionTy) {
672   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
673                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
674 }
675
676 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
677   return wrap(unwrap(M)->getFunction(Name));
678 }
679
680 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
681   Module *Mod = unwrap(M);
682   Module::iterator I = Mod->begin();
683   if (I == Mod->end())
684     return 0;
685   return wrap(I);
686 }
687
688 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
689   Module *Mod = unwrap(M);
690   Module::iterator I = Mod->end();
691   if (I == Mod->begin())
692     return 0;
693   return wrap(--I);
694 }
695
696 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
697   Function *Func = unwrap<Function>(Fn);
698   Module::iterator I = Func;
699   if (++I == Func->getParent()->end())
700     return 0;
701   return wrap(I);
702 }
703
704 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
705   Function *Func = unwrap<Function>(Fn);
706   Module::iterator I = Func;
707   if (I == Func->getParent()->begin())
708     return 0;
709   return wrap(--I);
710 }
711
712 void LLVMDeleteFunction(LLVMValueRef Fn) {
713   unwrap<Function>(Fn)->eraseFromParent();
714 }
715
716 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
717   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
718     return F->getIntrinsicID();
719   return 0;
720 }
721
722 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
723   return unwrap<Function>(Fn)->getCallingConv();
724 }
725
726 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
727   return unwrap<Function>(Fn)->setCallingConv(CC);
728 }
729
730 const char *LLVMGetCollector(LLVMValueRef Fn) {
731   Function *F = unwrap<Function>(Fn);
732   return F->hasCollector()? F->getCollector() : 0;
733 }
734
735 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
736   Function *F = unwrap<Function>(Fn);
737   if (Coll)
738     F->setCollector(Coll);
739   else
740     F->clearCollector();
741 }
742
743 /*--.. Operations on parameters ............................................--*/
744
745 unsigned LLVMCountParams(LLVMValueRef FnRef) {
746   // This function is strictly redundant to
747   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
748   return unwrap<Function>(FnRef)->getArgumentList().size();
749 }
750
751 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
752   Function *Fn = unwrap<Function>(FnRef);
753   for (Function::arg_iterator I = Fn->arg_begin(),
754                               E = Fn->arg_end(); I != E; I++)
755     *ParamRefs++ = wrap(I);
756 }
757
758 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
759   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
760   while (index --> 0)
761     AI++;
762   return wrap(AI);
763 }
764
765 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
766   return wrap(unwrap<Argument>(V)->getParent());
767 }
768
769 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
770   Function *Func = unwrap<Function>(Fn);
771   Function::arg_iterator I = Func->arg_begin();
772   if (I == Func->arg_end())
773     return 0;
774   return wrap(I);
775 }
776
777 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
778   Function *Func = unwrap<Function>(Fn);
779   Function::arg_iterator I = Func->arg_end();
780   if (I == Func->arg_begin())
781     return 0;
782   return wrap(--I);
783 }
784
785 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
786   Argument *A = unwrap<Argument>(Arg);
787   Function::arg_iterator I = A;
788   if (++I == A->getParent()->arg_end())
789     return 0;
790   return wrap(I);
791 }
792
793 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
794   Argument *A = unwrap<Argument>(Arg);
795   Function::arg_iterator I = A;
796   if (I == A->getParent()->arg_begin())
797     return 0;
798   return wrap(--I);
799 }
800
801 /*--.. Operations on basic blocks ..........................................--*/
802
803 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
804   return wrap(static_cast<Value*>(unwrap(BB)));
805 }
806
807 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
808   return isa<BasicBlock>(unwrap(Val));
809 }
810
811 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
812   return wrap(unwrap<BasicBlock>(Val));
813 }
814
815 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
816   return wrap(unwrap(BB)->getParent());
817 }
818
819 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
820   return unwrap<Function>(FnRef)->getBasicBlockList().size();
821 }
822
823 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
824   Function *Fn = unwrap<Function>(FnRef);
825   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
826     *BasicBlocksRefs++ = wrap(I);
827 }
828
829 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
830   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
831 }
832
833 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
834   Function *Func = unwrap<Function>(Fn);
835   Function::iterator I = Func->begin();
836   if (I == Func->end())
837     return 0;
838   return wrap(I);
839 }
840
841 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
842   Function *Func = unwrap<Function>(Fn);
843   Function::iterator I = Func->end();
844   if (I == Func->begin())
845     return 0;
846   return wrap(--I);
847 }
848
849 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
850   BasicBlock *Block = unwrap(BB);
851   Function::iterator I = Block;
852   if (++I == Block->getParent()->end())
853     return 0;
854   return wrap(I);
855 }
856
857 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
858   BasicBlock *Block = unwrap(BB);
859   Function::iterator I = Block;
860   if (I == Block->getParent()->begin())
861     return 0;
862   return wrap(--I);
863 }
864
865 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
866   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
867 }
868
869 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
870                                        const char *Name) {
871   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
872   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
873                                  InsertBeforeBB));
874 }
875
876 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
877   unwrap(BBRef)->eraseFromParent();
878 }
879
880 /*--.. Operations on instructions ..........................................--*/
881
882 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
883   return wrap(unwrap<Instruction>(Inst)->getParent());
884 }
885
886 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
887   BasicBlock *Block = unwrap(BB);
888   BasicBlock::iterator I = Block->begin();
889   if (I == Block->end())
890     return 0;
891   return wrap(I);
892 }
893
894 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
895   BasicBlock *Block = unwrap(BB);
896   BasicBlock::iterator I = Block->end();
897   if (I == Block->begin())
898     return 0;
899   return wrap(--I);
900 }
901
902 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
903   Instruction *Instr = unwrap<Instruction>(Inst);
904   BasicBlock::iterator I = Instr;
905   if (++I == Instr->getParent()->end())
906     return 0;
907   return wrap(I);
908 }
909
910 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
911   Instruction *Instr = unwrap<Instruction>(Inst);
912   BasicBlock::iterator I = Instr;
913   if (I == Instr->getParent()->begin())
914     return 0;
915   return wrap(--I);
916 }
917
918 /*--.. Call and invoke instructions ........................................--*/
919
920 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
921   Value *V = unwrap(Instr);
922   if (CallInst *CI = dyn_cast<CallInst>(V))
923     return CI->getCallingConv();
924   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
925     return II->getCallingConv();
926   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
927   return 0;
928 }
929
930 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
931   Value *V = unwrap(Instr);
932   if (CallInst *CI = dyn_cast<CallInst>(V))
933     return CI->setCallingConv(CC);
934   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
935     return II->setCallingConv(CC);
936   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
937 }
938
939 /*--.. Operations on phi nodes .............................................--*/
940
941 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
942                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
943   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
944   for (unsigned I = 0; I != Count; ++I)
945     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
946 }
947
948 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
949   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
950 }
951
952 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
953   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
954 }
955
956 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
957   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
958 }
959
960
961 /*===-- Instruction builders ----------------------------------------------===*/
962
963 LLVMBuilderRef LLVMCreateBuilder() {
964   return wrap(new IRBuilder());
965 }
966
967 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
968                          LLVMValueRef Instr) {
969   BasicBlock *BB = unwrap(Block);
970   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
971   unwrap(Builder)->SetInsertPoint(BB, I);
972 }
973
974 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
975   Instruction *I = unwrap<Instruction>(Instr);
976   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
977 }
978
979 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
980   BasicBlock *BB = unwrap(Block);
981   unwrap(Builder)->SetInsertPoint(BB);
982 }
983
984 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
985    return wrap(unwrap(Builder)->GetInsertBlock());
986 }
987
988 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
989   delete unwrap(Builder);
990 }
991
992 /*--.. Instruction builders ................................................--*/
993
994 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
995   return wrap(unwrap(B)->CreateRetVoid());
996 }
997
998 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
999   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1000 }
1001
1002 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1003   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1004 }
1005
1006 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1007                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1008   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1009 }
1010
1011 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1012                              LLVMBasicBlockRef Else, unsigned NumCases) {
1013   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1014 }
1015
1016 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1017                              LLVMValueRef *Args, unsigned NumArgs,
1018                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1019                              const char *Name) {
1020   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1021                                       unwrap(Args), unwrap(Args) + NumArgs,
1022                                       Name));
1023 }
1024
1025 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1026   return wrap(unwrap(B)->CreateUnwind());
1027 }
1028
1029 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1030   return wrap(unwrap(B)->CreateUnreachable());
1031 }
1032
1033 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1034                  LLVMBasicBlockRef Dest) {
1035   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1036 }
1037
1038 /*--.. Arithmetic ..........................................................--*/
1039
1040 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1041                           const char *Name) {
1042   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1043 }
1044
1045 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1046                           const char *Name) {
1047   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1048 }
1049
1050 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1051                           const char *Name) {
1052   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1053 }
1054
1055 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1056                            const char *Name) {
1057   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1058 }
1059
1060 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1061                            const char *Name) {
1062   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1063 }
1064
1065 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1066                            const char *Name) {
1067   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1068 }
1069
1070 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1071                            const char *Name) {
1072   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1073 }
1074
1075 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1076                            const char *Name) {
1077   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1078 }
1079
1080 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1081                            const char *Name) {
1082   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1083 }
1084
1085 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1086                           const char *Name) {
1087   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1088 }
1089
1090 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1091                            const char *Name) {
1092   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1093 }
1094
1095 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1096                            const char *Name) {
1097   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1098 }
1099
1100 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1101                           const char *Name) {
1102   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1103 }
1104
1105 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1106                          const char *Name) {
1107   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1108 }
1109
1110 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1111                           const char *Name) {
1112   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1113 }
1114
1115 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1116   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1117 }
1118
1119 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1120   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1121 }
1122
1123 /*--.. Memory ..............................................................--*/
1124
1125 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1126                              const char *Name) {
1127   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1128 }
1129
1130 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1131                                   LLVMValueRef Val, const char *Name) {
1132   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1133 }
1134
1135 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1136                              const char *Name) {
1137   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1138 }
1139
1140 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1141                                   LLVMValueRef Val, const char *Name) {
1142   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1143 }
1144
1145 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1146   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1147 }
1148
1149
1150 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1151                            const char *Name) {
1152   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1153 }
1154
1155 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1156                             LLVMValueRef PointerVal) {
1157   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1158 }
1159
1160 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1161                           LLVMValueRef *Indices, unsigned NumIndices,
1162                           const char *Name) {
1163   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1164                                    unwrap(Indices) + NumIndices, Name));
1165 }
1166
1167 /*--.. Casts ...............................................................--*/
1168
1169 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1170                             LLVMTypeRef DestTy, const char *Name) {
1171   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1172 }
1173
1174 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1175                            LLVMTypeRef DestTy, const char *Name) {
1176   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1177 }
1178
1179 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1180                            LLVMTypeRef DestTy, const char *Name) {
1181   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1182 }
1183
1184 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1185                              LLVMTypeRef DestTy, const char *Name) {
1186   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1187 }
1188
1189 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1190                              LLVMTypeRef DestTy, const char *Name) {
1191   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1192 }
1193
1194 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1195                              LLVMTypeRef DestTy, const char *Name) {
1196   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1197 }
1198
1199 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1200                              LLVMTypeRef DestTy, const char *Name) {
1201   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1202 }
1203
1204 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1205                               LLVMTypeRef DestTy, const char *Name) {
1206   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1207 }
1208
1209 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1210                             LLVMTypeRef DestTy, const char *Name) {
1211   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1212 }
1213
1214 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1215                                LLVMTypeRef DestTy, const char *Name) {
1216   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1217 }
1218
1219 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1220                                LLVMTypeRef DestTy, const char *Name) {
1221   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1222 }
1223
1224 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1225                               LLVMTypeRef DestTy, const char *Name) {
1226   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1227 }
1228
1229 /*--.. Comparisons .........................................................--*/
1230
1231 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1232                            LLVMValueRef LHS, LLVMValueRef RHS,
1233                            const char *Name) {
1234   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1235                                     unwrap(LHS), unwrap(RHS), Name));
1236 }
1237
1238 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1239                            LLVMValueRef LHS, LLVMValueRef RHS,
1240                            const char *Name) {
1241   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1242                                     unwrap(LHS), unwrap(RHS), Name));
1243 }
1244
1245 /*--.. Miscellaneous instructions ..........................................--*/
1246
1247 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1248   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1249 }
1250
1251 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1252                            LLVMValueRef *Args, unsigned NumArgs,
1253                            const char *Name) {
1254   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1255                                     unwrap(Args) + NumArgs, Name));
1256 }
1257
1258 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1259                              LLVMValueRef Then, LLVMValueRef Else,
1260                              const char *Name) {
1261   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1262                                       Name));
1263 }
1264
1265 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1266                             LLVMTypeRef Ty, const char *Name) {
1267   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1268 }
1269
1270 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1271                                       LLVMValueRef Index, const char *Name) {
1272   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1273                                               Name));
1274 }
1275
1276 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1277                                     LLVMValueRef EltVal, LLVMValueRef Index,
1278                                     const char *Name) {
1279   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1280                                              unwrap(Index), Name));
1281 }
1282
1283 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1284                                     LLVMValueRef V2, LLVMValueRef Mask,
1285                                     const char *Name) {
1286   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1287                                              unwrap(Mask), Name));
1288 }
1289
1290
1291 /*===-- Module providers --------------------------------------------------===*/
1292
1293 LLVMModuleProviderRef
1294 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1295   return wrap(new ExistingModuleProvider(unwrap(M)));
1296 }
1297
1298 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1299   delete unwrap(MP);
1300 }
1301
1302
1303 /*===-- Memory buffers ----------------------------------------------------===*/
1304
1305 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1306                                              LLVMMemoryBufferRef *OutMemBuf,
1307                                              char **OutMessage) {
1308   std::string Error;
1309   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1310     *OutMemBuf = wrap(MB);
1311     return 0;
1312   }
1313   
1314   *OutMessage = strdup(Error.c_str());
1315   return 1;
1316 }
1317
1318 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1319                                     char **OutMessage) {
1320   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1321     *OutMemBuf = wrap(MB);
1322     return 0;
1323   }
1324   
1325   *OutMessage = strdup("stdin is empty.");
1326   return 1;
1327 }
1328
1329 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1330   delete unwrap(MemBuf);
1331 }