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