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