Adding C and Ocaml bindings for ConstantExpr.
[oota-llvm.git] / lib / VMCore / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Gordon Henriksen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // 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/TypeSymbolTable.h"
21 #include <cassert>
22
23 using namespace llvm;
24
25
26 /*===-- Operations on modules ---------------------------------------------===*/
27
28 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
29   return wrap(new Module(ModuleID));
30 }
31
32 void LLVMDisposeModule(LLVMModuleRef M) {
33   delete unwrap(M);
34 }
35
36 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
37   return unwrap(M)->addTypeName(Name, unwrap(Ty));
38 }
39
40 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
41   std::string N(Name);
42   
43   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
44   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
45     if (I->first == N)
46       TST.remove(I);
47 }
48
49
50 /*===-- Operations on types -----------------------------------------------===*/
51
52 /*--.. Operations on all types (mostly) ....................................--*/
53
54 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
55   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
56 }
57
58 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
59   DerivedType *Ty = unwrap<DerivedType>(AbstractType);
60   Ty->refineAbstractTypeTo(unwrap(ConcreteType));
61 }
62
63 /*--.. Operations on integer types .........................................--*/
64
65 LLVMTypeRef LLVMInt1Type()  { return (LLVMTypeRef) Type::Int1Ty;  }
66 LLVMTypeRef LLVMInt8Type()  { return (LLVMTypeRef) Type::Int8Ty;  }
67 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
68 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
69 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
70
71 LLVMTypeRef LLVMCreateIntType(unsigned NumBits) {
72   return wrap(IntegerType::get(NumBits));
73 }
74
75 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
76   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
77 }
78
79 /*--.. Operations on real types ............................................--*/
80
81 LLVMTypeRef LLVMFloatType()    { return (LLVMTypeRef) Type::FloatTy;     }
82 LLVMTypeRef LLVMDoubleType()   { return (LLVMTypeRef) Type::DoubleTy;    }
83 LLVMTypeRef LLVMX86FP80Type()  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
84 LLVMTypeRef LLVMFP128Type()    { return (LLVMTypeRef) Type::FP128Ty;     }
85 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
86
87 /*--.. Operations on function types ........................................--*/
88
89 LLVMTypeRef LLVMCreateFunctionType(LLVMTypeRef ReturnType,
90                            LLVMTypeRef *ParamTypes, unsigned ParamCount,
91                            int IsVarArg) {
92   std::vector<const Type*> Tys;
93   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
94     Tys.push_back(unwrap(*I));
95   
96   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
97 }
98
99 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
100   return unwrap<FunctionType>(FunctionTy)->isVarArg();
101 }
102
103 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
104   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
105 }
106
107 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
108   return unwrap<FunctionType>(FunctionTy)->getNumParams();
109 }
110
111 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
112   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
113   for (FunctionType::param_iterator I = Ty->param_begin(),
114                                     E = Ty->param_end(); I != E; ++I)
115     *Dest++ = wrap(*I);
116 }
117
118 /*--.. Operations on struct types ..........................................--*/
119
120 LLVMTypeRef LLVMCreateStructType(LLVMTypeRef *ElementTypes,
121                                  unsigned ElementCount, int Packed) {
122   std::vector<const Type*> Tys;
123   for (LLVMTypeRef *I = ElementTypes,
124                    *E = ElementTypes + ElementCount; I != E; ++I)
125     Tys.push_back(unwrap(*I));
126   
127   return wrap(StructType::get(Tys, Packed != 0));
128 }
129
130 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
131   return unwrap<StructType>(StructTy)->getNumElements();
132 }
133
134 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
135   StructType *Ty = unwrap<StructType>(StructTy);
136   for (FunctionType::param_iterator I = Ty->element_begin(),
137                                     E = Ty->element_end(); I != E; ++I)
138     *Dest++ = wrap(*I);
139 }
140
141 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
142   return unwrap<StructType>(StructTy)->isPacked();
143 }
144
145 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
146
147 LLVMTypeRef LLVMCreateArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
148   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
149 }
150
151 LLVMTypeRef LLVMCreatePointerType(LLVMTypeRef ElementType) {
152   return wrap(PointerType::get(unwrap(ElementType)));
153 }
154
155 LLVMTypeRef LLVMCreateVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
156   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
157 }
158
159 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
160   return wrap(unwrap<SequentialType>(Ty)->getElementType());
161 }
162
163 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
164   return unwrap<ArrayType>(ArrayTy)->getNumElements();
165 }
166
167 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
168   return unwrap<VectorType>(VectorTy)->getNumElements();
169 }
170
171 /*--.. Operations on other types ...........................................--*/
172
173 LLVMTypeRef LLVMVoidType()  { return (LLVMTypeRef) Type::VoidTy;  }
174 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
175
176 LLVMTypeRef LLVMCreateOpaqueType() {
177   return wrap(llvm::OpaqueType::get());
178 }
179
180
181 /*===-- Operations on values ----------------------------------------------===*/
182
183 /*--.. Operations on all values ............................................--*/
184
185 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
186   return wrap(unwrap(Val)->getType());
187 }
188
189 const char *LLVMGetValueName(LLVMValueRef Val) {
190   return unwrap(Val)->getNameStart();
191 }
192
193 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
194   unwrap(Val)->setName(Name);
195 }
196
197 void LLVMDumpValue(LLVMValueRef Val) {
198   unwrap(Val)->dump();
199 }
200
201 /*--.. Operations on constants of any type .................................--*/
202
203 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty) {
204   return wrap(Constant::getNullValue(unwrap(Ty)));
205 }
206
207 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty) {
208   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
209 }
210
211 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
212   return wrap(UndefValue::get(unwrap(Ty)));
213 }
214
215 int LLVMIsConstant(LLVMValueRef Ty) {
216   return isa<Constant>(unwrap(Ty));
217 }
218
219 int LLVMIsNull(LLVMValueRef Val) {
220   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
221     return C->isNullValue();
222   return false;
223 }
224
225 int LLVMIsUndef(LLVMValueRef Val) {
226   return isa<UndefValue>(unwrap(Val));
227 }
228
229 /*--.. Operations on scalar constants ......................................--*/
230
231 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
232                                 int SignExtend) {
233   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
234 }
235
236 LLVMValueRef LLVMGetRealConstant(LLVMTypeRef RealTy, double N) {
237   return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
238 }
239
240 /*--.. Operations on composite constants ...................................--*/
241
242 LLVMValueRef LLVMGetStringConstant(const char *Str, unsigned Length,
243                                    int DontNullTerminate) {
244   /* Inverted the sense of AddNull because ', 0)' is a
245      better mnemonic for null termination than ', 1)'. */
246   return wrap(ConstantArray::get(std::string(Str, Length),
247                                  DontNullTerminate == 0));
248 }
249
250 LLVMValueRef LLVMGetArrayConstant(LLVMTypeRef ElementTy,
251                                   LLVMValueRef *ConstantVals, unsigned Length) {
252   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
253                                  unwrap<Constant>(ConstantVals, Length),
254                                  Length));
255 }
256
257 LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count,
258                                    int Packed) {
259   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
260                                   Count, Packed != 0));
261 }
262
263 LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals,
264                                    unsigned Size) {
265   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
266                                   Size));
267 }
268
269 /*--.. Constant expressions ................................................--*/
270
271 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
272   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
273 }
274
275 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
276   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
277 }
278
279 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
280   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
281 }
282
283 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
284   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
285                                    unwrap<Constant>(RHSConstant)));
286 }
287
288 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
289   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
290                                    unwrap<Constant>(RHSConstant)));
291 }
292
293 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
294   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
295                                    unwrap<Constant>(RHSConstant)));
296 }
297
298 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
299   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
300                                     unwrap<Constant>(RHSConstant)));
301 }
302
303 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
304   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
305                                     unwrap<Constant>(RHSConstant)));
306 }
307
308 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
309   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
310                                     unwrap<Constant>(RHSConstant)));
311 }
312
313 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
314   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
315                                     unwrap<Constant>(RHSConstant)));
316 }
317
318 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
319   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
320                                     unwrap<Constant>(RHSConstant)));
321 }
322
323 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
324   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
325                                     unwrap<Constant>(RHSConstant)));
326 }
327
328 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
329   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
330                                    unwrap<Constant>(RHSConstant)));
331 }
332
333 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
334   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
335                                   unwrap<Constant>(RHSConstant)));
336 }
337
338 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
339   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
340                                    unwrap<Constant>(RHSConstant)));
341 }
342
343 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
344                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
345   return wrap(ConstantExpr::getICmp(Predicate,
346                                     unwrap<Constant>(LHSConstant),
347                                     unwrap<Constant>(RHSConstant)));
348 }
349
350 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
351                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
352   return wrap(ConstantExpr::getFCmp(Predicate,
353                                     unwrap<Constant>(LHSConstant),
354                                     unwrap<Constant>(RHSConstant)));
355 }
356
357 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
358   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
359                                   unwrap<Constant>(RHSConstant)));
360 }
361
362 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
363   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
364                                     unwrap<Constant>(RHSConstant)));
365 }
366
367 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
368   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
369                                     unwrap<Constant>(RHSConstant)));
370 }
371
372 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
373                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
374   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
375                                              unwrap<Constant>(ConstantIndices, 
376                                                               NumIndices),
377                                              NumIndices));
378 }
379
380 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
381   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
382                                      unwrap(ToType)));
383 }
384
385 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
386   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
387                                     unwrap(ToType)));
388 }
389
390 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
391   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
392                                     unwrap(ToType)));
393 }
394
395 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
396   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
397                                        unwrap(ToType)));
398 }
399
400 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
401   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
402                                         unwrap(ToType)));
403 }
404
405 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
406   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
407                                       unwrap(ToType)));
408 }
409
410 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
411   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
412                                       unwrap(ToType)));
413 }
414
415 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
416   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
417                                       unwrap(ToType)));
418 }
419
420 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
421   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
422                                       unwrap(ToType)));
423 }
424
425 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
426   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
427                                         unwrap(ToType)));
428 }
429
430 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
431   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
432                                         unwrap(ToType)));
433 }
434
435 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
436   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
437                                        unwrap(ToType)));
438 }
439
440 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
441                              LLVMValueRef ConstantIfTrue,
442                              LLVMValueRef ConstantIfFalse) {
443   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
444                                       unwrap<Constant>(ConstantIfTrue),
445                                       unwrap<Constant>(ConstantIfFalse)));
446 }
447
448 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
449                                      LLVMValueRef IndexConstant) {
450   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
451                                               unwrap<Constant>(IndexConstant)));
452 }
453
454 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
455                                     LLVMValueRef ElementValueConstant,
456                                     LLVMValueRef IndexConstant) {
457   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
458                                          unwrap<Constant>(ElementValueConstant),
459                                              unwrap<Constant>(IndexConstant)));
460 }
461
462 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
463                                     LLVMValueRef VectorBConstant,
464                                     LLVMValueRef MaskConstant) {
465   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
466                                              unwrap<Constant>(VectorBConstant),
467                                              unwrap<Constant>(MaskConstant)));
468 }
469
470 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
471
472 int LLVMIsDeclaration(LLVMValueRef Global) {
473   return unwrap<GlobalValue>(Global)->isDeclaration();
474 }
475
476 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
477   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
478 }
479
480 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
481   unwrap<GlobalValue>(Global)
482     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
483 }
484
485 const char *LLVMGetSection(LLVMValueRef Global) {
486   return unwrap<GlobalValue>(Global)->getSection().c_str();
487 }
488
489 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
490   unwrap<GlobalValue>(Global)->setSection(Section);
491 }
492
493 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
494   return static_cast<LLVMVisibility>(
495     unwrap<GlobalValue>(Global)->getVisibility());
496 }
497
498 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
499   unwrap<GlobalValue>(Global)
500     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
501 }
502
503 unsigned LLVMGetAlignment(LLVMValueRef Global) {
504   return unwrap<GlobalValue>(Global)->getAlignment();
505 }
506
507 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
508   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
509 }
510
511 /*--.. Operations on global variables ......................................--*/
512
513 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
514   return wrap(new GlobalVariable(unwrap(Ty), false,
515               GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
516 }
517
518 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
519   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
520 }
521
522 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
523   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
524 }
525
526 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
527   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
528 }
529
530 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
531   unwrap<GlobalVariable>(GlobalVar)
532     ->setInitializer(unwrap<Constant>(ConstantVal));
533 }
534
535 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
536   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
537 }
538
539 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
540   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
541 }
542
543 /*--.. Operations on functions .............................................--*/
544
545 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
546                              LLVMTypeRef FunctionTy) {
547   return wrap(new Function(unwrap<FunctionType>(FunctionTy),
548                            GlobalValue::ExternalLinkage, Name, unwrap(M)));
549 }
550
551 void LLVMDeleteFunction(LLVMValueRef Fn) {
552   unwrap<Function>(Fn)->eraseFromParent();
553 }
554
555 unsigned LLVMCountParams(LLVMValueRef FnRef) {
556   // This function is strictly redundant to
557   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
558   return unwrap<Function>(FnRef)->getArgumentList().size();
559 }
560
561 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
562   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
563   while (index --> 0)
564     AI++;
565   return wrap(AI);
566 }
567
568 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
569   Function *Fn = unwrap<Function>(FnRef);
570   for (Function::arg_iterator I = Fn->arg_begin(),
571                               E = Fn->arg_end(); I != E; I++)
572     *ParamRefs++ = wrap(I);
573 }
574
575 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
576   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
577     return F->getIntrinsicID();
578   return 0;
579 }
580
581 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
582   return unwrap<Function>(Fn)->getCallingConv();
583 }
584
585 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
586   return unwrap<Function>(Fn)->setCallingConv(CC);
587 }
588
589 /*--.. Operations on basic blocks ..........................................--*/
590
591 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
592   return wrap(static_cast<Value*>(unwrap(Bb)));
593 }
594
595 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
596   return isa<BasicBlock>(unwrap(Val));
597 }
598
599 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
600   return wrap(unwrap<BasicBlock>(Val));
601 }
602
603 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
604   return unwrap<Function>(FnRef)->getBasicBlockList().size();
605 }
606
607 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
608   Function *Fn = unwrap<Function>(FnRef);
609   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
610     *BasicBlocksRefs++ = wrap(I);
611 }
612
613 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
614   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
615 }
616
617 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
618   return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
619 }
620
621 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
622                                        const char *Name) {
623   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
624   return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
625                              InsertBeforeBB));
626 }
627
628 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
629   unwrap(BBRef)->eraseFromParent();
630 }
631
632 /*--.. Call and invoke instructions ........................................--*/
633
634 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
635   Value *V = unwrap(Instr);
636   if (CallInst *CI = dyn_cast<CallInst>(V))
637     return CI->getCallingConv();
638   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
639     return II->getCallingConv();
640   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
641   return 0;
642 }
643
644 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
645   Value *V = unwrap(Instr);
646   if (CallInst *CI = dyn_cast<CallInst>(V))
647     return CI->setCallingConv(CC);
648   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
649     return II->setCallingConv(CC);
650   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
651 }
652
653
654 /*===-- Instruction builders ----------------------------------------------===*/
655
656 LLVMBuilderRef LLVMCreateBuilder() {
657   return wrap(new LLVMBuilder());
658 }
659
660 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
661   Instruction *I = unwrap<Instruction>(Instr);
662   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
663 }
664
665 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
666   BasicBlock *BB = unwrap(Block);
667   unwrap(Builder)->SetInsertPoint(BB);
668 }
669
670 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
671   delete unwrap(Builder);
672 }
673
674 /*--.. Instruction builders ................................................--*/
675
676 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
677   return wrap(unwrap(B)->CreateRetVoid());
678 }
679
680 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
681   return wrap(unwrap(B)->CreateRet(unwrap(V)));
682 }
683
684 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
685   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
686 }
687
688 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
689                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
690   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
691 }
692
693 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
694                              LLVMBasicBlockRef Else, unsigned NumCases) {
695   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
696 }
697
698 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
699                              LLVMValueRef *Args, unsigned NumArgs,
700                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
701                              const char *Name) {
702   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
703                                       unwrap(Args), unwrap(Args) + NumArgs,
704                                       Name));
705 }
706
707 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
708   return wrap(unwrap(B)->CreateUnwind());
709 }
710
711 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
712   return wrap(unwrap(B)->CreateUnreachable());
713 }
714
715 /*--.. Arithmetic ..........................................................--*/
716
717 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
718                           const char *Name) {
719   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
720 }
721
722 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
723                           const char *Name) {
724   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
725 }
726
727 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
728                           const char *Name) {
729   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
730 }
731
732 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
733                            const char *Name) {
734   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
735 }
736
737 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
738                            const char *Name) {
739   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
740 }
741
742 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
743                            const char *Name) {
744   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
745 }
746
747 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
748                            const char *Name) {
749   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
750 }
751
752 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
753                            const char *Name) {
754   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
755 }
756
757 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
758                            const char *Name) {
759   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
760 }
761
762 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
763                           const char *Name) {
764   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
765 }
766
767 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
768                            const char *Name) {
769   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
770 }
771
772 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
773                            const char *Name) {
774   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
775 }
776
777 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
778                           const char *Name) {
779   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
780 }
781
782 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
783                          const char *Name) {
784   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
785 }
786
787 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
788                           const char *Name) {
789   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
790 }
791
792 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
793   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
794 }
795
796 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
797   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
798 }
799
800 /*--.. Memory ..............................................................--*/
801
802 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
803                              const char *Name) {
804   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
805 }
806
807 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
808                                   LLVMValueRef Val, const char *Name) {
809   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
810 }
811
812 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
813                              const char *Name) {
814   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
815 }
816
817 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
818                                   LLVMValueRef Val, const char *Name) {
819   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
820 }
821
822 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
823   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
824 }
825
826
827 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
828                            const char *Name) {
829   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
830 }
831
832 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
833                             LLVMValueRef PointerVal) {
834   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
835 }
836
837 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
838                           LLVMValueRef *Indices, unsigned NumIndices,
839                           const char *Name) {
840   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
841                                    unwrap(Indices) + NumIndices, Name));
842 }
843
844 /*--.. Casts ...............................................................--*/
845
846 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
847                             LLVMTypeRef DestTy, const char *Name) {
848   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
849 }
850
851 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
852                            LLVMTypeRef DestTy, const char *Name) {
853   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
854 }
855
856 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
857                            LLVMTypeRef DestTy, const char *Name) {
858   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
859 }
860
861 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
862                              LLVMTypeRef DestTy, const char *Name) {
863   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
864 }
865
866 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
867                              LLVMTypeRef DestTy, const char *Name) {
868   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
869 }
870
871 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
872                              LLVMTypeRef DestTy, const char *Name) {
873   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
874 }
875
876 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
877                              LLVMTypeRef DestTy, const char *Name) {
878   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
879 }
880
881 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
882                               LLVMTypeRef DestTy, const char *Name) {
883   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
884 }
885
886 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
887                             LLVMTypeRef DestTy, const char *Name) {
888   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
889 }
890
891 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
892                                LLVMTypeRef DestTy, const char *Name) {
893   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
894 }
895
896 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
897                                LLVMTypeRef DestTy, const char *Name) {
898   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
899 }
900
901 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
902                               LLVMTypeRef DestTy, const char *Name) {
903   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
904 }
905
906 /*--.. Comparisons .........................................................--*/
907
908 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
909                            LLVMValueRef LHS, LLVMValueRef RHS,
910                            const char *Name) {
911   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
912                                     unwrap(LHS), unwrap(RHS), Name));
913 }
914
915 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
916                            LLVMValueRef LHS, LLVMValueRef RHS,
917                            const char *Name) {
918   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
919                                     unwrap(LHS), unwrap(RHS), Name));
920 }
921
922 /*--.. Miscellaneous instructions ..........................................--*/
923
924 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
925   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
926 }
927
928 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
929                            LLVMValueRef *Args, unsigned NumArgs,
930                            const char *Name) {
931   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
932                                     unwrap(Args) + NumArgs, Name));
933 }
934
935 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
936                              LLVMValueRef Then, LLVMValueRef Else,
937                              const char *Name) {
938   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
939                                       Name));
940 }
941
942 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
943                             LLVMTypeRef Ty, const char *Name) {
944   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
945 }
946
947 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
948                                       LLVMValueRef Index, const char *Name) {
949   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
950                                               Name));
951 }
952
953 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
954                                     LLVMValueRef EltVal, LLVMValueRef Index,
955                                     const char *Name) {
956   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
957                                              unwrap(Index), Name));
958 }
959
960 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
961                                     LLVMValueRef V2, LLVMValueRef Mask,
962                                     const char *Name) {
963   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
964                                              unwrap(Mask), Name));
965 }