d383b3db8d4c9b3302ed252b3435da0c6189b567
[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/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/Support/CallSite.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <cassert>
31 #include <cstdlib>
32 #include <cstring>
33
34 using namespace llvm;
35
36
37 /*===-- Error handling ----------------------------------------------------===*/
38
39 void LLVMDisposeMessage(char *Message) {
40   free(Message);
41 }
42
43
44 /*===-- Operations on contexts --------------------------------------------===*/
45
46 LLVMContextRef LLVMContextCreate() {
47   return wrap(new LLVMContext());
48 }
49
50 LLVMContextRef LLVMGetGlobalContext() {
51   return wrap(&getGlobalContext());
52 }
53
54 void LLVMContextDispose(LLVMContextRef C) {
55   delete unwrap(C);
56 }
57
58
59 /*===-- Operations on modules ---------------------------------------------===*/
60
61 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
62   return wrap(new Module(ModuleID, getGlobalContext()));
63 }
64
65 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
66                                                 LLVMContextRef C) {
67   return wrap(new Module(ModuleID, *unwrap(C)));
68 }
69
70 void LLVMDisposeModule(LLVMModuleRef M) {
71   delete unwrap(M);
72 }
73
74 /*--.. Data layout .........................................................--*/
75 const char * LLVMGetDataLayout(LLVMModuleRef M) {
76   return unwrap(M)->getDataLayout().c_str();
77 }
78
79 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
80   unwrap(M)->setDataLayout(Triple);
81 }
82
83 /*--.. Target triple .......................................................--*/
84 const char * LLVMGetTarget(LLVMModuleRef M) {
85   return unwrap(M)->getTargetTriple().c_str();
86 }
87
88 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
89   unwrap(M)->setTargetTriple(Triple);
90 }
91
92 /*--.. Type names ..........................................................--*/
93 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
94   return unwrap(M)->addTypeName(Name, unwrap(Ty));
95 }
96
97 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
99
100   TypeSymbolTable::iterator I = TST.find(Name);
101   if (I != TST.end())
102     TST.remove(I);
103 }
104
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106   return wrap(unwrap(M)->getTypeByName(Name));
107 }
108
109 void LLVMDumpModule(LLVMModuleRef M) {
110   unwrap(M)->dump();
111 }
112
113
114 /*===-- Operations on types -----------------------------------------------===*/
115
116 /*--.. Operations on all types (mostly) ....................................--*/
117
118 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
119   switch (unwrap(Ty)->getTypeID()) {
120   default:
121     assert(false && "Unhandled TypeID.");
122   case Type::VoidTyID:
123     return LLVMVoidTypeKind;
124   case Type::FloatTyID:
125     return LLVMFloatTypeKind;
126   case Type::DoubleTyID:
127     return LLVMDoubleTypeKind;
128   case Type::X86_FP80TyID:
129     return LLVMX86_FP80TypeKind;
130   case Type::FP128TyID:
131     return LLVMFP128TypeKind;
132   case Type::PPC_FP128TyID:
133     return LLVMPPC_FP128TypeKind;
134   case Type::LabelTyID:
135     return LLVMLabelTypeKind;
136   case Type::MetadataTyID:
137     return LLVMMetadataTypeKind;
138   case Type::IntegerTyID:
139     return LLVMIntegerTypeKind;
140   case Type::FunctionTyID:
141     return LLVMFunctionTypeKind;
142   case Type::StructTyID:
143     return LLVMStructTypeKind;
144   case Type::UnionTyID:
145     return LLVMUnionTypeKind;
146   case Type::ArrayTyID:
147     return LLVMArrayTypeKind;
148   case Type::PointerTyID:
149     return LLVMPointerTypeKind;
150   case Type::OpaqueTyID:
151     return LLVMOpaqueTypeKind;
152   case Type::VectorTyID:
153     return LLVMVectorTypeKind;
154   }
155 }
156
157 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
158   return wrap(&unwrap(Ty)->getContext());
159 }
160
161 /*--.. Operations on integer types .........................................--*/
162
163 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
164   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
165 }
166 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
167   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
168 }
169 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
170   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
171 }
172 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
173   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
174 }
175 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
176   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
177 }
178 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
179   return wrap(IntegerType::get(*unwrap(C), NumBits));
180 }
181
182 LLVMTypeRef LLVMInt1Type(void)  {
183   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
184 }
185 LLVMTypeRef LLVMInt8Type(void)  {
186   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
187 }
188 LLVMTypeRef LLVMInt16Type(void) {
189   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
190 }
191 LLVMTypeRef LLVMInt32Type(void) {
192   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
193 }
194 LLVMTypeRef LLVMInt64Type(void) {
195   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
196 }
197 LLVMTypeRef LLVMIntType(unsigned NumBits) {
198   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
199 }
200
201 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
202   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
203 }
204
205 /*--.. Operations on real types ............................................--*/
206
207 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
208   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
209 }
210 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
211   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
212 }
213 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
214   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
215 }
216 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
217   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
218 }
219 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
220   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
221 }
222
223 LLVMTypeRef LLVMFloatType(void) {
224   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
225 }
226 LLVMTypeRef LLVMDoubleType(void) {
227   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
228 }
229 LLVMTypeRef LLVMX86FP80Type(void) {
230   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
231 }
232 LLVMTypeRef LLVMFP128Type(void) {
233   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
234 }
235 LLVMTypeRef LLVMPPCFP128Type(void) {
236   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
237 }
238
239 /*--.. Operations on function types ........................................--*/
240
241 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
242                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
243                              LLVMBool IsVarArg) {
244   std::vector<const Type*> Tys;
245   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
246     Tys.push_back(unwrap(*I));
247   
248   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
249 }
250
251 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
252   return unwrap<FunctionType>(FunctionTy)->isVarArg();
253 }
254
255 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
256   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
257 }
258
259 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
260   return unwrap<FunctionType>(FunctionTy)->getNumParams();
261 }
262
263 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
264   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
265   for (FunctionType::param_iterator I = Ty->param_begin(),
266                                     E = Ty->param_end(); I != E; ++I)
267     *Dest++ = wrap(*I);
268 }
269
270 /*--.. Operations on struct types ..........................................--*/
271
272 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
273                            unsigned ElementCount, LLVMBool Packed) {
274   std::vector<const Type*> Tys;
275   for (LLVMTypeRef *I = ElementTypes,
276                    *E = ElementTypes + ElementCount; I != E; ++I)
277     Tys.push_back(unwrap(*I));
278   
279   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
280 }
281
282 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
283                            unsigned ElementCount, LLVMBool Packed) {
284   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
285                                  ElementCount, Packed);
286 }
287
288
289 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
290   return unwrap<StructType>(StructTy)->getNumElements();
291 }
292
293 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
294   StructType *Ty = unwrap<StructType>(StructTy);
295   for (FunctionType::param_iterator I = Ty->element_begin(),
296                                     E = Ty->element_end(); I != E; ++I)
297     *Dest++ = wrap(*I);
298 }
299
300 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
301   return unwrap<StructType>(StructTy)->isPacked();
302 }
303
304 /*--.. Operations on union types ..........................................--*/
305
306 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
307                                    unsigned ElementCount) {
308   SmallVector<const Type*, 8> Tys;
309   for (LLVMTypeRef *I = ElementTypes,
310                    *E = ElementTypes + ElementCount; I != E; ++I)
311     Tys.push_back(unwrap(*I));
312   
313   return wrap(UnionType::get(&Tys[0], Tys.size()));
314 }
315
316 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes,
317                            unsigned ElementCount, int Packed) {
318   return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
319                                 ElementCount);
320 }
321
322 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
323   return unwrap<UnionType>(UnionTy)->getNumElements();
324 }
325
326 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
327   UnionType *Ty = unwrap<UnionType>(UnionTy);
328   for (FunctionType::param_iterator I = Ty->element_begin(),
329                                     E = Ty->element_end(); I != E; ++I)
330     *Dest++ = wrap(*I);
331 }
332
333 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
334
335 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
336   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
337 }
338
339 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
340   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
341 }
342
343 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
344   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
345 }
346
347 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
348   return wrap(unwrap<SequentialType>(Ty)->getElementType());
349 }
350
351 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
352   return unwrap<ArrayType>(ArrayTy)->getNumElements();
353 }
354
355 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
356   return unwrap<PointerType>(PointerTy)->getAddressSpace();
357 }
358
359 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
360   return unwrap<VectorType>(VectorTy)->getNumElements();
361 }
362
363 /*--.. Operations on other types ...........................................--*/
364
365 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
366   return wrap(Type::getVoidTy(*unwrap(C)));
367 }
368 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
369   return wrap(Type::getLabelTy(*unwrap(C)));
370 }
371 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
372   return wrap(OpaqueType::get(*unwrap(C)));
373 }
374
375 LLVMTypeRef LLVMVoidType(void)  {
376   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
377 }
378 LLVMTypeRef LLVMLabelType(void) {
379   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
380 }
381 LLVMTypeRef LLVMOpaqueType(void) {
382   return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
383 }
384
385 /*--.. Operations on type handles ..........................................--*/
386
387 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
388   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
389 }
390
391 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
392   delete unwrap(TypeHandle);
393 }
394
395 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
396   return wrap(unwrap(TypeHandle)->get());
397 }
398
399 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
400   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
401 }
402
403
404 /*===-- Operations on values ----------------------------------------------===*/
405
406 /*--.. Operations on all values ............................................--*/
407
408 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
409   return wrap(unwrap(Val)->getType());
410 }
411
412 const char *LLVMGetValueName(LLVMValueRef Val) {
413   return unwrap(Val)->getName().data();
414 }
415
416 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
417   unwrap(Val)->setName(Name);
418 }
419
420 void LLVMDumpValue(LLVMValueRef Val) {
421   unwrap(Val)->dump();
422 }
423
424 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
425   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
426 }
427
428 /*--.. Conversion functions ................................................--*/
429
430 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
431   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
432     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
433   }
434
435 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
436
437 /*--.. Operations on Uses ..................................................--*/
438 LLVMUseIteratorRef LLVMGetFirstUse(LLVMValueRef Val) {
439   Value *V = unwrap(Val);
440   Value::use_iterator I = V->use_begin();
441   if (I == V->use_end())
442     return 0;
443   return wrap(&(I.getUse()));
444 }
445
446 LLVMUseIteratorRef LLVMGetNextUse(LLVMUseIteratorRef UR) {
447   return wrap(unwrap(UR)->getNext());
448 }
449
450 LLVMValueRef LLVMGetUser(LLVMUseIteratorRef UR) {
451   return wrap(unwrap(UR)->getUser());
452 }
453
454 LLVMValueRef LLVMGetUsedValue(LLVMUseIteratorRef UR) {
455   return wrap(unwrap(UR)->get());
456 }
457
458 /*--.. Operations on Users .................................................--*/
459 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
460   return wrap(unwrap<User>(Val)->getOperand(Index));
461 }
462
463 /*--.. Operations on constants of any type .................................--*/
464
465 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
466   return wrap(Constant::getNullValue(unwrap(Ty)));
467 }
468
469 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
470   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
471 }
472
473 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
474   return wrap(UndefValue::get(unwrap(Ty)));
475 }
476
477 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
478   return isa<Constant>(unwrap(Ty));
479 }
480
481 LLVMBool LLVMIsNull(LLVMValueRef Val) {
482   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
483     return C->isNullValue();
484   return false;
485 }
486
487 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
488   return isa<UndefValue>(unwrap(Val));
489 }
490
491 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
492   return
493       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
494 }
495
496 /*--.. Operations on scalar constants ......................................--*/
497
498 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
499                           LLVMBool SignExtend) {
500   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
501 }
502
503 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
504                                   uint8_t Radix) {
505   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
506                                Radix));
507 }
508
509 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
510                                          unsigned SLen, uint8_t Radix) {
511   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
512                                Radix));
513 }
514
515 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
516   return wrap(ConstantFP::get(unwrap(RealTy), N));
517 }
518
519 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
520   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
521 }
522
523 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
524                                           unsigned SLen) {
525   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
526 }
527
528 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
529   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
530 }
531
532 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
533   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
534 }
535
536 /*--.. Operations on composite constants ...................................--*/
537
538 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
539                                       unsigned Length,
540                                       LLVMBool DontNullTerminate) {
541   /* Inverted the sense of AddNull because ', 0)' is a
542      better mnemonic for null termination than ', 1)'. */
543   return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
544                                  DontNullTerminate == 0));
545 }
546 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
547                                       LLVMValueRef *ConstantVals,
548                                       unsigned Count, LLVMBool Packed) {
549   return wrap(ConstantStruct::get(*unwrap(C),
550                                   unwrap<Constant>(ConstantVals, Count),
551                                   Count, Packed != 0));
552 }
553
554 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
555                              LLVMBool DontNullTerminate) {
556   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
557                                   DontNullTerminate);
558 }
559 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
560                             LLVMValueRef *ConstantVals, unsigned Length) {
561   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
562                                  unwrap<Constant>(ConstantVals, Length),
563                                  Length));
564 }
565 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
566                              LLVMBool Packed) {
567   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
568                                   Packed);
569 }
570
571 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
572   return wrap(ConstantVector::get(
573                             unwrap<Constant>(ScalarConstantVals, Size), Size));
574 }
575
576 /*--.. Constant expressions ................................................--*/
577
578 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
579   return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
580 }
581
582 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
583   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
584 }
585
586 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
587   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
588 }
589
590 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
591   return wrap(ConstantExpr::getNeg(
592                                    unwrap<Constant>(ConstantVal)));
593 }
594
595 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
596   return wrap(ConstantExpr::getFNeg(
597                                     unwrap<Constant>(ConstantVal)));
598 }
599
600 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
601   return wrap(ConstantExpr::getNot(
602                                    unwrap<Constant>(ConstantVal)));
603 }
604
605 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
606   return wrap(ConstantExpr::getAdd(
607                                    unwrap<Constant>(LHSConstant),
608                                    unwrap<Constant>(RHSConstant)));
609 }
610
611 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
612                              LLVMValueRef RHSConstant) {
613   return wrap(ConstantExpr::getNSWAdd(
614                                       unwrap<Constant>(LHSConstant),
615                                       unwrap<Constant>(RHSConstant)));
616 }
617
618 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
619   return wrap(ConstantExpr::getFAdd(
620                                     unwrap<Constant>(LHSConstant),
621                                     unwrap<Constant>(RHSConstant)));
622 }
623
624 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
625   return wrap(ConstantExpr::getSub(
626                                    unwrap<Constant>(LHSConstant),
627                                    unwrap<Constant>(RHSConstant)));
628 }
629
630 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
631   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
632                                     unwrap<Constant>(RHSConstant)));
633 }
634
635 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
636   return wrap(ConstantExpr::getMul(
637                                    unwrap<Constant>(LHSConstant),
638                                    unwrap<Constant>(RHSConstant)));
639 }
640
641 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
642   return wrap(ConstantExpr::getFMul(
643                                     unwrap<Constant>(LHSConstant),
644                                     unwrap<Constant>(RHSConstant)));
645 }
646
647 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
648   return wrap(ConstantExpr::getUDiv(
649                                     unwrap<Constant>(LHSConstant),
650                                     unwrap<Constant>(RHSConstant)));
651 }
652
653 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
654   return wrap(ConstantExpr::getSDiv(
655                                     unwrap<Constant>(LHSConstant),
656                                     unwrap<Constant>(RHSConstant)));
657 }
658
659 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
660                                 LLVMValueRef RHSConstant) {
661   return wrap(ConstantExpr::getExactSDiv(
662                                          unwrap<Constant>(LHSConstant),
663                                          unwrap<Constant>(RHSConstant)));
664 }
665
666 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
667   return wrap(ConstantExpr::getFDiv(
668                                     unwrap<Constant>(LHSConstant),
669                                     unwrap<Constant>(RHSConstant)));
670 }
671
672 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
673   return wrap(ConstantExpr::getURem(
674                                     unwrap<Constant>(LHSConstant),
675                                     unwrap<Constant>(RHSConstant)));
676 }
677
678 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
679   return wrap(ConstantExpr::getSRem(
680                                     unwrap<Constant>(LHSConstant),
681                                     unwrap<Constant>(RHSConstant)));
682 }
683
684 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
685   return wrap(ConstantExpr::getFRem(
686                                     unwrap<Constant>(LHSConstant),
687                                     unwrap<Constant>(RHSConstant)));
688 }
689
690 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
691   return wrap(ConstantExpr::getAnd(
692                                    unwrap<Constant>(LHSConstant),
693                                    unwrap<Constant>(RHSConstant)));
694 }
695
696 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
697   return wrap(ConstantExpr::getOr(
698                                   unwrap<Constant>(LHSConstant),
699                                   unwrap<Constant>(RHSConstant)));
700 }
701
702 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
703   return wrap(ConstantExpr::getXor(
704                                    unwrap<Constant>(LHSConstant),
705                                    unwrap<Constant>(RHSConstant)));
706 }
707
708 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
709                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
710   return wrap(ConstantExpr::getICmp(Predicate,
711                                     unwrap<Constant>(LHSConstant),
712                                     unwrap<Constant>(RHSConstant)));
713 }
714
715 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
716                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
717   return wrap(ConstantExpr::getFCmp(Predicate,
718                                     unwrap<Constant>(LHSConstant),
719                                     unwrap<Constant>(RHSConstant)));
720 }
721
722 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
723   return wrap(ConstantExpr::getShl(
724                                   unwrap<Constant>(LHSConstant),
725                                   unwrap<Constant>(RHSConstant)));
726 }
727
728 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
729   return wrap(ConstantExpr::getLShr(
730                                     unwrap<Constant>(LHSConstant),
731                                     unwrap<Constant>(RHSConstant)));
732 }
733
734 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
735   return wrap(ConstantExpr::getAShr(
736                                     unwrap<Constant>(LHSConstant),
737                                     unwrap<Constant>(RHSConstant)));
738 }
739
740 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
741                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
742   return wrap(ConstantExpr::getGetElementPtr(
743                                              unwrap<Constant>(ConstantVal),
744                                              unwrap<Constant>(ConstantIndices, 
745                                                               NumIndices),
746                                              NumIndices));
747 }
748
749 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
750                                   LLVMValueRef *ConstantIndices,
751                                   unsigned NumIndices) {
752   Constant* Val = unwrap<Constant>(ConstantVal);
753   Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
754   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
755 }
756
757 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
758   return wrap(ConstantExpr::getTrunc(
759                                      unwrap<Constant>(ConstantVal),
760                                      unwrap(ToType)));
761 }
762
763 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
764   return wrap(ConstantExpr::getSExt(
765                                     unwrap<Constant>(ConstantVal),
766                                     unwrap(ToType)));
767 }
768
769 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
770   return wrap(ConstantExpr::getZExt(
771                                     unwrap<Constant>(ConstantVal),
772                                     unwrap(ToType)));
773 }
774
775 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
776   return wrap(ConstantExpr::getFPTrunc(
777                                        unwrap<Constant>(ConstantVal),
778                                        unwrap(ToType)));
779 }
780
781 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
782   return wrap(ConstantExpr::getFPExtend(
783                                         unwrap<Constant>(ConstantVal),
784                                         unwrap(ToType)));
785 }
786
787 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
788   return wrap(ConstantExpr::getUIToFP(
789                                       unwrap<Constant>(ConstantVal),
790                                       unwrap(ToType)));
791 }
792
793 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
794   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
795                                       unwrap(ToType)));
796 }
797
798 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
799   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
800                                       unwrap(ToType)));
801 }
802
803 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
804   return wrap(ConstantExpr::getFPToSI(
805                                       unwrap<Constant>(ConstantVal),
806                                       unwrap(ToType)));
807 }
808
809 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
810   return wrap(ConstantExpr::getPtrToInt(
811                                         unwrap<Constant>(ConstantVal),
812                                         unwrap(ToType)));
813 }
814
815 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
816   return wrap(ConstantExpr::getIntToPtr(
817                                         unwrap<Constant>(ConstantVal),
818                                         unwrap(ToType)));
819 }
820
821 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
822   return wrap(ConstantExpr::getBitCast(
823                                        unwrap<Constant>(ConstantVal),
824                                        unwrap(ToType)));
825 }
826
827 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
828                                     LLVMTypeRef ToType) {
829   return wrap(ConstantExpr::getZExtOrBitCast(
830                                              unwrap<Constant>(ConstantVal),
831                                              unwrap(ToType)));
832 }
833
834 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
835                                     LLVMTypeRef ToType) {
836   return wrap(ConstantExpr::getSExtOrBitCast(
837                                              unwrap<Constant>(ConstantVal),
838                                              unwrap(ToType)));
839 }
840
841 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
842                                      LLVMTypeRef ToType) {
843   return wrap(ConstantExpr::getTruncOrBitCast(
844                                               unwrap<Constant>(ConstantVal),
845                                               unwrap(ToType)));
846 }
847
848 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
849                                   LLVMTypeRef ToType) {
850   return wrap(ConstantExpr::getPointerCast(
851                                            unwrap<Constant>(ConstantVal),
852                                            unwrap(ToType)));
853 }
854
855 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
856                               LLVMBool isSigned) {
857   return wrap(ConstantExpr::getIntegerCast(
858                                            unwrap<Constant>(ConstantVal),
859                                            unwrap(ToType),
860                                            isSigned));
861 }
862
863 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864   return wrap(ConstantExpr::getFPCast(
865                                       unwrap<Constant>(ConstantVal),
866                                       unwrap(ToType)));
867 }
868
869 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
870                              LLVMValueRef ConstantIfTrue,
871                              LLVMValueRef ConstantIfFalse) {
872   return wrap(ConstantExpr::getSelect(
873                                       unwrap<Constant>(ConstantCondition),
874                                       unwrap<Constant>(ConstantIfTrue),
875                                       unwrap<Constant>(ConstantIfFalse)));
876 }
877
878 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
879                                      LLVMValueRef IndexConstant) {
880   return wrap(ConstantExpr::getExtractElement(
881                                               unwrap<Constant>(VectorConstant),
882                                               unwrap<Constant>(IndexConstant)));
883 }
884
885 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
886                                     LLVMValueRef ElementValueConstant,
887                                     LLVMValueRef IndexConstant) {
888   return wrap(ConstantExpr::getInsertElement(
889                                          unwrap<Constant>(VectorConstant),
890                                          unwrap<Constant>(ElementValueConstant),
891                                              unwrap<Constant>(IndexConstant)));
892 }
893
894 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
895                                     LLVMValueRef VectorBConstant,
896                                     LLVMValueRef MaskConstant) {
897   return wrap(ConstantExpr::getShuffleVector(
898                                              unwrap<Constant>(VectorAConstant),
899                                              unwrap<Constant>(VectorBConstant),
900                                              unwrap<Constant>(MaskConstant)));
901 }
902
903 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
904                                    unsigned NumIdx) {
905   return wrap(ConstantExpr::getExtractValue(
906                                             unwrap<Constant>(AggConstant),
907                                             IdxList, NumIdx));
908 }
909
910 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
911                                   LLVMValueRef ElementValueConstant,
912                                   unsigned *IdxList, unsigned NumIdx) {
913   return wrap(ConstantExpr::getInsertValue(
914                                          unwrap<Constant>(AggConstant),
915                                          unwrap<Constant>(ElementValueConstant),
916                                            IdxList, NumIdx));
917 }
918
919 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
920                                 const char *Constraints,
921                                 LLVMBool HasSideEffects,
922                                 LLVMBool IsAlignStack) {
923   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
924                              Constraints, HasSideEffects, IsAlignStack));
925 }
926
927 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
928
929 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
930   return wrap(unwrap<GlobalValue>(Global)->getParent());
931 }
932
933 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
934   return unwrap<GlobalValue>(Global)->isDeclaration();
935 }
936
937 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
938   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
939   default:
940     assert(false && "Unhandled Linkage Type.");
941   case GlobalValue::ExternalLinkage:
942     return LLVMExternalLinkage;
943   case GlobalValue::AvailableExternallyLinkage:
944     return LLVMAvailableExternallyLinkage;
945   case GlobalValue::LinkOnceAnyLinkage:
946     return LLVMLinkOnceAnyLinkage;
947   case GlobalValue::LinkOnceODRLinkage:
948     return LLVMLinkOnceODRLinkage;
949   case GlobalValue::WeakAnyLinkage:
950     return LLVMWeakAnyLinkage;
951   case GlobalValue::WeakODRLinkage:
952     return LLVMWeakODRLinkage;
953   case GlobalValue::AppendingLinkage:
954     return LLVMAppendingLinkage;
955   case GlobalValue::InternalLinkage:
956     return LLVMInternalLinkage;
957   case GlobalValue::PrivateLinkage:
958     return LLVMPrivateLinkage;
959   case GlobalValue::LinkerPrivateLinkage:
960     return LLVMLinkerPrivateLinkage;
961   case GlobalValue::DLLImportLinkage:
962     return LLVMDLLImportLinkage;
963   case GlobalValue::DLLExportLinkage:
964     return LLVMDLLExportLinkage;
965   case GlobalValue::ExternalWeakLinkage:
966     return LLVMExternalWeakLinkage;
967   case GlobalValue::CommonLinkage:
968     return LLVMCommonLinkage;
969   }
970
971   // Should never get here.
972   return static_cast<LLVMLinkage>(0);
973 }
974
975 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
976   GlobalValue *GV = unwrap<GlobalValue>(Global);
977
978   switch (Linkage) {
979   default:
980     assert(false && "Unhandled Linkage Type.");
981   case LLVMExternalLinkage:
982     GV->setLinkage(GlobalValue::ExternalLinkage);
983     break;
984   case LLVMAvailableExternallyLinkage:
985     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
986     break;
987   case LLVMLinkOnceAnyLinkage:
988     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
989     break;
990   case LLVMLinkOnceODRLinkage:
991     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
992     break;
993   case LLVMWeakAnyLinkage:
994     GV->setLinkage(GlobalValue::WeakAnyLinkage);
995     break;
996   case LLVMWeakODRLinkage:
997     GV->setLinkage(GlobalValue::WeakODRLinkage);
998     break;
999   case LLVMAppendingLinkage:
1000     GV->setLinkage(GlobalValue::AppendingLinkage);
1001     break;
1002   case LLVMInternalLinkage:
1003     GV->setLinkage(GlobalValue::InternalLinkage);
1004     break;
1005   case LLVMPrivateLinkage:
1006     GV->setLinkage(GlobalValue::PrivateLinkage);
1007     break;
1008   case LLVMLinkerPrivateLinkage:
1009     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1010     break;
1011   case LLVMDLLImportLinkage:
1012     GV->setLinkage(GlobalValue::DLLImportLinkage);
1013     break;
1014   case LLVMDLLExportLinkage:
1015     GV->setLinkage(GlobalValue::DLLExportLinkage);
1016     break;
1017   case LLVMExternalWeakLinkage:
1018     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1019     break;
1020   case LLVMGhostLinkage:
1021     DEBUG(errs()
1022           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1023     break;
1024   case LLVMCommonLinkage:
1025     GV->setLinkage(GlobalValue::CommonLinkage);
1026     break;
1027   }
1028 }
1029
1030 const char *LLVMGetSection(LLVMValueRef Global) {
1031   return unwrap<GlobalValue>(Global)->getSection().c_str();
1032 }
1033
1034 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1035   unwrap<GlobalValue>(Global)->setSection(Section);
1036 }
1037
1038 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1039   return static_cast<LLVMVisibility>(
1040     unwrap<GlobalValue>(Global)->getVisibility());
1041 }
1042
1043 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1044   unwrap<GlobalValue>(Global)
1045     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1046 }
1047
1048 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1049   return unwrap<GlobalValue>(Global)->getAlignment();
1050 }
1051
1052 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1053   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1054 }
1055
1056 /*--.. Operations on global variables ......................................--*/
1057
1058 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1059   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1060                                  GlobalValue::ExternalLinkage, 0, Name));
1061 }
1062
1063 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1064   return wrap(unwrap(M)->getNamedGlobal(Name));
1065 }
1066
1067 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1068   Module *Mod = unwrap(M);
1069   Module::global_iterator I = Mod->global_begin();
1070   if (I == Mod->global_end())
1071     return 0;
1072   return wrap(I);
1073 }
1074
1075 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1076   Module *Mod = unwrap(M);
1077   Module::global_iterator I = Mod->global_end();
1078   if (I == Mod->global_begin())
1079     return 0;
1080   return wrap(--I);
1081 }
1082
1083 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1084   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1085   Module::global_iterator I = GV;
1086   if (++I == GV->getParent()->global_end())
1087     return 0;
1088   return wrap(I);
1089 }
1090
1091 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1092   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1093   Module::global_iterator I = GV;
1094   if (I == GV->getParent()->global_begin())
1095     return 0;
1096   return wrap(--I);
1097 }
1098
1099 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1100   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1101 }
1102
1103 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1104   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1105   if ( !GV->hasInitializer() )
1106     return 0;
1107   return wrap(GV->getInitializer());
1108 }
1109
1110 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1111   unwrap<GlobalVariable>(GlobalVar)
1112     ->setInitializer(unwrap<Constant>(ConstantVal));
1113 }
1114
1115 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1116   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1117 }
1118
1119 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1120   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1121 }
1122
1123 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1124   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1125 }
1126
1127 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1128   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1129 }
1130
1131 /*--.. Operations on aliases ......................................--*/
1132
1133 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1134                           const char *Name) {
1135   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1136                               unwrap<Constant>(Aliasee), unwrap (M)));
1137 }
1138
1139 /*--.. Operations on functions .............................................--*/
1140
1141 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1142                              LLVMTypeRef FunctionTy) {
1143   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1144                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1145 }
1146
1147 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1148   return wrap(unwrap(M)->getFunction(Name));
1149 }
1150
1151 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1152   Module *Mod = unwrap(M);
1153   Module::iterator I = Mod->begin();
1154   if (I == Mod->end())
1155     return 0;
1156   return wrap(I);
1157 }
1158
1159 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1160   Module *Mod = unwrap(M);
1161   Module::iterator I = Mod->end();
1162   if (I == Mod->begin())
1163     return 0;
1164   return wrap(--I);
1165 }
1166
1167 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1168   Function *Func = unwrap<Function>(Fn);
1169   Module::iterator I = Func;
1170   if (++I == Func->getParent()->end())
1171     return 0;
1172   return wrap(I);
1173 }
1174
1175 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1176   Function *Func = unwrap<Function>(Fn);
1177   Module::iterator I = Func;
1178   if (I == Func->getParent()->begin())
1179     return 0;
1180   return wrap(--I);
1181 }
1182
1183 void LLVMDeleteFunction(LLVMValueRef Fn) {
1184   unwrap<Function>(Fn)->eraseFromParent();
1185 }
1186
1187 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1188   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1189     return F->getIntrinsicID();
1190   return 0;
1191 }
1192
1193 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1194   return unwrap<Function>(Fn)->getCallingConv();
1195 }
1196
1197 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1198   return unwrap<Function>(Fn)->setCallingConv(
1199     static_cast<CallingConv::ID>(CC));
1200 }
1201
1202 const char *LLVMGetGC(LLVMValueRef Fn) {
1203   Function *F = unwrap<Function>(Fn);
1204   return F->hasGC()? F->getGC() : 0;
1205 }
1206
1207 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1208   Function *F = unwrap<Function>(Fn);
1209   if (GC)
1210     F->setGC(GC);
1211   else
1212     F->clearGC();
1213 }
1214
1215 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1216   Function *Func = unwrap<Function>(Fn);
1217   const AttrListPtr PAL = Func->getAttributes();
1218   const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1219   Func->setAttributes(PALnew);
1220 }
1221
1222 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1223   Function *Func = unwrap<Function>(Fn);
1224   const AttrListPtr PAL = Func->getAttributes();
1225   const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1226   Func->setAttributes(PALnew);
1227 }
1228
1229 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1230   Function *Func = unwrap<Function>(Fn);
1231   const AttrListPtr PAL = Func->getAttributes();
1232   Attributes attr = PAL.getFnAttributes();
1233   return (LLVMAttribute)attr;
1234 }
1235
1236 /*--.. Operations on parameters ............................................--*/
1237
1238 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1239   // This function is strictly redundant to
1240   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1241   return unwrap<Function>(FnRef)->arg_size();
1242 }
1243
1244 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1245   Function *Fn = unwrap<Function>(FnRef);
1246   for (Function::arg_iterator I = Fn->arg_begin(),
1247                               E = Fn->arg_end(); I != E; I++)
1248     *ParamRefs++ = wrap(I);
1249 }
1250
1251 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1252   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1253   while (index --> 0)
1254     AI++;
1255   return wrap(AI);
1256 }
1257
1258 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1259   return wrap(unwrap<Argument>(V)->getParent());
1260 }
1261
1262 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1263   Function *Func = unwrap<Function>(Fn);
1264   Function::arg_iterator I = Func->arg_begin();
1265   if (I == Func->arg_end())
1266     return 0;
1267   return wrap(I);
1268 }
1269
1270 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1271   Function *Func = unwrap<Function>(Fn);
1272   Function::arg_iterator I = Func->arg_end();
1273   if (I == Func->arg_begin())
1274     return 0;
1275   return wrap(--I);
1276 }
1277
1278 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1279   Argument *A = unwrap<Argument>(Arg);
1280   Function::arg_iterator I = A;
1281   if (++I == A->getParent()->arg_end())
1282     return 0;
1283   return wrap(I);
1284 }
1285
1286 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1287   Argument *A = unwrap<Argument>(Arg);
1288   Function::arg_iterator I = A;
1289   if (I == A->getParent()->arg_begin())
1290     return 0;
1291   return wrap(--I);
1292 }
1293
1294 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1295   unwrap<Argument>(Arg)->addAttr(PA);
1296 }
1297
1298 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1299   unwrap<Argument>(Arg)->removeAttr(PA);
1300 }
1301
1302 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1303   Argument *A = unwrap<Argument>(Arg);
1304   Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1305     A->getArgNo()+1);
1306   return (LLVMAttribute)attr;
1307 }
1308   
1309
1310 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1311   unwrap<Argument>(Arg)->addAttr(
1312           Attribute::constructAlignmentFromInt(align));
1313 }
1314
1315 /*--.. Operations on basic blocks ..........................................--*/
1316
1317 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1318   return wrap(static_cast<Value*>(unwrap(BB)));
1319 }
1320
1321 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1322   return isa<BasicBlock>(unwrap(Val));
1323 }
1324
1325 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1326   return wrap(unwrap<BasicBlock>(Val));
1327 }
1328
1329 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1330   return wrap(unwrap(BB)->getParent());
1331 }
1332
1333 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1334   return unwrap<Function>(FnRef)->size();
1335 }
1336
1337 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1338   Function *Fn = unwrap<Function>(FnRef);
1339   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1340     *BasicBlocksRefs++ = wrap(I);
1341 }
1342
1343 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1344   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1345 }
1346
1347 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1348   Function *Func = unwrap<Function>(Fn);
1349   Function::iterator I = Func->begin();
1350   if (I == Func->end())
1351     return 0;
1352   return wrap(I);
1353 }
1354
1355 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1356   Function *Func = unwrap<Function>(Fn);
1357   Function::iterator I = Func->end();
1358   if (I == Func->begin())
1359     return 0;
1360   return wrap(--I);
1361 }
1362
1363 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1364   BasicBlock *Block = unwrap(BB);
1365   Function::iterator I = Block;
1366   if (++I == Block->getParent()->end())
1367     return 0;
1368   return wrap(I);
1369 }
1370
1371 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1372   BasicBlock *Block = unwrap(BB);
1373   Function::iterator I = Block;
1374   if (I == Block->getParent()->begin())
1375     return 0;
1376   return wrap(--I);
1377 }
1378
1379 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1380                                                 LLVMValueRef FnRef,
1381                                                 const char *Name) {
1382   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1383 }
1384
1385 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1386   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1387 }
1388
1389 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1390                                                 LLVMBasicBlockRef BBRef,
1391                                                 const char *Name) {
1392   BasicBlock *BB = unwrap(BBRef);
1393   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1394 }
1395
1396 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1397                                        const char *Name) {
1398   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1399 }
1400
1401 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1402   unwrap(BBRef)->eraseFromParent();
1403 }
1404
1405 /*--.. Operations on instructions ..........................................--*/
1406
1407 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1408   return wrap(unwrap<Instruction>(Inst)->getParent());
1409 }
1410
1411 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1412   BasicBlock *Block = unwrap(BB);
1413   BasicBlock::iterator I = Block->begin();
1414   if (I == Block->end())
1415     return 0;
1416   return wrap(I);
1417 }
1418
1419 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1420   BasicBlock *Block = unwrap(BB);
1421   BasicBlock::iterator I = Block->end();
1422   if (I == Block->begin())
1423     return 0;
1424   return wrap(--I);
1425 }
1426
1427 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1428   Instruction *Instr = unwrap<Instruction>(Inst);
1429   BasicBlock::iterator I = Instr;
1430   if (++I == Instr->getParent()->end())
1431     return 0;
1432   return wrap(I);
1433 }
1434
1435 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1436   Instruction *Instr = unwrap<Instruction>(Inst);
1437   BasicBlock::iterator I = Instr;
1438   if (I == Instr->getParent()->begin())
1439     return 0;
1440   return wrap(--I);
1441 }
1442
1443 /*--.. Call and invoke instructions ........................................--*/
1444
1445 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1446   Value *V = unwrap(Instr);
1447   if (CallInst *CI = dyn_cast<CallInst>(V))
1448     return CI->getCallingConv();
1449   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1450     return II->getCallingConv();
1451   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1452   return 0;
1453 }
1454
1455 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1456   Value *V = unwrap(Instr);
1457   if (CallInst *CI = dyn_cast<CallInst>(V))
1458     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1459   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1460     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1461   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1462 }
1463
1464 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1465                            LLVMAttribute PA) {
1466   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1467   Call.setAttributes(
1468     Call.getAttributes().addAttr(index, PA));
1469 }
1470
1471 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1472                               LLVMAttribute PA) {
1473   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1474   Call.setAttributes(
1475     Call.getAttributes().removeAttr(index, PA));
1476 }
1477
1478 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1479                                 unsigned align) {
1480   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1481   Call.setAttributes(
1482     Call.getAttributes().addAttr(index, 
1483         Attribute::constructAlignmentFromInt(align)));
1484 }
1485
1486 /*--.. Operations on call instructions (only) ..............................--*/
1487
1488 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1489   return unwrap<CallInst>(Call)->isTailCall();
1490 }
1491
1492 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1493   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1494 }
1495
1496 /*--.. Operations on phi nodes .............................................--*/
1497
1498 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1499                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1500   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1501   for (unsigned I = 0; I != Count; ++I)
1502     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1503 }
1504
1505 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1506   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1507 }
1508
1509 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1510   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1511 }
1512
1513 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1514   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1515 }
1516
1517
1518 /*===-- Instruction builders ----------------------------------------------===*/
1519
1520 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1521   return wrap(new IRBuilder<>(*unwrap(C)));
1522 }
1523
1524 LLVMBuilderRef LLVMCreateBuilder(void) {
1525   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1526 }
1527
1528 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1529                          LLVMValueRef Instr) {
1530   BasicBlock *BB = unwrap(Block);
1531   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1532   unwrap(Builder)->SetInsertPoint(BB, I);
1533 }
1534
1535 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1536   Instruction *I = unwrap<Instruction>(Instr);
1537   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1538 }
1539
1540 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1541   BasicBlock *BB = unwrap(Block);
1542   unwrap(Builder)->SetInsertPoint(BB);
1543 }
1544
1545 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1546    return wrap(unwrap(Builder)->GetInsertBlock());
1547 }
1548
1549 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1550   unwrap(Builder)->ClearInsertionPoint ();
1551 }
1552
1553 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1554   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1555 }
1556
1557 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1558                                    const char *Name) {
1559   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1560 }
1561
1562 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1563   delete unwrap(Builder);
1564 }
1565
1566 /*--.. Instruction builders ................................................--*/
1567
1568 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1569   return wrap(unwrap(B)->CreateRetVoid());
1570 }
1571
1572 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1573   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1574 }
1575
1576 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1577                                    unsigned N) {
1578   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1579 }
1580
1581 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1582   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1583 }
1584
1585 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1586                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1587   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1588 }
1589
1590 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1591                              LLVMBasicBlockRef Else, unsigned NumCases) {
1592   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1593 }
1594
1595 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1596                              LLVMValueRef *Args, unsigned NumArgs,
1597                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1598                              const char *Name) {
1599   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1600                                       unwrap(Args), unwrap(Args) + NumArgs,
1601                                       Name));
1602 }
1603
1604 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1605   return wrap(unwrap(B)->CreateUnwind());
1606 }
1607
1608 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1609   return wrap(unwrap(B)->CreateUnreachable());
1610 }
1611
1612 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1613                  LLVMBasicBlockRef Dest) {
1614   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1615 }
1616
1617 /*--.. Arithmetic ..........................................................--*/
1618
1619 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1620                           const char *Name) {
1621   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1622 }
1623
1624 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1625                           const char *Name) {
1626   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1627 }
1628
1629 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1630                           const char *Name) {
1631   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1632 }
1633
1634 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1635                           const char *Name) {
1636   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1637 }
1638
1639 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1640                           const char *Name) {
1641   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1642 }
1643
1644 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1645                           const char *Name) {
1646   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1647 }
1648
1649 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1650                           const char *Name) {
1651   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1652 }
1653
1654 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1655                            const char *Name) {
1656   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1657 }
1658
1659 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1660                            const char *Name) {
1661   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1662 }
1663
1664 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1665                                 LLVMValueRef RHS, const char *Name) {
1666   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1667 }
1668
1669 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1670                            const char *Name) {
1671   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1672 }
1673
1674 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1675                            const char *Name) {
1676   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1677 }
1678
1679 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1680                            const char *Name) {
1681   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1682 }
1683
1684 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1685                            const char *Name) {
1686   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1687 }
1688
1689 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1690                           const char *Name) {
1691   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1692 }
1693
1694 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1695                            const char *Name) {
1696   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1697 }
1698
1699 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1700                            const char *Name) {
1701   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1702 }
1703
1704 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1705                           const char *Name) {
1706   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1707 }
1708
1709 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1710                          const char *Name) {
1711   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1712 }
1713
1714 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1715                           const char *Name) {
1716   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1717 }
1718
1719 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1720                             LLVMValueRef LHS, LLVMValueRef RHS,
1721                             const char *Name) {
1722   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1723                                      unwrap(RHS), Name));
1724 }
1725
1726 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1727   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1728 }
1729
1730 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1731   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1732 }
1733
1734 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1735   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1736 }
1737
1738 /*--.. Memory ..............................................................--*/
1739
1740 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1741                              const char *Name) {
1742   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1743   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1744   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1745   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1746                                                ITy, unwrap(Ty), AllocSize, 
1747                                                0, 0, "");
1748   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1749 }
1750
1751 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1752                                   LLVMValueRef Val, const char *Name) {
1753   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1754   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1755   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1756   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1757                                                ITy, unwrap(Ty), AllocSize, 
1758                                                unwrap(Val), 0, "");
1759   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1760 }
1761
1762 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1763                              const char *Name) {
1764   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1765 }
1766
1767 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1768                                   LLVMValueRef Val, const char *Name) {
1769   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1770 }
1771
1772 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1773   return wrap(unwrap(B)->Insert(
1774      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1775 }
1776
1777
1778 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1779                            const char *Name) {
1780   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1781 }
1782
1783 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1784                             LLVMValueRef PointerVal) {
1785   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1786 }
1787
1788 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1789                           LLVMValueRef *Indices, unsigned NumIndices,
1790                           const char *Name) {
1791   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1792                                    unwrap(Indices) + NumIndices, Name));
1793 }
1794
1795 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1796                                   LLVMValueRef *Indices, unsigned NumIndices,
1797                                   const char *Name) {
1798   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1799                                            unwrap(Indices) + NumIndices, Name));
1800 }
1801
1802 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1803                                 unsigned Idx, const char *Name) {
1804   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1805 }
1806
1807 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1808                                    const char *Name) {
1809   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1810 }
1811
1812 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1813                                       const char *Name) {
1814   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1815 }
1816
1817 /*--.. Casts ...............................................................--*/
1818
1819 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1820                             LLVMTypeRef DestTy, const char *Name) {
1821   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1822 }
1823
1824 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1825                            LLVMTypeRef DestTy, const char *Name) {
1826   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1827 }
1828
1829 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1830                            LLVMTypeRef DestTy, const char *Name) {
1831   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1832 }
1833
1834 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1835                              LLVMTypeRef DestTy, const char *Name) {
1836   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1837 }
1838
1839 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1840                              LLVMTypeRef DestTy, const char *Name) {
1841   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1842 }
1843
1844 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1845                              LLVMTypeRef DestTy, const char *Name) {
1846   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1847 }
1848
1849 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1850                              LLVMTypeRef DestTy, const char *Name) {
1851   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1852 }
1853
1854 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1855                               LLVMTypeRef DestTy, const char *Name) {
1856   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1857 }
1858
1859 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1860                             LLVMTypeRef DestTy, const char *Name) {
1861   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1862 }
1863
1864 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1865                                LLVMTypeRef DestTy, const char *Name) {
1866   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1867 }
1868
1869 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1870                                LLVMTypeRef DestTy, const char *Name) {
1871   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1872 }
1873
1874 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1875                               LLVMTypeRef DestTy, const char *Name) {
1876   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1877 }
1878
1879 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1880                                     LLVMTypeRef DestTy, const char *Name) {
1881   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1882                                              Name));
1883 }
1884
1885 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1886                                     LLVMTypeRef DestTy, const char *Name) {
1887   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1888                                              Name));
1889 }
1890
1891 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1892                                      LLVMTypeRef DestTy, const char *Name) {
1893   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1894                                               Name));
1895 }
1896
1897 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
1898                            LLVMTypeRef DestTy, const char *Name) {
1899   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
1900                                     unwrap(DestTy), Name));
1901 }
1902
1903 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1904                                   LLVMTypeRef DestTy, const char *Name) {
1905   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1906 }
1907
1908 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1909                               LLVMTypeRef DestTy, const char *Name) {
1910   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
1911                                        /*isSigned*/true, Name));
1912 }
1913
1914 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1915                              LLVMTypeRef DestTy, const char *Name) {
1916   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
1917 }
1918
1919 /*--.. Comparisons .........................................................--*/
1920
1921 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1922                            LLVMValueRef LHS, LLVMValueRef RHS,
1923                            const char *Name) {
1924   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1925                                     unwrap(LHS), unwrap(RHS), Name));
1926 }
1927
1928 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1929                            LLVMValueRef LHS, LLVMValueRef RHS,
1930                            const char *Name) {
1931   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1932                                     unwrap(LHS), unwrap(RHS), Name));
1933 }
1934
1935 /*--.. Miscellaneous instructions ..........................................--*/
1936
1937 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1938   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1939 }
1940
1941 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1942                            LLVMValueRef *Args, unsigned NumArgs,
1943                            const char *Name) {
1944   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1945                                     unwrap(Args) + NumArgs, Name));
1946 }
1947
1948 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1949                              LLVMValueRef Then, LLVMValueRef Else,
1950                              const char *Name) {
1951   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1952                                       Name));
1953 }
1954
1955 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1956                             LLVMTypeRef Ty, const char *Name) {
1957   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1958 }
1959
1960 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1961                                       LLVMValueRef Index, const char *Name) {
1962   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1963                                               Name));
1964 }
1965
1966 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1967                                     LLVMValueRef EltVal, LLVMValueRef Index,
1968                                     const char *Name) {
1969   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1970                                              unwrap(Index), Name));
1971 }
1972
1973 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1974                                     LLVMValueRef V2, LLVMValueRef Mask,
1975                                     const char *Name) {
1976   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1977                                              unwrap(Mask), Name));
1978 }
1979
1980 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1981                                    unsigned Index, const char *Name) {
1982   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1983 }
1984
1985 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1986                                   LLVMValueRef EltVal, unsigned Index,
1987                                   const char *Name) {
1988   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1989                                            Index, Name));
1990 }
1991
1992 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
1993                              const char *Name) {
1994   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
1995 }
1996
1997 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
1998                                 const char *Name) {
1999   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2000 }
2001
2002 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2003                               LLVMValueRef RHS, const char *Name) {
2004   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2005 }
2006
2007
2008 /*===-- Module providers --------------------------------------------------===*/
2009
2010 LLVMModuleProviderRef
2011 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2012   return reinterpret_cast<LLVMModuleProviderRef>(M);
2013 }
2014
2015 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2016   delete unwrap(MP);
2017 }
2018
2019
2020 /*===-- Memory buffers ----------------------------------------------------===*/
2021
2022 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2023     const char *Path,
2024     LLVMMemoryBufferRef *OutMemBuf,
2025     char **OutMessage) {
2026
2027   std::string Error;
2028   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2029     *OutMemBuf = wrap(MB);
2030     return 0;
2031   }
2032   
2033   *OutMessage = strdup(Error.c_str());
2034   return 1;
2035 }
2036
2037 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2038                                          char **OutMessage) {
2039   MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2040   if (!MB->getBufferSize()) {
2041     delete MB;
2042     *OutMessage = strdup("stdin is empty.");
2043     return 1;
2044   }
2045
2046   *OutMemBuf = wrap(MB);
2047   return 0;
2048 }
2049
2050 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2051   delete unwrap(MemBuf);
2052 }