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