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