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