Remove attribution from file headers, per discussion on llvmdev.
[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, unwrap(M)));
566 }
567
568 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
569   return wrap(unwrap(M)->getNamedGlobal(Name));
570 }
571
572 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
573   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
574 }
575
576 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
577   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
578 }
579
580 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
581   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
582 }
583
584 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
585   unwrap<GlobalVariable>(GlobalVar)
586     ->setInitializer(unwrap<Constant>(ConstantVal));
587 }
588
589 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
590   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
591 }
592
593 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
594   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
595 }
596
597 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
598   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
599 }
600
601 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
602   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
603 }
604
605 /*--.. Operations on functions .............................................--*/
606
607 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
608                              LLVMTypeRef FunctionTy) {
609   return wrap(new Function(unwrap<FunctionType>(FunctionTy),
610                            GlobalValue::ExternalLinkage, Name, unwrap(M)));
611 }
612
613 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
614   return wrap(unwrap(M)->getFunction(Name));
615 }
616
617 void LLVMDeleteFunction(LLVMValueRef Fn) {
618   unwrap<Function>(Fn)->eraseFromParent();
619 }
620
621 unsigned LLVMCountParams(LLVMValueRef FnRef) {
622   // This function is strictly redundant to
623   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
624   return unwrap<Function>(FnRef)->getArgumentList().size();
625 }
626
627 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
628   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
629   while (index --> 0)
630     AI++;
631   return wrap(AI);
632 }
633
634 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
635   Function *Fn = unwrap<Function>(FnRef);
636   for (Function::arg_iterator I = Fn->arg_begin(),
637                               E = Fn->arg_end(); I != E; I++)
638     *ParamRefs++ = wrap(I);
639 }
640
641 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
642   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
643     return F->getIntrinsicID();
644   return 0;
645 }
646
647 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
648   return unwrap<Function>(Fn)->getCallingConv();
649 }
650
651 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
652   return unwrap<Function>(Fn)->setCallingConv(CC);
653 }
654
655 const char *LLVMGetCollector(LLVMValueRef Fn) {
656   Function *F = unwrap<Function>(Fn);
657   return F->hasCollector()? F->getCollector() : 0;
658 }
659
660 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
661   Function *F = unwrap<Function>(Fn);
662   if (Coll)
663     F->setCollector(Coll);
664   else
665     F->clearCollector();
666 }
667
668 /*--.. Operations on basic blocks ..........................................--*/
669
670 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
671   return wrap(static_cast<Value*>(unwrap(Bb)));
672 }
673
674 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
675   return isa<BasicBlock>(unwrap(Val));
676 }
677
678 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
679   return wrap(unwrap<BasicBlock>(Val));
680 }
681
682 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
683   return unwrap<Function>(FnRef)->getBasicBlockList().size();
684 }
685
686 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
687   Function *Fn = unwrap<Function>(FnRef);
688   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
689     *BasicBlocksRefs++ = wrap(I);
690 }
691
692 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
693   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
694 }
695
696 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
697   return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
698 }
699
700 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
701                                        const char *Name) {
702   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
703   return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
704                              InsertBeforeBB));
705 }
706
707 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
708   unwrap(BBRef)->eraseFromParent();
709 }
710
711 /*--.. Call and invoke instructions ........................................--*/
712
713 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
714   Value *V = unwrap(Instr);
715   if (CallInst *CI = dyn_cast<CallInst>(V))
716     return CI->getCallingConv();
717   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
718     return II->getCallingConv();
719   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
720   return 0;
721 }
722
723 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
724   Value *V = unwrap(Instr);
725   if (CallInst *CI = dyn_cast<CallInst>(V))
726     return CI->setCallingConv(CC);
727   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
728     return II->setCallingConv(CC);
729   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
730 }
731
732 /*--.. Operations on phi nodes .............................................--*/
733
734 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
735                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
736   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
737   for (unsigned I = 0; I != Count; ++I)
738     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
739 }
740
741 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
742   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
743 }
744
745 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
746   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
747 }
748
749 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
750   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
751 }
752
753
754 /*===-- Instruction builders ----------------------------------------------===*/
755
756 LLVMBuilderRef LLVMCreateBuilder() {
757   return wrap(new LLVMFoldingBuilder());
758 }
759
760 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
761   Instruction *I = unwrap<Instruction>(Instr);
762   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
763 }
764
765 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
766   BasicBlock *BB = unwrap(Block);
767   unwrap(Builder)->SetInsertPoint(BB);
768 }
769
770 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
771   delete unwrap(Builder);
772 }
773
774 /*--.. Instruction builders ................................................--*/
775
776 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
777   return wrap(unwrap(B)->CreateRetVoid());
778 }
779
780 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
781   return wrap(unwrap(B)->CreateRet(unwrap(V)));
782 }
783
784 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
785   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
786 }
787
788 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
789                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
790   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
791 }
792
793 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
794                              LLVMBasicBlockRef Else, unsigned NumCases) {
795   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
796 }
797
798 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
799                              LLVMValueRef *Args, unsigned NumArgs,
800                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
801                              const char *Name) {
802   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
803                                       unwrap(Args), unwrap(Args) + NumArgs,
804                                       Name));
805 }
806
807 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
808   return wrap(unwrap(B)->CreateUnwind());
809 }
810
811 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
812   return wrap(unwrap(B)->CreateUnreachable());
813 }
814
815 /*--.. Arithmetic ..........................................................--*/
816
817 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
818                           const char *Name) {
819   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
820 }
821
822 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
823                           const char *Name) {
824   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
825 }
826
827 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
828                           const char *Name) {
829   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
830 }
831
832 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
833                            const char *Name) {
834   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
835 }
836
837 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
838                            const char *Name) {
839   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
840 }
841
842 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
843                            const char *Name) {
844   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
845 }
846
847 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
848                            const char *Name) {
849   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
850 }
851
852 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
853                            const char *Name) {
854   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
855 }
856
857 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
858                            const char *Name) {
859   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
860 }
861
862 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
863                           const char *Name) {
864   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
865 }
866
867 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
868                            const char *Name) {
869   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
870 }
871
872 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
873                            const char *Name) {
874   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
875 }
876
877 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
878                           const char *Name) {
879   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
880 }
881
882 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
883                          const char *Name) {
884   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
885 }
886
887 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
888                           const char *Name) {
889   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
890 }
891
892 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
893   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
894 }
895
896 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
897   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
898 }
899
900 /*--.. Memory ..............................................................--*/
901
902 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
903                              const char *Name) {
904   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
905 }
906
907 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
908                                   LLVMValueRef Val, const char *Name) {
909   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
910 }
911
912 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
913                              const char *Name) {
914   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
915 }
916
917 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
918                                   LLVMValueRef Val, const char *Name) {
919   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
920 }
921
922 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
923   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
924 }
925
926
927 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
928                            const char *Name) {
929   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
930 }
931
932 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
933                             LLVMValueRef PointerVal) {
934   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
935 }
936
937 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
938                           LLVMValueRef *Indices, unsigned NumIndices,
939                           const char *Name) {
940   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
941                                    unwrap(Indices) + NumIndices, Name));
942 }
943
944 /*--.. Casts ...............................................................--*/
945
946 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
947                             LLVMTypeRef DestTy, const char *Name) {
948   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
949 }
950
951 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
952                            LLVMTypeRef DestTy, const char *Name) {
953   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
954 }
955
956 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
957                            LLVMTypeRef DestTy, const char *Name) {
958   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
959 }
960
961 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
962                              LLVMTypeRef DestTy, const char *Name) {
963   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
964 }
965
966 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
967                              LLVMTypeRef DestTy, const char *Name) {
968   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
969 }
970
971 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
972                              LLVMTypeRef DestTy, const char *Name) {
973   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
974 }
975
976 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
977                              LLVMTypeRef DestTy, const char *Name) {
978   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
979 }
980
981 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
982                               LLVMTypeRef DestTy, const char *Name) {
983   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
984 }
985
986 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
987                             LLVMTypeRef DestTy, const char *Name) {
988   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
989 }
990
991 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
992                                LLVMTypeRef DestTy, const char *Name) {
993   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
994 }
995
996 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
997                                LLVMTypeRef DestTy, const char *Name) {
998   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
999 }
1000
1001 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1002                               LLVMTypeRef DestTy, const char *Name) {
1003   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1004 }
1005
1006 /*--.. Comparisons .........................................................--*/
1007
1008 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1009                            LLVMValueRef LHS, LLVMValueRef RHS,
1010                            const char *Name) {
1011   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1012                                     unwrap(LHS), unwrap(RHS), Name));
1013 }
1014
1015 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1016                            LLVMValueRef LHS, LLVMValueRef RHS,
1017                            const char *Name) {
1018   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1019                                     unwrap(LHS), unwrap(RHS), Name));
1020 }
1021
1022 /*--.. Miscellaneous instructions ..........................................--*/
1023
1024 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1025   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1026 }
1027
1028 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1029                            LLVMValueRef *Args, unsigned NumArgs,
1030                            const char *Name) {
1031   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1032                                     unwrap(Args) + NumArgs, Name));
1033 }
1034
1035 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1036                              LLVMValueRef Then, LLVMValueRef Else,
1037                              const char *Name) {
1038   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1039                                       Name));
1040 }
1041
1042 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1043                             LLVMTypeRef Ty, const char *Name) {
1044   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1045 }
1046
1047 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1048                                       LLVMValueRef Index, const char *Name) {
1049   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1050                                               Name));
1051 }
1052
1053 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1054                                     LLVMValueRef EltVal, LLVMValueRef Index,
1055                                     const char *Name) {
1056   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1057                                              unwrap(Index), Name));
1058 }
1059
1060 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1061                                     LLVMValueRef V2, LLVMValueRef Mask,
1062                                     const char *Name) {
1063   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1064                                              unwrap(Mask), Name));
1065 }
1066
1067
1068 /*===-- Module providers --------------------------------------------------===*/
1069
1070 LLVMModuleProviderRef
1071 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1072   return wrap(new ExistingModuleProvider(unwrap(M)));
1073 }
1074
1075 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1076   delete unwrap(MP);
1077 }
1078
1079
1080 /*===-- Memory buffers ----------------------------------------------------===*/
1081
1082 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1083                                              LLVMMemoryBufferRef *OutMemBuf,
1084                                              char **OutMessage) {
1085   std::string Error;
1086   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1087     *OutMemBuf = wrap(MB);
1088     return 0;
1089   }
1090   
1091   *OutMessage = strdup(Error.c_str());
1092   return 1;
1093 }
1094
1095 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1096                                     char **OutMessage) {
1097   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1098     *OutMemBuf = wrap(MB);
1099     return 0;
1100   }
1101   
1102   *OutMessage = strdup("stdin is empty.");
1103   return 1;
1104 }
1105
1106 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1107   delete unwrap(MemBuf);
1108 }