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