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