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