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