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