a47976f29cc28355b8349714c19f82e936591307
[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   AttributeSet::AttrIndex Idx =
1450     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1451   AttrBuilder B;
1452
1453   B.addAttribute(A, V);
1454   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1455   Func->addAttributes(Idx, Set);
1456 }
1457
1458 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1459   Function *Func = unwrap<Function>(Fn);
1460   const AttributeSet PAL = Func->getAttributes();
1461   AttrBuilder B(PA);
1462   const AttributeSet PALnew =
1463     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1464                          AttributeSet::get(Func->getContext(),
1465                                            AttributeSet::FunctionIndex, B));
1466   Func->setAttributes(PALnew);
1467 }
1468
1469 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1470   Function *Func = unwrap<Function>(Fn);
1471   const AttributeSet PAL = Func->getAttributes();
1472   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1473 }
1474
1475 /*--.. Operations on parameters ............................................--*/
1476
1477 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1478   // This function is strictly redundant to
1479   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1480   return unwrap<Function>(FnRef)->arg_size();
1481 }
1482
1483 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1484   Function *Fn = unwrap<Function>(FnRef);
1485   for (Function::arg_iterator I = Fn->arg_begin(),
1486                               E = Fn->arg_end(); I != E; I++)
1487     *ParamRefs++ = wrap(I);
1488 }
1489
1490 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1491   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1492   while (index --> 0)
1493     AI++;
1494   return wrap(AI);
1495 }
1496
1497 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1498   return wrap(unwrap<Argument>(V)->getParent());
1499 }
1500
1501 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1502   Function *Func = unwrap<Function>(Fn);
1503   Function::arg_iterator I = Func->arg_begin();
1504   if (I == Func->arg_end())
1505     return 0;
1506   return wrap(I);
1507 }
1508
1509 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1510   Function *Func = unwrap<Function>(Fn);
1511   Function::arg_iterator I = Func->arg_end();
1512   if (I == Func->arg_begin())
1513     return 0;
1514   return wrap(--I);
1515 }
1516
1517 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1518   Argument *A = unwrap<Argument>(Arg);
1519   Function::arg_iterator I = A;
1520   if (++I == A->getParent()->arg_end())
1521     return 0;
1522   return wrap(I);
1523 }
1524
1525 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1526   Argument *A = unwrap<Argument>(Arg);
1527   Function::arg_iterator I = A;
1528   if (I == A->getParent()->arg_begin())
1529     return 0;
1530   return wrap(--I);
1531 }
1532
1533 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1534   Argument *A = unwrap<Argument>(Arg);
1535   AttrBuilder B(PA);
1536   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
1537 }
1538
1539 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1540   Argument *A = unwrap<Argument>(Arg);
1541   AttrBuilder B(PA);
1542   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
1543 }
1544
1545 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1546   Argument *A = unwrap<Argument>(Arg);
1547   return (LLVMAttribute)A->getParent()->getAttributes().
1548     Raw(A->getArgNo()+1);
1549 }
1550   
1551
1552 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1553   Argument *A = unwrap<Argument>(Arg);
1554   AttrBuilder B;
1555   B.addAlignmentAttr(align);
1556   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1557 }
1558
1559 /*--.. Operations on basic blocks ..........................................--*/
1560
1561 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1562   return wrap(static_cast<Value*>(unwrap(BB)));
1563 }
1564
1565 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1566   return isa<BasicBlock>(unwrap(Val));
1567 }
1568
1569 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1570   return wrap(unwrap<BasicBlock>(Val));
1571 }
1572
1573 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1574   return wrap(unwrap(BB)->getParent());
1575 }
1576
1577 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1578   return wrap(unwrap(BB)->getTerminator());
1579 }
1580
1581 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1582   return unwrap<Function>(FnRef)->size();
1583 }
1584
1585 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1586   Function *Fn = unwrap<Function>(FnRef);
1587   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1588     *BasicBlocksRefs++ = wrap(I);
1589 }
1590
1591 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1592   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1593 }
1594
1595 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1596   Function *Func = unwrap<Function>(Fn);
1597   Function::iterator I = Func->begin();
1598   if (I == Func->end())
1599     return 0;
1600   return wrap(I);
1601 }
1602
1603 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1604   Function *Func = unwrap<Function>(Fn);
1605   Function::iterator I = Func->end();
1606   if (I == Func->begin())
1607     return 0;
1608   return wrap(--I);
1609 }
1610
1611 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1612   BasicBlock *Block = unwrap(BB);
1613   Function::iterator I = Block;
1614   if (++I == Block->getParent()->end())
1615     return 0;
1616   return wrap(I);
1617 }
1618
1619 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1620   BasicBlock *Block = unwrap(BB);
1621   Function::iterator I = Block;
1622   if (I == Block->getParent()->begin())
1623     return 0;
1624   return wrap(--I);
1625 }
1626
1627 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1628                                                 LLVMValueRef FnRef,
1629                                                 const char *Name) {
1630   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1631 }
1632
1633 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1634   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1635 }
1636
1637 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1638                                                 LLVMBasicBlockRef BBRef,
1639                                                 const char *Name) {
1640   BasicBlock *BB = unwrap(BBRef);
1641   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1642 }
1643
1644 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1645                                        const char *Name) {
1646   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1647 }
1648
1649 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1650   unwrap(BBRef)->eraseFromParent();
1651 }
1652
1653 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1654   unwrap(BBRef)->removeFromParent();
1655 }
1656
1657 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1658   unwrap(BB)->moveBefore(unwrap(MovePos));
1659 }
1660
1661 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1662   unwrap(BB)->moveAfter(unwrap(MovePos));
1663 }
1664
1665 /*--.. Operations on instructions ..........................................--*/
1666
1667 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1668   return wrap(unwrap<Instruction>(Inst)->getParent());
1669 }
1670
1671 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1672   BasicBlock *Block = unwrap(BB);
1673   BasicBlock::iterator I = Block->begin();
1674   if (I == Block->end())
1675     return 0;
1676   return wrap(I);
1677 }
1678
1679 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1680   BasicBlock *Block = unwrap(BB);
1681   BasicBlock::iterator I = Block->end();
1682   if (I == Block->begin())
1683     return 0;
1684   return wrap(--I);
1685 }
1686
1687 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1688   Instruction *Instr = unwrap<Instruction>(Inst);
1689   BasicBlock::iterator I = Instr;
1690   if (++I == Instr->getParent()->end())
1691     return 0;
1692   return wrap(I);
1693 }
1694
1695 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1696   Instruction *Instr = unwrap<Instruction>(Inst);
1697   BasicBlock::iterator I = Instr;
1698   if (I == Instr->getParent()->begin())
1699     return 0;
1700   return wrap(--I);
1701 }
1702
1703 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1704   unwrap<Instruction>(Inst)->eraseFromParent();
1705 }
1706
1707 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1708   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1709     return (LLVMIntPredicate)I->getPredicate();
1710   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1711     if (CE->getOpcode() == Instruction::ICmp)
1712       return (LLVMIntPredicate)CE->getPredicate();
1713   return (LLVMIntPredicate)0;
1714 }
1715
1716 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1717   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1718     return map_to_llvmopcode(C->getOpcode());
1719   return (LLVMOpcode)0;
1720 }
1721
1722 /*--.. Call and invoke instructions ........................................--*/
1723
1724 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1725   Value *V = unwrap(Instr);
1726   if (CallInst *CI = dyn_cast<CallInst>(V))
1727     return CI->getCallingConv();
1728   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1729     return II->getCallingConv();
1730   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1731 }
1732
1733 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1734   Value *V = unwrap(Instr);
1735   if (CallInst *CI = dyn_cast<CallInst>(V))
1736     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1737   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1738     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1739   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1740 }
1741
1742 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1743                            LLVMAttribute PA) {
1744   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1745   AttrBuilder B(PA);
1746   Call.setAttributes(
1747     Call.getAttributes().addAttributes(Call->getContext(), index,
1748                                        AttributeSet::get(Call->getContext(),
1749                                                          index, B)));
1750 }
1751
1752 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1753                               LLVMAttribute PA) {
1754   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1755   AttrBuilder B(PA);
1756   Call.setAttributes(Call.getAttributes()
1757                        .removeAttributes(Call->getContext(), index,
1758                                          AttributeSet::get(Call->getContext(),
1759                                                            index, B)));
1760 }
1761
1762 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1763                                 unsigned align) {
1764   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1765   AttrBuilder B;
1766   B.addAlignmentAttr(align);
1767   Call.setAttributes(Call.getAttributes()
1768                        .addAttributes(Call->getContext(), index,
1769                                       AttributeSet::get(Call->getContext(),
1770                                                         index, B)));
1771 }
1772
1773 /*--.. Operations on call instructions (only) ..............................--*/
1774
1775 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1776   return unwrap<CallInst>(Call)->isTailCall();
1777 }
1778
1779 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1780   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1781 }
1782
1783 /*--.. Operations on switch instructions (only) ............................--*/
1784
1785 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1786   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1787 }
1788
1789 /*--.. Operations on phi nodes .............................................--*/
1790
1791 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1792                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1793   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1794   for (unsigned I = 0; I != Count; ++I)
1795     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1796 }
1797
1798 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1799   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1800 }
1801
1802 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1803   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1804 }
1805
1806 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1807   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1808 }
1809
1810
1811 /*===-- Instruction builders ----------------------------------------------===*/
1812
1813 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1814   return wrap(new IRBuilder<>(*unwrap(C)));
1815 }
1816
1817 LLVMBuilderRef LLVMCreateBuilder(void) {
1818   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1819 }
1820
1821 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1822                          LLVMValueRef Instr) {
1823   BasicBlock *BB = unwrap(Block);
1824   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1825   unwrap(Builder)->SetInsertPoint(BB, I);
1826 }
1827
1828 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1829   Instruction *I = unwrap<Instruction>(Instr);
1830   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1831 }
1832
1833 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1834   BasicBlock *BB = unwrap(Block);
1835   unwrap(Builder)->SetInsertPoint(BB);
1836 }
1837
1838 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1839    return wrap(unwrap(Builder)->GetInsertBlock());
1840 }
1841
1842 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1843   unwrap(Builder)->ClearInsertionPoint();
1844 }
1845
1846 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1847   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1848 }
1849
1850 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1851                                    const char *Name) {
1852   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1853 }
1854
1855 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1856   delete unwrap(Builder);
1857 }
1858
1859 /*--.. Metadata builders ...................................................--*/
1860
1861 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1862   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1863   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1864 }
1865
1866 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1867   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1868               .getAsMDNode(unwrap(Builder)->getContext()));
1869 }
1870
1871 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1872   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1873 }
1874
1875
1876 /*--.. Instruction builders ................................................--*/
1877
1878 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1879   return wrap(unwrap(B)->CreateRetVoid());
1880 }
1881
1882 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1883   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1884 }
1885
1886 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1887                                    unsigned N) {
1888   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1889 }
1890
1891 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1892   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1893 }
1894
1895 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1896                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1897   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1898 }
1899
1900 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1901                              LLVMBasicBlockRef Else, unsigned NumCases) {
1902   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1903 }
1904
1905 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1906                                  unsigned NumDests) {
1907   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1908 }
1909
1910 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1911                              LLVMValueRef *Args, unsigned NumArgs,
1912                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1913                              const char *Name) {
1914   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1915                                       makeArrayRef(unwrap(Args), NumArgs),
1916                                       Name));
1917 }
1918
1919 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1920                                  LLVMValueRef PersFn, unsigned NumClauses,
1921                                  const char *Name) {
1922   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1923                                           cast<Function>(unwrap(PersFn)),
1924                                           NumClauses, Name));
1925 }
1926
1927 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1928   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1929 }
1930
1931 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1932   return wrap(unwrap(B)->CreateUnreachable());
1933 }
1934
1935 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1936                  LLVMBasicBlockRef Dest) {
1937   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1938 }
1939
1940 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1941   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1942 }
1943
1944 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1945   unwrap<LandingPadInst>(LandingPad)->
1946     addClause(cast<Constant>(unwrap(ClauseVal)));
1947 }
1948
1949 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1950   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1951 }
1952
1953 /*--.. Arithmetic ..........................................................--*/
1954
1955 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1956                           const char *Name) {
1957   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1958 }
1959
1960 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1961                           const char *Name) {
1962   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1963 }
1964
1965 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1966                           const char *Name) {
1967   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1968 }
1969
1970 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1971                           const char *Name) {
1972   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1973 }
1974
1975 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1976                           const char *Name) {
1977   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1978 }
1979
1980 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1981                           const char *Name) {
1982   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1983 }
1984
1985 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1986                           const char *Name) {
1987   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1988 }
1989
1990 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1991                           const char *Name) {
1992   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1993 }
1994
1995 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1996                           const char *Name) {
1997   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1998 }
1999
2000 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2001                           const char *Name) {
2002   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2003 }
2004
2005 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2006                           const char *Name) {
2007   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2008 }
2009
2010 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2011                           const char *Name) {
2012   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2013 }
2014
2015 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2016                            const char *Name) {
2017   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2018 }
2019
2020 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2021                            const char *Name) {
2022   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2023 }
2024
2025 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2026                                 LLVMValueRef RHS, const char *Name) {
2027   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2028 }
2029
2030 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2031                            const char *Name) {
2032   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2033 }
2034
2035 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2036                            const char *Name) {
2037   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2038 }
2039
2040 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2041                            const char *Name) {
2042   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2043 }
2044
2045 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2046                            const char *Name) {
2047   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2048 }
2049
2050 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2051                           const char *Name) {
2052   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2053 }
2054
2055 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2056                            const char *Name) {
2057   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2058 }
2059
2060 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2061                            const char *Name) {
2062   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2063 }
2064
2065 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2066                           const char *Name) {
2067   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2068 }
2069
2070 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2071                          const char *Name) {
2072   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2073 }
2074
2075 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2076                           const char *Name) {
2077   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2078 }
2079
2080 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2081                             LLVMValueRef LHS, LLVMValueRef RHS,
2082                             const char *Name) {
2083   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2084                                      unwrap(RHS), Name));
2085 }
2086
2087 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2088   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2089 }
2090
2091 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2092                              const char *Name) {
2093   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2094 }
2095
2096 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2097                              const char *Name) {
2098   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2099 }
2100
2101 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2102   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2103 }
2104
2105 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2106   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2107 }
2108
2109 /*--.. Memory ..............................................................--*/
2110
2111 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2112                              const char *Name) {
2113   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2114   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2115   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2116   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
2117                                                ITy, unwrap(Ty), AllocSize, 
2118                                                0, 0, "");
2119   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2120 }
2121
2122 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2123                                   LLVMValueRef Val, const char *Name) {
2124   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2125   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2126   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2127   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
2128                                                ITy, unwrap(Ty), AllocSize, 
2129                                                unwrap(Val), 0, "");
2130   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2131 }
2132
2133 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2134                              const char *Name) {
2135   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2136 }
2137
2138 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2139                                   LLVMValueRef Val, const char *Name) {
2140   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2141 }
2142
2143 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2144   return wrap(unwrap(B)->Insert(
2145      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2146 }
2147
2148
2149 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2150                            const char *Name) {
2151   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2152 }
2153
2154 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
2155                             LLVMValueRef PointerVal) {
2156   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2157 }
2158
2159 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2160                           LLVMValueRef *Indices, unsigned NumIndices,
2161                           const char *Name) {
2162   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2163   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2164 }
2165
2166 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2167                                   LLVMValueRef *Indices, unsigned NumIndices,
2168                                   const char *Name) {
2169   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2170   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2171 }
2172
2173 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2174                                 unsigned Idx, const char *Name) {
2175   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2176 }
2177
2178 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2179                                    const char *Name) {
2180   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2181 }
2182
2183 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2184                                       const char *Name) {
2185   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2186 }
2187
2188 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2189   Value *P = unwrap<Value>(MemAccessInst);
2190   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2191     return LI->isVolatile();
2192   return cast<StoreInst>(P)->isVolatile();
2193 }
2194
2195 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2196   Value *P = unwrap<Value>(MemAccessInst);
2197   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2198     return LI->setVolatile(isVolatile);
2199   return cast<StoreInst>(P)->setVolatile(isVolatile);
2200 }
2201
2202 /*--.. Casts ...............................................................--*/
2203
2204 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2205                             LLVMTypeRef DestTy, const char *Name) {
2206   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2207 }
2208
2209 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2210                            LLVMTypeRef DestTy, const char *Name) {
2211   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2212 }
2213
2214 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2215                            LLVMTypeRef DestTy, const char *Name) {
2216   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2217 }
2218
2219 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2220                              LLVMTypeRef DestTy, const char *Name) {
2221   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2222 }
2223
2224 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2225                              LLVMTypeRef DestTy, const char *Name) {
2226   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2227 }
2228
2229 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2230                              LLVMTypeRef DestTy, const char *Name) {
2231   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2232 }
2233
2234 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2235                              LLVMTypeRef DestTy, const char *Name) {
2236   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2237 }
2238
2239 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2240                               LLVMTypeRef DestTy, const char *Name) {
2241   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2242 }
2243
2244 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2245                             LLVMTypeRef DestTy, const char *Name) {
2246   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2247 }
2248
2249 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2250                                LLVMTypeRef DestTy, const char *Name) {
2251   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2252 }
2253
2254 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2255                                LLVMTypeRef DestTy, const char *Name) {
2256   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2257 }
2258
2259 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2260                               LLVMTypeRef DestTy, const char *Name) {
2261   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2262 }
2263
2264 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2265                                     LLVMTypeRef DestTy, const char *Name) {
2266   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2267                                              Name));
2268 }
2269
2270 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2271                                     LLVMTypeRef DestTy, const char *Name) {
2272   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2273                                              Name));
2274 }
2275
2276 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2277                                      LLVMTypeRef DestTy, const char *Name) {
2278   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2279                                               Name));
2280 }
2281
2282 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2283                            LLVMTypeRef DestTy, const char *Name) {
2284   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2285                                     unwrap(DestTy), Name));
2286 }
2287
2288 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2289                                   LLVMTypeRef DestTy, const char *Name) {
2290   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2291 }
2292
2293 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2294                               LLVMTypeRef DestTy, const char *Name) {
2295   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2296                                        /*isSigned*/true, Name));
2297 }
2298
2299 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2300                              LLVMTypeRef DestTy, const char *Name) {
2301   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2302 }
2303
2304 /*--.. Comparisons .........................................................--*/
2305
2306 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2307                            LLVMValueRef LHS, LLVMValueRef RHS,
2308                            const char *Name) {
2309   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2310                                     unwrap(LHS), unwrap(RHS), Name));
2311 }
2312
2313 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2314                            LLVMValueRef LHS, LLVMValueRef RHS,
2315                            const char *Name) {
2316   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2317                                     unwrap(LHS), unwrap(RHS), Name));
2318 }
2319
2320 /*--.. Miscellaneous instructions ..........................................--*/
2321
2322 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2323   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2324 }
2325
2326 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2327                            LLVMValueRef *Args, unsigned NumArgs,
2328                            const char *Name) {
2329   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2330                                     makeArrayRef(unwrap(Args), NumArgs),
2331                                     Name));
2332 }
2333
2334 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2335                              LLVMValueRef Then, LLVMValueRef Else,
2336                              const char *Name) {
2337   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2338                                       Name));
2339 }
2340
2341 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2342                             LLVMTypeRef Ty, const char *Name) {
2343   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2344 }
2345
2346 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2347                                       LLVMValueRef Index, const char *Name) {
2348   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2349                                               Name));
2350 }
2351
2352 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2353                                     LLVMValueRef EltVal, LLVMValueRef Index,
2354                                     const char *Name) {
2355   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2356                                              unwrap(Index), Name));
2357 }
2358
2359 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2360                                     LLVMValueRef V2, LLVMValueRef Mask,
2361                                     const char *Name) {
2362   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2363                                              unwrap(Mask), Name));
2364 }
2365
2366 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2367                                    unsigned Index, const char *Name) {
2368   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2369 }
2370
2371 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2372                                   LLVMValueRef EltVal, unsigned Index,
2373                                   const char *Name) {
2374   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2375                                            Index, Name));
2376 }
2377
2378 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2379                              const char *Name) {
2380   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2381 }
2382
2383 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2384                                 const char *Name) {
2385   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2386 }
2387
2388 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2389                               LLVMValueRef RHS, const char *Name) {
2390   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2391 }
2392
2393
2394 /*===-- Module providers --------------------------------------------------===*/
2395
2396 LLVMModuleProviderRef
2397 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2398   return reinterpret_cast<LLVMModuleProviderRef>(M);
2399 }
2400
2401 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2402   delete unwrap(MP);
2403 }
2404
2405
2406 /*===-- Memory buffers ----------------------------------------------------===*/
2407
2408 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2409     const char *Path,
2410     LLVMMemoryBufferRef *OutMemBuf,
2411     char **OutMessage) {
2412
2413   OwningPtr<MemoryBuffer> MB;
2414   error_code ec;
2415   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2416     *OutMemBuf = wrap(MB.take());
2417     return 0;
2418   }
2419
2420   *OutMessage = strdup(ec.message().c_str());
2421   return 1;
2422 }
2423
2424 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2425                                          char **OutMessage) {
2426   OwningPtr<MemoryBuffer> MB;
2427   error_code ec;
2428   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2429     *OutMemBuf = wrap(MB.take());
2430     return 0;
2431   }
2432
2433   *OutMessage = strdup(ec.message().c_str());
2434   return 1;
2435 }
2436
2437 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2438     const char *InputData,
2439     size_t InputDataLength,
2440     const char *BufferName,
2441     LLVMBool RequiresNullTerminator) {
2442
2443   return wrap(MemoryBuffer::getMemBuffer(
2444       StringRef(InputData, InputDataLength),
2445       StringRef(BufferName),
2446       RequiresNullTerminator));
2447 }
2448
2449 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2450     const char *InputData,
2451     size_t InputDataLength,
2452     const char *BufferName) {
2453
2454   return wrap(MemoryBuffer::getMemBufferCopy(
2455       StringRef(InputData, InputDataLength),
2456       StringRef(BufferName)));
2457 }
2458
2459 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2460   return unwrap(MemBuf)->getBufferStart();
2461 }
2462
2463 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2464   return unwrap(MemBuf)->getBufferSize();
2465 }
2466
2467 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2468   delete unwrap(MemBuf);
2469 }
2470
2471 /*===-- Pass Registry -----------------------------------------------------===*/
2472
2473 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2474   return wrap(PassRegistry::getPassRegistry());
2475 }
2476
2477 /*===-- Pass Manager ------------------------------------------------------===*/
2478
2479 LLVMPassManagerRef LLVMCreatePassManager() {
2480   return wrap(new PassManager());
2481 }
2482
2483 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2484   return wrap(new FunctionPassManager(unwrap(M)));
2485 }
2486
2487 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2488   return LLVMCreateFunctionPassManagerForModule(
2489                                             reinterpret_cast<LLVMModuleRef>(P));
2490 }
2491
2492 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2493   return unwrap<PassManager>(PM)->run(*unwrap(M));
2494 }
2495
2496 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2497   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2498 }
2499
2500 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2501   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2502 }
2503
2504 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2505   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2506 }
2507
2508 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2509   delete unwrap(PM);
2510 }
2511
2512 /*===-- Threading ------------------------------------------------------===*/
2513
2514 LLVMBool LLVMStartMultithreaded() {
2515   return llvm_start_multithreaded();
2516 }
2517
2518 void LLVMStopMultithreaded() {
2519   llvm_stop_multithreaded();
2520 }
2521
2522 LLVMBool LLVMIsMultithreaded() {
2523   return llvm_is_multithreaded();
2524 }