Add initialization routines for VMCore.
[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 LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
551                                   uint8_t Radix) {
552   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
553                                Radix));
554 }
555
556 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
557                                          unsigned SLen, uint8_t Radix) {
558   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
559                                Radix));
560 }
561
562 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
563   return wrap(ConstantFP::get(unwrap(RealTy), N));
564 }
565
566 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
567   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
568 }
569
570 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
571                                           unsigned SLen) {
572   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
573 }
574
575 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
576   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
577 }
578
579 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
580   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
581 }
582
583 /*--.. Operations on composite constants ...................................--*/
584
585 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
586                                       unsigned Length,
587                                       LLVMBool DontNullTerminate) {
588   /* Inverted the sense of AddNull because ', 0)' is a
589      better mnemonic for null termination than ', 1)'. */
590   return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
591                                  DontNullTerminate == 0));
592 }
593 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
594                                       LLVMValueRef *ConstantVals,
595                                       unsigned Count, LLVMBool Packed) {
596   return wrap(ConstantStruct::get(*unwrap(C),
597                                   unwrap<Constant>(ConstantVals, Count),
598                                   Count, Packed != 0));
599 }
600
601 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
602                              LLVMBool DontNullTerminate) {
603   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
604                                   DontNullTerminate);
605 }
606 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
607                             LLVMValueRef *ConstantVals, unsigned Length) {
608   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
609                                  unwrap<Constant>(ConstantVals, Length),
610                                  Length));
611 }
612 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
613                              LLVMBool Packed) {
614   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
615                                   Packed);
616 }
617 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
618   return wrap(ConstantVector::get(
619                             unwrap<Constant>(ScalarConstantVals, Size), Size));
620 }
621 /*--.. Constant expressions ................................................--*/
622
623 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
624   return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
625 }
626
627 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
628   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
629 }
630
631 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
632   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
633 }
634
635 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
636   return wrap(ConstantExpr::getNeg(
637                                    unwrap<Constant>(ConstantVal)));
638 }
639
640 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
641   return wrap(ConstantExpr::getNSWNeg(
642                                       unwrap<Constant>(ConstantVal)));
643 }
644
645 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
646   return wrap(ConstantExpr::getNUWNeg(
647                                       unwrap<Constant>(ConstantVal)));
648 }
649
650
651 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
652   return wrap(ConstantExpr::getFNeg(
653                                     unwrap<Constant>(ConstantVal)));
654 }
655
656 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
657   return wrap(ConstantExpr::getNot(
658                                    unwrap<Constant>(ConstantVal)));
659 }
660
661 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
662   return wrap(ConstantExpr::getAdd(
663                                    unwrap<Constant>(LHSConstant),
664                                    unwrap<Constant>(RHSConstant)));
665 }
666
667 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
668                              LLVMValueRef RHSConstant) {
669   return wrap(ConstantExpr::getNSWAdd(
670                                       unwrap<Constant>(LHSConstant),
671                                       unwrap<Constant>(RHSConstant)));
672 }
673
674 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
675                              LLVMValueRef RHSConstant) {
676   return wrap(ConstantExpr::getNUWAdd(
677                                       unwrap<Constant>(LHSConstant),
678                                       unwrap<Constant>(RHSConstant)));
679 }
680
681 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
682   return wrap(ConstantExpr::getFAdd(
683                                     unwrap<Constant>(LHSConstant),
684                                     unwrap<Constant>(RHSConstant)));
685 }
686
687 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
688   return wrap(ConstantExpr::getSub(
689                                    unwrap<Constant>(LHSConstant),
690                                    unwrap<Constant>(RHSConstant)));
691 }
692
693 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
694                              LLVMValueRef RHSConstant) {
695   return wrap(ConstantExpr::getNSWSub(
696                                       unwrap<Constant>(LHSConstant),
697                                       unwrap<Constant>(RHSConstant)));
698 }
699
700 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
701                              LLVMValueRef RHSConstant) {
702   return wrap(ConstantExpr::getNUWSub(
703                                       unwrap<Constant>(LHSConstant),
704                                       unwrap<Constant>(RHSConstant)));
705 }
706
707 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
708   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
709                                     unwrap<Constant>(RHSConstant)));
710 }
711
712 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
713   return wrap(ConstantExpr::getMul(
714                                    unwrap<Constant>(LHSConstant),
715                                    unwrap<Constant>(RHSConstant)));
716 }
717
718 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
719                              LLVMValueRef RHSConstant) {
720   return wrap(ConstantExpr::getNSWMul(
721                                       unwrap<Constant>(LHSConstant),
722                                       unwrap<Constant>(RHSConstant)));
723 }
724
725 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
726                              LLVMValueRef RHSConstant) {
727   return wrap(ConstantExpr::getNUWMul(
728                                       unwrap<Constant>(LHSConstant),
729                                       unwrap<Constant>(RHSConstant)));
730 }
731
732 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
733   return wrap(ConstantExpr::getFMul(
734                                     unwrap<Constant>(LHSConstant),
735                                     unwrap<Constant>(RHSConstant)));
736 }
737
738 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
739   return wrap(ConstantExpr::getUDiv(
740                                     unwrap<Constant>(LHSConstant),
741                                     unwrap<Constant>(RHSConstant)));
742 }
743
744 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
745   return wrap(ConstantExpr::getSDiv(
746                                     unwrap<Constant>(LHSConstant),
747                                     unwrap<Constant>(RHSConstant)));
748 }
749
750 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
751                                 LLVMValueRef RHSConstant) {
752   return wrap(ConstantExpr::getExactSDiv(
753                                          unwrap<Constant>(LHSConstant),
754                                          unwrap<Constant>(RHSConstant)));
755 }
756
757 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758   return wrap(ConstantExpr::getFDiv(
759                                     unwrap<Constant>(LHSConstant),
760                                     unwrap<Constant>(RHSConstant)));
761 }
762
763 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
764   return wrap(ConstantExpr::getURem(
765                                     unwrap<Constant>(LHSConstant),
766                                     unwrap<Constant>(RHSConstant)));
767 }
768
769 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
770   return wrap(ConstantExpr::getSRem(
771                                     unwrap<Constant>(LHSConstant),
772                                     unwrap<Constant>(RHSConstant)));
773 }
774
775 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
776   return wrap(ConstantExpr::getFRem(
777                                     unwrap<Constant>(LHSConstant),
778                                     unwrap<Constant>(RHSConstant)));
779 }
780
781 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
782   return wrap(ConstantExpr::getAnd(
783                                    unwrap<Constant>(LHSConstant),
784                                    unwrap<Constant>(RHSConstant)));
785 }
786
787 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
788   return wrap(ConstantExpr::getOr(
789                                   unwrap<Constant>(LHSConstant),
790                                   unwrap<Constant>(RHSConstant)));
791 }
792
793 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
794   return wrap(ConstantExpr::getXor(
795                                    unwrap<Constant>(LHSConstant),
796                                    unwrap<Constant>(RHSConstant)));
797 }
798
799 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
800                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
801   return wrap(ConstantExpr::getICmp(Predicate,
802                                     unwrap<Constant>(LHSConstant),
803                                     unwrap<Constant>(RHSConstant)));
804 }
805
806 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
807                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
808   return wrap(ConstantExpr::getFCmp(Predicate,
809                                     unwrap<Constant>(LHSConstant),
810                                     unwrap<Constant>(RHSConstant)));
811 }
812
813 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
814   return wrap(ConstantExpr::getShl(
815                                   unwrap<Constant>(LHSConstant),
816                                   unwrap<Constant>(RHSConstant)));
817 }
818
819 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
820   return wrap(ConstantExpr::getLShr(
821                                     unwrap<Constant>(LHSConstant),
822                                     unwrap<Constant>(RHSConstant)));
823 }
824
825 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
826   return wrap(ConstantExpr::getAShr(
827                                     unwrap<Constant>(LHSConstant),
828                                     unwrap<Constant>(RHSConstant)));
829 }
830
831 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
832                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
833   return wrap(ConstantExpr::getGetElementPtr(
834                                              unwrap<Constant>(ConstantVal),
835                                              unwrap<Constant>(ConstantIndices, 
836                                                               NumIndices),
837                                              NumIndices));
838 }
839
840 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
841                                   LLVMValueRef *ConstantIndices,
842                                   unsigned NumIndices) {
843   Constant* Val = unwrap<Constant>(ConstantVal);
844   Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
845   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
846 }
847
848 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
849   return wrap(ConstantExpr::getTrunc(
850                                      unwrap<Constant>(ConstantVal),
851                                      unwrap(ToType)));
852 }
853
854 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
855   return wrap(ConstantExpr::getSExt(
856                                     unwrap<Constant>(ConstantVal),
857                                     unwrap(ToType)));
858 }
859
860 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
861   return wrap(ConstantExpr::getZExt(
862                                     unwrap<Constant>(ConstantVal),
863                                     unwrap(ToType)));
864 }
865
866 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
867   return wrap(ConstantExpr::getFPTrunc(
868                                        unwrap<Constant>(ConstantVal),
869                                        unwrap(ToType)));
870 }
871
872 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
873   return wrap(ConstantExpr::getFPExtend(
874                                         unwrap<Constant>(ConstantVal),
875                                         unwrap(ToType)));
876 }
877
878 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
879   return wrap(ConstantExpr::getUIToFP(
880                                       unwrap<Constant>(ConstantVal),
881                                       unwrap(ToType)));
882 }
883
884 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
885   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
886                                       unwrap(ToType)));
887 }
888
889 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
890   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
891                                       unwrap(ToType)));
892 }
893
894 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
895   return wrap(ConstantExpr::getFPToSI(
896                                       unwrap<Constant>(ConstantVal),
897                                       unwrap(ToType)));
898 }
899
900 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
901   return wrap(ConstantExpr::getPtrToInt(
902                                         unwrap<Constant>(ConstantVal),
903                                         unwrap(ToType)));
904 }
905
906 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
907   return wrap(ConstantExpr::getIntToPtr(
908                                         unwrap<Constant>(ConstantVal),
909                                         unwrap(ToType)));
910 }
911
912 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
913   return wrap(ConstantExpr::getBitCast(
914                                        unwrap<Constant>(ConstantVal),
915                                        unwrap(ToType)));
916 }
917
918 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
919                                     LLVMTypeRef ToType) {
920   return wrap(ConstantExpr::getZExtOrBitCast(
921                                              unwrap<Constant>(ConstantVal),
922                                              unwrap(ToType)));
923 }
924
925 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
926                                     LLVMTypeRef ToType) {
927   return wrap(ConstantExpr::getSExtOrBitCast(
928                                              unwrap<Constant>(ConstantVal),
929                                              unwrap(ToType)));
930 }
931
932 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
933                                      LLVMTypeRef ToType) {
934   return wrap(ConstantExpr::getTruncOrBitCast(
935                                               unwrap<Constant>(ConstantVal),
936                                               unwrap(ToType)));
937 }
938
939 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
940                                   LLVMTypeRef ToType) {
941   return wrap(ConstantExpr::getPointerCast(
942                                            unwrap<Constant>(ConstantVal),
943                                            unwrap(ToType)));
944 }
945
946 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
947                               LLVMBool isSigned) {
948   return wrap(ConstantExpr::getIntegerCast(
949                                            unwrap<Constant>(ConstantVal),
950                                            unwrap(ToType),
951                                            isSigned));
952 }
953
954 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
955   return wrap(ConstantExpr::getFPCast(
956                                       unwrap<Constant>(ConstantVal),
957                                       unwrap(ToType)));
958 }
959
960 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
961                              LLVMValueRef ConstantIfTrue,
962                              LLVMValueRef ConstantIfFalse) {
963   return wrap(ConstantExpr::getSelect(
964                                       unwrap<Constant>(ConstantCondition),
965                                       unwrap<Constant>(ConstantIfTrue),
966                                       unwrap<Constant>(ConstantIfFalse)));
967 }
968
969 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
970                                      LLVMValueRef IndexConstant) {
971   return wrap(ConstantExpr::getExtractElement(
972                                               unwrap<Constant>(VectorConstant),
973                                               unwrap<Constant>(IndexConstant)));
974 }
975
976 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
977                                     LLVMValueRef ElementValueConstant,
978                                     LLVMValueRef IndexConstant) {
979   return wrap(ConstantExpr::getInsertElement(
980                                          unwrap<Constant>(VectorConstant),
981                                          unwrap<Constant>(ElementValueConstant),
982                                              unwrap<Constant>(IndexConstant)));
983 }
984
985 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
986                                     LLVMValueRef VectorBConstant,
987                                     LLVMValueRef MaskConstant) {
988   return wrap(ConstantExpr::getShuffleVector(
989                                              unwrap<Constant>(VectorAConstant),
990                                              unwrap<Constant>(VectorBConstant),
991                                              unwrap<Constant>(MaskConstant)));
992 }
993
994 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
995                                    unsigned NumIdx) {
996   return wrap(ConstantExpr::getExtractValue(
997                                             unwrap<Constant>(AggConstant),
998                                             IdxList, NumIdx));
999 }
1000
1001 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1002                                   LLVMValueRef ElementValueConstant,
1003                                   unsigned *IdxList, unsigned NumIdx) {
1004   return wrap(ConstantExpr::getInsertValue(
1005                                          unwrap<Constant>(AggConstant),
1006                                          unwrap<Constant>(ElementValueConstant),
1007                                            IdxList, NumIdx));
1008 }
1009
1010 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1011                                 const char *Constraints,
1012                                 LLVMBool HasSideEffects,
1013                                 LLVMBool IsAlignStack) {
1014   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1015                              Constraints, HasSideEffects, IsAlignStack));
1016 }
1017
1018 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1019   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1020 }
1021
1022 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1023
1024 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1025   return wrap(unwrap<GlobalValue>(Global)->getParent());
1026 }
1027
1028 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1029   return unwrap<GlobalValue>(Global)->isDeclaration();
1030 }
1031
1032 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1033   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1034   default:
1035     assert(false && "Unhandled Linkage Type.");
1036   case GlobalValue::ExternalLinkage:
1037     return LLVMExternalLinkage;
1038   case GlobalValue::AvailableExternallyLinkage:
1039     return LLVMAvailableExternallyLinkage;
1040   case GlobalValue::LinkOnceAnyLinkage:
1041     return LLVMLinkOnceAnyLinkage;
1042   case GlobalValue::LinkOnceODRLinkage:
1043     return LLVMLinkOnceODRLinkage;
1044   case GlobalValue::WeakAnyLinkage:
1045     return LLVMWeakAnyLinkage;
1046   case GlobalValue::WeakODRLinkage:
1047     return LLVMWeakODRLinkage;
1048   case GlobalValue::AppendingLinkage:
1049     return LLVMAppendingLinkage;
1050   case GlobalValue::InternalLinkage:
1051     return LLVMInternalLinkage;
1052   case GlobalValue::PrivateLinkage:
1053     return LLVMPrivateLinkage;
1054   case GlobalValue::LinkerPrivateLinkage:
1055     return LLVMLinkerPrivateLinkage;
1056   case GlobalValue::LinkerPrivateWeakLinkage:
1057     return LLVMLinkerPrivateWeakLinkage;
1058   case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1059     return LLVMLinkerPrivateWeakDefAutoLinkage;
1060   case GlobalValue::DLLImportLinkage:
1061     return LLVMDLLImportLinkage;
1062   case GlobalValue::DLLExportLinkage:
1063     return LLVMDLLExportLinkage;
1064   case GlobalValue::ExternalWeakLinkage:
1065     return LLVMExternalWeakLinkage;
1066   case GlobalValue::CommonLinkage:
1067     return LLVMCommonLinkage;
1068   }
1069
1070   // Should never get here.
1071   return static_cast<LLVMLinkage>(0);
1072 }
1073
1074 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1075   GlobalValue *GV = unwrap<GlobalValue>(Global);
1076
1077   switch (Linkage) {
1078   default:
1079     assert(false && "Unhandled Linkage Type.");
1080   case LLVMExternalLinkage:
1081     GV->setLinkage(GlobalValue::ExternalLinkage);
1082     break;
1083   case LLVMAvailableExternallyLinkage:
1084     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1085     break;
1086   case LLVMLinkOnceAnyLinkage:
1087     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1088     break;
1089   case LLVMLinkOnceODRLinkage:
1090     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1091     break;
1092   case LLVMWeakAnyLinkage:
1093     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1094     break;
1095   case LLVMWeakODRLinkage:
1096     GV->setLinkage(GlobalValue::WeakODRLinkage);
1097     break;
1098   case LLVMAppendingLinkage:
1099     GV->setLinkage(GlobalValue::AppendingLinkage);
1100     break;
1101   case LLVMInternalLinkage:
1102     GV->setLinkage(GlobalValue::InternalLinkage);
1103     break;
1104   case LLVMPrivateLinkage:
1105     GV->setLinkage(GlobalValue::PrivateLinkage);
1106     break;
1107   case LLVMLinkerPrivateLinkage:
1108     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1109     break;
1110   case LLVMLinkerPrivateWeakLinkage:
1111     GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1112     break;
1113   case LLVMLinkerPrivateWeakDefAutoLinkage:
1114     GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1115     break;
1116   case LLVMDLLImportLinkage:
1117     GV->setLinkage(GlobalValue::DLLImportLinkage);
1118     break;
1119   case LLVMDLLExportLinkage:
1120     GV->setLinkage(GlobalValue::DLLExportLinkage);
1121     break;
1122   case LLVMExternalWeakLinkage:
1123     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1124     break;
1125   case LLVMGhostLinkage:
1126     DEBUG(errs()
1127           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1128     break;
1129   case LLVMCommonLinkage:
1130     GV->setLinkage(GlobalValue::CommonLinkage);
1131     break;
1132   }
1133 }
1134
1135 const char *LLVMGetSection(LLVMValueRef Global) {
1136   return unwrap<GlobalValue>(Global)->getSection().c_str();
1137 }
1138
1139 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1140   unwrap<GlobalValue>(Global)->setSection(Section);
1141 }
1142
1143 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1144   return static_cast<LLVMVisibility>(
1145     unwrap<GlobalValue>(Global)->getVisibility());
1146 }
1147
1148 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1149   unwrap<GlobalValue>(Global)
1150     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1151 }
1152
1153 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1154   return unwrap<GlobalValue>(Global)->getAlignment();
1155 }
1156
1157 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1158   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1159 }
1160
1161 /*--.. Operations on global variables ......................................--*/
1162
1163 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1164   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1165                                  GlobalValue::ExternalLinkage, 0, Name));
1166 }
1167
1168 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1169                                          const char *Name,
1170                                          unsigned AddressSpace) {
1171   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1172                                  GlobalValue::ExternalLinkage, 0, Name, 0,
1173                                  false, AddressSpace));
1174 }
1175
1176 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1177   return wrap(unwrap(M)->getNamedGlobal(Name));
1178 }
1179
1180 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1181   Module *Mod = unwrap(M);
1182   Module::global_iterator I = Mod->global_begin();
1183   if (I == Mod->global_end())
1184     return 0;
1185   return wrap(I);
1186 }
1187
1188 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1189   Module *Mod = unwrap(M);
1190   Module::global_iterator I = Mod->global_end();
1191   if (I == Mod->global_begin())
1192     return 0;
1193   return wrap(--I);
1194 }
1195
1196 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1197   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1198   Module::global_iterator I = GV;
1199   if (++I == GV->getParent()->global_end())
1200     return 0;
1201   return wrap(I);
1202 }
1203
1204 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1205   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1206   Module::global_iterator I = GV;
1207   if (I == GV->getParent()->global_begin())
1208     return 0;
1209   return wrap(--I);
1210 }
1211
1212 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1213   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1214 }
1215
1216 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1217   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1218   if ( !GV->hasInitializer() )
1219     return 0;
1220   return wrap(GV->getInitializer());
1221 }
1222
1223 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1224   unwrap<GlobalVariable>(GlobalVar)
1225     ->setInitializer(unwrap<Constant>(ConstantVal));
1226 }
1227
1228 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1229   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1230 }
1231
1232 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1233   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1234 }
1235
1236 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1237   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1238 }
1239
1240 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1241   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1242 }
1243
1244 /*--.. Operations on aliases ......................................--*/
1245
1246 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1247                           const char *Name) {
1248   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1249                               unwrap<Constant>(Aliasee), unwrap (M)));
1250 }
1251
1252 /*--.. Operations on functions .............................................--*/
1253
1254 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1255                              LLVMTypeRef FunctionTy) {
1256   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1257                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1258 }
1259
1260 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1261   return wrap(unwrap(M)->getFunction(Name));
1262 }
1263
1264 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1265   Module *Mod = unwrap(M);
1266   Module::iterator I = Mod->begin();
1267   if (I == Mod->end())
1268     return 0;
1269   return wrap(I);
1270 }
1271
1272 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1273   Module *Mod = unwrap(M);
1274   Module::iterator I = Mod->end();
1275   if (I == Mod->begin())
1276     return 0;
1277   return wrap(--I);
1278 }
1279
1280 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1281   Function *Func = unwrap<Function>(Fn);
1282   Module::iterator I = Func;
1283   if (++I == Func->getParent()->end())
1284     return 0;
1285   return wrap(I);
1286 }
1287
1288 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1289   Function *Func = unwrap<Function>(Fn);
1290   Module::iterator I = Func;
1291   if (I == Func->getParent()->begin())
1292     return 0;
1293   return wrap(--I);
1294 }
1295
1296 void LLVMDeleteFunction(LLVMValueRef Fn) {
1297   unwrap<Function>(Fn)->eraseFromParent();
1298 }
1299
1300 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1301   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1302     return F->getIntrinsicID();
1303   return 0;
1304 }
1305
1306 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1307   return unwrap<Function>(Fn)->getCallingConv();
1308 }
1309
1310 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1311   return unwrap<Function>(Fn)->setCallingConv(
1312     static_cast<CallingConv::ID>(CC));
1313 }
1314
1315 const char *LLVMGetGC(LLVMValueRef Fn) {
1316   Function *F = unwrap<Function>(Fn);
1317   return F->hasGC()? F->getGC() : 0;
1318 }
1319
1320 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1321   Function *F = unwrap<Function>(Fn);
1322   if (GC)
1323     F->setGC(GC);
1324   else
1325     F->clearGC();
1326 }
1327
1328 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1329   Function *Func = unwrap<Function>(Fn);
1330   const AttrListPtr PAL = Func->getAttributes();
1331   const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1332   Func->setAttributes(PALnew);
1333 }
1334
1335 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1336   Function *Func = unwrap<Function>(Fn);
1337   const AttrListPtr PAL = Func->getAttributes();
1338   const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1339   Func->setAttributes(PALnew);
1340 }
1341
1342 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1343   Function *Func = unwrap<Function>(Fn);
1344   const AttrListPtr PAL = Func->getAttributes();
1345   Attributes attr = PAL.getFnAttributes();
1346   return (LLVMAttribute)attr;
1347 }
1348
1349 /*--.. Operations on parameters ............................................--*/
1350
1351 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1352   // This function is strictly redundant to
1353   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1354   return unwrap<Function>(FnRef)->arg_size();
1355 }
1356
1357 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1358   Function *Fn = unwrap<Function>(FnRef);
1359   for (Function::arg_iterator I = Fn->arg_begin(),
1360                               E = Fn->arg_end(); I != E; I++)
1361     *ParamRefs++ = wrap(I);
1362 }
1363
1364 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1365   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1366   while (index --> 0)
1367     AI++;
1368   return wrap(AI);
1369 }
1370
1371 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1372   return wrap(unwrap<Argument>(V)->getParent());
1373 }
1374
1375 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1376   Function *Func = unwrap<Function>(Fn);
1377   Function::arg_iterator I = Func->arg_begin();
1378   if (I == Func->arg_end())
1379     return 0;
1380   return wrap(I);
1381 }
1382
1383 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1384   Function *Func = unwrap<Function>(Fn);
1385   Function::arg_iterator I = Func->arg_end();
1386   if (I == Func->arg_begin())
1387     return 0;
1388   return wrap(--I);
1389 }
1390
1391 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1392   Argument *A = unwrap<Argument>(Arg);
1393   Function::arg_iterator I = A;
1394   if (++I == A->getParent()->arg_end())
1395     return 0;
1396   return wrap(I);
1397 }
1398
1399 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1400   Argument *A = unwrap<Argument>(Arg);
1401   Function::arg_iterator I = A;
1402   if (I == A->getParent()->arg_begin())
1403     return 0;
1404   return wrap(--I);
1405 }
1406
1407 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1408   unwrap<Argument>(Arg)->addAttr(PA);
1409 }
1410
1411 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1412   unwrap<Argument>(Arg)->removeAttr(PA);
1413 }
1414
1415 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1416   Argument *A = unwrap<Argument>(Arg);
1417   Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1418     A->getArgNo()+1);
1419   return (LLVMAttribute)attr;
1420 }
1421   
1422
1423 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1424   unwrap<Argument>(Arg)->addAttr(
1425           Attribute::constructAlignmentFromInt(align));
1426 }
1427
1428 /*--.. Operations on basic blocks ..........................................--*/
1429
1430 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1431   return wrap(static_cast<Value*>(unwrap(BB)));
1432 }
1433
1434 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1435   return isa<BasicBlock>(unwrap(Val));
1436 }
1437
1438 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1439   return wrap(unwrap<BasicBlock>(Val));
1440 }
1441
1442 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1443   return wrap(unwrap(BB)->getParent());
1444 }
1445
1446 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1447   return unwrap<Function>(FnRef)->size();
1448 }
1449
1450 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1451   Function *Fn = unwrap<Function>(FnRef);
1452   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1453     *BasicBlocksRefs++ = wrap(I);
1454 }
1455
1456 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1457   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1458 }
1459
1460 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1461   Function *Func = unwrap<Function>(Fn);
1462   Function::iterator I = Func->begin();
1463   if (I == Func->end())
1464     return 0;
1465   return wrap(I);
1466 }
1467
1468 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1469   Function *Func = unwrap<Function>(Fn);
1470   Function::iterator I = Func->end();
1471   if (I == Func->begin())
1472     return 0;
1473   return wrap(--I);
1474 }
1475
1476 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1477   BasicBlock *Block = unwrap(BB);
1478   Function::iterator I = Block;
1479   if (++I == Block->getParent()->end())
1480     return 0;
1481   return wrap(I);
1482 }
1483
1484 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1485   BasicBlock *Block = unwrap(BB);
1486   Function::iterator I = Block;
1487   if (I == Block->getParent()->begin())
1488     return 0;
1489   return wrap(--I);
1490 }
1491
1492 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1493                                                 LLVMValueRef FnRef,
1494                                                 const char *Name) {
1495   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1496 }
1497
1498 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1499   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1500 }
1501
1502 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1503                                                 LLVMBasicBlockRef BBRef,
1504                                                 const char *Name) {
1505   BasicBlock *BB = unwrap(BBRef);
1506   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1507 }
1508
1509 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1510                                        const char *Name) {
1511   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1512 }
1513
1514 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1515   unwrap(BBRef)->eraseFromParent();
1516 }
1517
1518 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1519   unwrap(BB)->moveBefore(unwrap(MovePos));
1520 }
1521
1522 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1523   unwrap(BB)->moveAfter(unwrap(MovePos));
1524 }
1525
1526 /*--.. Operations on instructions ..........................................--*/
1527
1528 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1529   return wrap(unwrap<Instruction>(Inst)->getParent());
1530 }
1531
1532 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1533   BasicBlock *Block = unwrap(BB);
1534   BasicBlock::iterator I = Block->begin();
1535   if (I == Block->end())
1536     return 0;
1537   return wrap(I);
1538 }
1539
1540 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1541   BasicBlock *Block = unwrap(BB);
1542   BasicBlock::iterator I = Block->end();
1543   if (I == Block->begin())
1544     return 0;
1545   return wrap(--I);
1546 }
1547
1548 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1549   Instruction *Instr = unwrap<Instruction>(Inst);
1550   BasicBlock::iterator I = Instr;
1551   if (++I == Instr->getParent()->end())
1552     return 0;
1553   return wrap(I);
1554 }
1555
1556 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1557   Instruction *Instr = unwrap<Instruction>(Inst);
1558   BasicBlock::iterator I = Instr;
1559   if (I == Instr->getParent()->begin())
1560     return 0;
1561   return wrap(--I);
1562 }
1563
1564 /*--.. Call and invoke instructions ........................................--*/
1565
1566 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1567   Value *V = unwrap(Instr);
1568   if (CallInst *CI = dyn_cast<CallInst>(V))
1569     return CI->getCallingConv();
1570   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1571     return II->getCallingConv();
1572   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1573   return 0;
1574 }
1575
1576 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1577   Value *V = unwrap(Instr);
1578   if (CallInst *CI = dyn_cast<CallInst>(V))
1579     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1580   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1581     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1582   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1583 }
1584
1585 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1586                            LLVMAttribute PA) {
1587   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1588   Call.setAttributes(
1589     Call.getAttributes().addAttr(index, PA));
1590 }
1591
1592 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1593                               LLVMAttribute PA) {
1594   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1595   Call.setAttributes(
1596     Call.getAttributes().removeAttr(index, PA));
1597 }
1598
1599 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1600                                 unsigned align) {
1601   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1602   Call.setAttributes(
1603     Call.getAttributes().addAttr(index, 
1604         Attribute::constructAlignmentFromInt(align)));
1605 }
1606
1607 /*--.. Operations on call instructions (only) ..............................--*/
1608
1609 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1610   return unwrap<CallInst>(Call)->isTailCall();
1611 }
1612
1613 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1614   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1615 }
1616
1617 /*--.. Operations on phi nodes .............................................--*/
1618
1619 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1620                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1621   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1622   for (unsigned I = 0; I != Count; ++I)
1623     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1624 }
1625
1626 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1627   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1628 }
1629
1630 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1631   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1632 }
1633
1634 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1635   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1636 }
1637
1638
1639 /*===-- Instruction builders ----------------------------------------------===*/
1640
1641 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1642   return wrap(new IRBuilder<>(*unwrap(C)));
1643 }
1644
1645 LLVMBuilderRef LLVMCreateBuilder(void) {
1646   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1647 }
1648
1649 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1650                          LLVMValueRef Instr) {
1651   BasicBlock *BB = unwrap(Block);
1652   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1653   unwrap(Builder)->SetInsertPoint(BB, I);
1654 }
1655
1656 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1657   Instruction *I = unwrap<Instruction>(Instr);
1658   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1659 }
1660
1661 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1662   BasicBlock *BB = unwrap(Block);
1663   unwrap(Builder)->SetInsertPoint(BB);
1664 }
1665
1666 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1667    return wrap(unwrap(Builder)->GetInsertBlock());
1668 }
1669
1670 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1671   unwrap(Builder)->ClearInsertionPoint();
1672 }
1673
1674 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1675   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1676 }
1677
1678 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1679                                    const char *Name) {
1680   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1681 }
1682
1683 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1684   delete unwrap(Builder);
1685 }
1686
1687 /*--.. Metadata builders ...................................................--*/
1688
1689 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1690   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1691   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1692 }
1693
1694 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1695   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1696               .getAsMDNode(unwrap(Builder)->getContext()));
1697 }
1698
1699 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1700   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1701 }
1702
1703
1704 /*--.. Instruction builders ................................................--*/
1705
1706 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1707   return wrap(unwrap(B)->CreateRetVoid());
1708 }
1709
1710 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1711   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1712 }
1713
1714 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1715                                    unsigned N) {
1716   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1717 }
1718
1719 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1720   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1721 }
1722
1723 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1724                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1725   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1726 }
1727
1728 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1729                              LLVMBasicBlockRef Else, unsigned NumCases) {
1730   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1731 }
1732
1733 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1734                                  unsigned NumDests) {
1735   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1736 }
1737
1738 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1739                              LLVMValueRef *Args, unsigned NumArgs,
1740                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1741                              const char *Name) {
1742   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1743                                       unwrap(Args), unwrap(Args) + NumArgs,
1744                                       Name));
1745 }
1746
1747 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1748   return wrap(unwrap(B)->CreateUnwind());
1749 }
1750
1751 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1752   return wrap(unwrap(B)->CreateUnreachable());
1753 }
1754
1755 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1756                  LLVMBasicBlockRef Dest) {
1757   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1758 }
1759
1760 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1761   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1762 }
1763
1764 /*--.. Arithmetic ..........................................................--*/
1765
1766 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1767                           const char *Name) {
1768   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1769 }
1770
1771 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1772                           const char *Name) {
1773   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1774 }
1775
1776 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1777                           const char *Name) {
1778   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1779 }
1780
1781 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1782                           const char *Name) {
1783   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1784 }
1785
1786 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1787                           const char *Name) {
1788   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1789 }
1790
1791 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1792                           const char *Name) {
1793   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1794 }
1795
1796 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1797                           const char *Name) {
1798   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1799 }
1800
1801 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1802                           const char *Name) {
1803   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1804 }
1805
1806 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1807                           const char *Name) {
1808   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1809 }
1810
1811 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1812                           const char *Name) {
1813   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1814 }
1815
1816 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1817                           const char *Name) {
1818   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1819 }
1820
1821 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1822                           const char *Name) {
1823   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1824 }
1825
1826 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1827                            const char *Name) {
1828   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1829 }
1830
1831 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1832                            const char *Name) {
1833   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1834 }
1835
1836 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1837                                 LLVMValueRef RHS, const char *Name) {
1838   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1839 }
1840
1841 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1842                            const char *Name) {
1843   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1844 }
1845
1846 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1847                            const char *Name) {
1848   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1849 }
1850
1851 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1852                            const char *Name) {
1853   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1854 }
1855
1856 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1857                            const char *Name) {
1858   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1859 }
1860
1861 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1862                           const char *Name) {
1863   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1864 }
1865
1866 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1867                            const char *Name) {
1868   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1869 }
1870
1871 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1872                            const char *Name) {
1873   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1874 }
1875
1876 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1877                           const char *Name) {
1878   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1879 }
1880
1881 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1882                          const char *Name) {
1883   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1884 }
1885
1886 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1887                           const char *Name) {
1888   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1889 }
1890
1891 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1892                             LLVMValueRef LHS, LLVMValueRef RHS,
1893                             const char *Name) {
1894   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1895                                      unwrap(RHS), Name));
1896 }
1897
1898 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1899   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1900 }
1901
1902 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1903                              const char *Name) {
1904   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1905 }
1906
1907 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1908                              const char *Name) {
1909   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1910 }
1911
1912 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1913   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1914 }
1915
1916 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1917   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1918 }
1919
1920 /*--.. Memory ..............................................................--*/
1921
1922 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1923                              const char *Name) {
1924   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1925   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1926   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1927   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1928                                                ITy, unwrap(Ty), AllocSize, 
1929                                                0, 0, "");
1930   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1931 }
1932
1933 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1934                                   LLVMValueRef Val, const char *Name) {
1935   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1936   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1937   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1938   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1939                                                ITy, unwrap(Ty), AllocSize, 
1940                                                unwrap(Val), 0, "");
1941   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1942 }
1943
1944 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1945                              const char *Name) {
1946   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1947 }
1948
1949 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1950                                   LLVMValueRef Val, const char *Name) {
1951   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1952 }
1953
1954 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1955   return wrap(unwrap(B)->Insert(
1956      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1957 }
1958
1959
1960 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1961                            const char *Name) {
1962   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1963 }
1964
1965 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1966                             LLVMValueRef PointerVal) {
1967   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1968 }
1969
1970 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1971                           LLVMValueRef *Indices, unsigned NumIndices,
1972                           const char *Name) {
1973   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1974                                    unwrap(Indices) + NumIndices, Name));
1975 }
1976
1977 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1978                                   LLVMValueRef *Indices, unsigned NumIndices,
1979                                   const char *Name) {
1980   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1981                                            unwrap(Indices) + NumIndices, Name));
1982 }
1983
1984 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1985                                 unsigned Idx, const char *Name) {
1986   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1987 }
1988
1989 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1990                                    const char *Name) {
1991   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1992 }
1993
1994 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1995                                       const char *Name) {
1996   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1997 }
1998
1999 /*--.. Casts ...............................................................--*/
2000
2001 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2002                             LLVMTypeRef DestTy, const char *Name) {
2003   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2004 }
2005
2006 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2007                            LLVMTypeRef DestTy, const char *Name) {
2008   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2009 }
2010
2011 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2012                            LLVMTypeRef DestTy, const char *Name) {
2013   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2014 }
2015
2016 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2017                              LLVMTypeRef DestTy, const char *Name) {
2018   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2019 }
2020
2021 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2022                              LLVMTypeRef DestTy, const char *Name) {
2023   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2024 }
2025
2026 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2027                              LLVMTypeRef DestTy, const char *Name) {
2028   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2029 }
2030
2031 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2032                              LLVMTypeRef DestTy, const char *Name) {
2033   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2034 }
2035
2036 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2037                               LLVMTypeRef DestTy, const char *Name) {
2038   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2039 }
2040
2041 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2042                             LLVMTypeRef DestTy, const char *Name) {
2043   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2044 }
2045
2046 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2047                                LLVMTypeRef DestTy, const char *Name) {
2048   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2049 }
2050
2051 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2052                                LLVMTypeRef DestTy, const char *Name) {
2053   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2054 }
2055
2056 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2057                               LLVMTypeRef DestTy, const char *Name) {
2058   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2059 }
2060
2061 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2062                                     LLVMTypeRef DestTy, const char *Name) {
2063   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2064                                              Name));
2065 }
2066
2067 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2068                                     LLVMTypeRef DestTy, const char *Name) {
2069   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2070                                              Name));
2071 }
2072
2073 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2074                                      LLVMTypeRef DestTy, const char *Name) {
2075   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2076                                               Name));
2077 }
2078
2079 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2080                            LLVMTypeRef DestTy, const char *Name) {
2081   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2082                                     unwrap(DestTy), Name));
2083 }
2084
2085 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2086                                   LLVMTypeRef DestTy, const char *Name) {
2087   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2088 }
2089
2090 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2091                               LLVMTypeRef DestTy, const char *Name) {
2092   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2093                                        /*isSigned*/true, Name));
2094 }
2095
2096 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2097                              LLVMTypeRef DestTy, const char *Name) {
2098   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2099 }
2100
2101 /*--.. Comparisons .........................................................--*/
2102
2103 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2104                            LLVMValueRef LHS, LLVMValueRef RHS,
2105                            const char *Name) {
2106   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2107                                     unwrap(LHS), unwrap(RHS), Name));
2108 }
2109
2110 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2111                            LLVMValueRef LHS, LLVMValueRef RHS,
2112                            const char *Name) {
2113   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2114                                     unwrap(LHS), unwrap(RHS), Name));
2115 }
2116
2117 /*--.. Miscellaneous instructions ..........................................--*/
2118
2119 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2120   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2121 }
2122
2123 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2124                            LLVMValueRef *Args, unsigned NumArgs,
2125                            const char *Name) {
2126   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2127                                     unwrap(Args) + NumArgs, Name));
2128 }
2129
2130 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2131                              LLVMValueRef Then, LLVMValueRef Else,
2132                              const char *Name) {
2133   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2134                                       Name));
2135 }
2136
2137 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2138                             LLVMTypeRef Ty, const char *Name) {
2139   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2140 }
2141
2142 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2143                                       LLVMValueRef Index, const char *Name) {
2144   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2145                                               Name));
2146 }
2147
2148 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2149                                     LLVMValueRef EltVal, LLVMValueRef Index,
2150                                     const char *Name) {
2151   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2152                                              unwrap(Index), Name));
2153 }
2154
2155 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2156                                     LLVMValueRef V2, LLVMValueRef Mask,
2157                                     const char *Name) {
2158   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2159                                              unwrap(Mask), Name));
2160 }
2161
2162 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2163                                    unsigned Index, const char *Name) {
2164   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2165 }
2166
2167 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2168                                   LLVMValueRef EltVal, unsigned Index,
2169                                   const char *Name) {
2170   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2171                                            Index, Name));
2172 }
2173
2174 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2175                              const char *Name) {
2176   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2177 }
2178
2179 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2180                                 const char *Name) {
2181   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2182 }
2183
2184 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2185                               LLVMValueRef RHS, const char *Name) {
2186   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2187 }
2188
2189
2190 /*===-- Module providers --------------------------------------------------===*/
2191
2192 LLVMModuleProviderRef
2193 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2194   return reinterpret_cast<LLVMModuleProviderRef>(M);
2195 }
2196
2197 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2198   delete unwrap(MP);
2199 }
2200
2201
2202 /*===-- Memory buffers ----------------------------------------------------===*/
2203
2204 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2205     const char *Path,
2206     LLVMMemoryBufferRef *OutMemBuf,
2207     char **OutMessage) {
2208
2209   std::string Error;
2210   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2211     *OutMemBuf = wrap(MB);
2212     return 0;
2213   }
2214   
2215   *OutMessage = strdup(Error.c_str());
2216   return 1;
2217 }
2218
2219 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2220                                          char **OutMessage) {
2221   std::string Error;
2222   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) {
2223     *OutMemBuf = wrap(MB);
2224     return 0;
2225   }
2226
2227   *OutMessage = strdup(Error.c_str());
2228   return 1;
2229 }
2230
2231 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2232   delete unwrap(MemBuf);
2233 }
2234
2235 /*===-- Pass Registry -----------------------------------------------------===*/
2236
2237 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2238   return wrap(PassRegistry::getPassRegistry());
2239 }
2240
2241 /*===-- Pass Manager ------------------------------------------------------===*/
2242
2243 LLVMPassManagerRef LLVMCreatePassManager() {
2244   return wrap(new PassManager());
2245 }
2246
2247 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2248   return wrap(new FunctionPassManager(unwrap(M)));
2249 }
2250
2251 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2252   return LLVMCreateFunctionPassManagerForModule(
2253                                             reinterpret_cast<LLVMModuleRef>(P));
2254 }
2255
2256 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2257   return unwrap<PassManager>(PM)->run(*unwrap(M));
2258 }
2259
2260 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2261   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2262 }
2263
2264 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2265   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2266 }
2267
2268 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2269   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2270 }
2271
2272 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2273   delete unwrap(PM);
2274 }