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