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