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