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