b6a825462ae10361988c0046c90f40600703e50c
[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 /*--.. Operations on constants of any type .................................--*/
198
199 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty) {
200   return wrap(Constant::getNullValue(unwrap(Ty)));
201 }
202
203 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty) {
204   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
205 }
206
207 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
208   return wrap(UndefValue::get(unwrap(Ty)));
209 }
210
211 int LLVMIsConstant(LLVMValueRef Ty) {
212   return isa<Constant>(unwrap(Ty));
213 }
214
215 int LLVMIsNull(LLVMValueRef Val) {
216   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
217     return C->isNullValue();
218   return false;
219 }
220
221 int LLVMIsUndef(LLVMValueRef Val) {
222   return isa<UndefValue>(unwrap(Val));
223 }
224
225 /*--.. Operations on scalar constants ......................................--*/
226
227 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
228                                 int SignExtend) {
229   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
230 }
231
232 LLVMValueRef LLVMGetRealConstant(LLVMTypeRef RealTy, double N) {
233   return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
234 }
235
236 /*--.. Operations on composite constants ...................................--*/
237
238 LLVMValueRef LLVMGetStringConstant(const char *Str, unsigned Length,
239                                    int DontNullTerminate) {
240   /* Inverted the sense of AddNull because ', 0)' is a
241      better mnemonic for null termination than ', 1)'. */
242   return wrap(ConstantArray::get(std::string(Str, Length),
243                                  DontNullTerminate == 0));
244 }
245
246 LLVMValueRef LLVMGetArrayConstant(LLVMTypeRef ElementTy,
247                                   LLVMValueRef *ConstantVals, unsigned Length) {
248   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
249                                  unwrap<Constant>(ConstantVals, Length),
250                                  Length));
251 }
252
253 LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count,
254                                    int Packed) {
255   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
256                                   Count, Packed != 0));
257 }
258
259 LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals,
260                                    unsigned Size) {
261   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
262                                   Size));
263 }
264
265 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
266
267 int LLVMIsDeclaration(LLVMValueRef Global) {
268   return unwrap<GlobalValue>(Global)->isDeclaration();
269 }
270
271 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
272   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
273 }
274
275 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
276   unwrap<GlobalValue>(Global)
277     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
278 }
279
280 const char *LLVMGetSection(LLVMValueRef Global) {
281   return unwrap<GlobalValue>(Global)->getSection().c_str();
282 }
283
284 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
285   unwrap<GlobalValue>(Global)->setSection(Section);
286 }
287
288 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
289   return static_cast<LLVMVisibility>(
290     unwrap<GlobalValue>(Global)->getVisibility());
291 }
292
293 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
294   unwrap<GlobalValue>(Global)
295     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
296 }
297
298 unsigned LLVMGetAlignment(LLVMValueRef Global) {
299   return unwrap<GlobalValue>(Global)->getAlignment();
300 }
301
302 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
303   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
304 }
305
306 /*--.. Operations on global variables ......................................--*/
307
308 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
309   return wrap(new GlobalVariable(unwrap(Ty), false,
310               GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
311 }
312
313 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
314   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
315 }
316
317 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
318   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
319 }
320
321 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
322   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
323 }
324
325 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
326   unwrap<GlobalVariable>(GlobalVar)
327     ->setInitializer(unwrap<Constant>(ConstantVal));
328 }
329
330 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
331   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
332 }
333
334 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
335   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
336 }
337
338 /*--.. Operations on functions .............................................--*/
339
340 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
341                              LLVMTypeRef FunctionTy) {
342   return wrap(new Function(unwrap<FunctionType>(FunctionTy),
343                            GlobalValue::ExternalLinkage, Name, unwrap(M)));
344 }
345
346 void LLVMDeleteFunction(LLVMValueRef Fn) {
347   unwrap<Function>(Fn)->eraseFromParent();
348 }
349
350 unsigned LLVMCountParams(LLVMValueRef FnRef) {
351   // This function is strictly redundant to
352   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
353   return unwrap<Function>(FnRef)->getArgumentList().size();
354 }
355
356 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
357   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
358   while (index --> 0)
359     AI++;
360   return wrap(AI);
361 }
362
363 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
364   Function *Fn = unwrap<Function>(FnRef);
365   for (Function::arg_iterator I = Fn->arg_begin(),
366                               E = Fn->arg_end(); I != E; I++)
367     *ParamRefs++ = wrap(I);
368 }
369
370 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
371   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
372     return F->getIntrinsicID();
373   return 0;
374 }
375
376 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
377   return unwrap<Function>(Fn)->getCallingConv();
378 }
379
380 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
381   return unwrap<Function>(Fn)->setCallingConv(CC);
382 }
383
384 /*--.. Operations on basic blocks ..........................................--*/
385
386 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
387   return wrap(static_cast<Value*>(unwrap(Bb)));
388 }
389
390 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
391   return isa<BasicBlock>(unwrap(Val));
392 }
393
394 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
395   return wrap(unwrap<BasicBlock>(Val));
396 }
397
398 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
399   return unwrap<Function>(FnRef)->getBasicBlockList().size();
400 }
401
402 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
403   Function *Fn = unwrap<Function>(FnRef);
404   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
405     *BasicBlocksRefs++ = wrap(I);
406 }
407
408 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
409   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
410 }
411
412 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
413   return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
414 }
415
416 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
417                                        const char *Name) {
418   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
419   return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
420                              InsertBeforeBB));
421 }
422
423 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
424   unwrap(BBRef)->eraseFromParent();
425 }
426
427 /*--.. Call and invoke instructions ........................................--*/
428
429 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
430   Value *V = unwrap(Instr);
431   if (CallInst *CI = dyn_cast<CallInst>(V))
432     return CI->getCallingConv();
433   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
434     return II->getCallingConv();
435   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
436   return 0;
437 }
438
439 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
440   Value *V = unwrap(Instr);
441   if (CallInst *CI = dyn_cast<CallInst>(V))
442     return CI->setCallingConv(CC);
443   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
444     return II->setCallingConv(CC);
445   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
446 }
447
448
449 /*===-- Instruction builders ----------------------------------------------===*/
450
451 LLVMBuilderRef LLVMCreateBuilder() {
452   return wrap(new LLVMBuilder());
453 }
454
455 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
456   Instruction *I = unwrap<Instruction>(Instr);
457   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
458 }
459
460 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
461   BasicBlock *BB = unwrap(Block);
462   unwrap(Builder)->SetInsertPoint(BB);
463 }
464
465 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
466   delete unwrap(Builder);
467 }
468
469 /*--.. Instruction builders ................................................--*/
470
471 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
472   return wrap(unwrap(B)->CreateRetVoid());
473 }
474
475 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
476   return wrap(unwrap(B)->CreateRet(unwrap(V)));
477 }
478
479 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
480   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
481 }
482
483 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
484                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
485   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
486 }
487
488 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
489                              LLVMBasicBlockRef Else, unsigned NumCases) {
490   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
491 }
492
493 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
494                              LLVMValueRef *Args, unsigned NumArgs,
495                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
496                              const char *Name) {
497   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
498                                       unwrap(Args), unwrap(Args) + NumArgs,
499                                       Name));
500 }
501
502 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
503   return wrap(unwrap(B)->CreateUnwind());
504 }
505
506 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
507   return wrap(unwrap(B)->CreateUnreachable());
508 }
509
510 /*--.. Arithmetic ..........................................................--*/
511
512 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
513                           const char *Name) {
514   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
515 }
516
517 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
518                           const char *Name) {
519   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
520 }
521
522 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
523                           const char *Name) {
524   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
525 }
526
527 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
528                            const char *Name) {
529   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
530 }
531
532 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
533                            const char *Name) {
534   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
535 }
536
537 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
538                            const char *Name) {
539   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
540 }
541
542 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
543                            const char *Name) {
544   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
545 }
546
547 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
548                            const char *Name) {
549   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
550 }
551
552 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
553                            const char *Name) {
554   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
555 }
556
557 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
558                           const char *Name) {
559   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
560 }
561
562 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
563                            const char *Name) {
564   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
565 }
566
567 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
568                            const char *Name) {
569   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
570 }
571
572 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
573                           const char *Name) {
574   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
575 }
576
577 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
578                          const char *Name) {
579   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
580 }
581
582 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
583                           const char *Name) {
584   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
585 }
586
587 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
588   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
589 }
590
591 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
592   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
593 }
594
595 /*--.. Memory ..............................................................--*/
596
597 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
598                              const char *Name) {
599   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
600 }
601
602 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
603                                   LLVMValueRef Val, const char *Name) {
604   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
605 }
606
607 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
608                              const char *Name) {
609   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
610 }
611
612 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
613                                   LLVMValueRef Val, const char *Name) {
614   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
615 }
616
617 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
618   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
619 }
620
621
622 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
623                            const char *Name) {
624   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
625 }
626
627 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
628                             LLVMValueRef PointerVal) {
629   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
630 }
631
632 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
633                           LLVMValueRef *Indices, unsigned NumIndices,
634                           const char *Name) {
635   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
636                                    unwrap(Indices) + NumIndices, Name));
637 }
638
639 /*--.. Casts ...............................................................--*/
640
641 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
642                             LLVMTypeRef DestTy, const char *Name) {
643   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
644 }
645
646 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
647                            LLVMTypeRef DestTy, const char *Name) {
648   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
649 }
650
651 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
652                            LLVMTypeRef DestTy, const char *Name) {
653   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
654 }
655
656 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
657                              LLVMTypeRef DestTy, const char *Name) {
658   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
659 }
660
661 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
662                              LLVMTypeRef DestTy, const char *Name) {
663   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
664 }
665
666 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
667                              LLVMTypeRef DestTy, const char *Name) {
668   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
669 }
670
671 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
672                              LLVMTypeRef DestTy, const char *Name) {
673   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
674 }
675
676 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
677                               LLVMTypeRef DestTy, const char *Name) {
678   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
679 }
680
681 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
682                             LLVMTypeRef DestTy, const char *Name) {
683   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
684 }
685
686 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
687                                LLVMTypeRef DestTy, const char *Name) {
688   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
689 }
690
691 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
692                                LLVMTypeRef DestTy, const char *Name) {
693   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
694 }
695
696 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
697                               LLVMTypeRef DestTy, const char *Name) {
698   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
699 }
700
701 /*--.. Comparisons .........................................................--*/
702
703 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
704                            LLVMValueRef LHS, LLVMValueRef RHS,
705                            const char *Name) {
706   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
707                                     unwrap(LHS), unwrap(RHS), Name));
708 }
709
710 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
711                            LLVMValueRef LHS, LLVMValueRef RHS,
712                            const char *Name) {
713   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
714                                     unwrap(LHS), unwrap(RHS), Name));
715 }
716
717 /*--.. Miscellaneous instructions ..........................................--*/
718
719 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
720   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
721 }
722
723 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
724                            LLVMValueRef *Args, unsigned NumArgs,
725                            const char *Name) {
726   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
727                                     unwrap(Args) + NumArgs, Name));
728 }
729
730 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
731                              LLVMValueRef Then, LLVMValueRef Else,
732                              const char *Name) {
733   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
734                                       Name));
735 }
736
737 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
738                             LLVMTypeRef Ty, const char *Name) {
739   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
740 }
741
742 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
743                                       LLVMValueRef Index, const char *Name) {
744   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
745                                               Name));
746 }
747
748 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
749                                     LLVMValueRef EltVal, LLVMValueRef Index,
750                                     const char *Name) {
751   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
752                                              unwrap(Index), Name));
753 }
754
755 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
756                                     LLVMValueRef V2, LLVMValueRef Mask,
757                                     const char *Name) {
758   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
759                                              unwrap(Mask), Name));
760 }