Demoting CHelpers.h to include/llvm/Support.
[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/Support/CHelpers.h"
21 #include "llvm/Support/LLVMBuilder.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include <cassert>
24
25 using namespace llvm;
26
27 namespace {
28   /// Opaque builder conversions.
29   /// 
30   inline LLVMBuilder *unwrap(LLVMBuilderRef B) {
31     return reinterpret_cast<LLVMBuilder*>(B);
32   }
33   
34   inline LLVMBuilderRef wrap(LLVMBuilder *B) {
35     return reinterpret_cast<LLVMBuilderRef>(B);
36   }
37 }
38
39
40 /*===-- Operations on modules ---------------------------------------------===*/
41
42 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
43   return wrap(new Module(ModuleID));
44 }
45
46 void LLVMDisposeModule(LLVMModuleRef M) {
47   delete unwrap(M);
48 }
49
50 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
51   return unwrap(M)->addTypeName(Name, unwrap(Ty));
52 }
53
54 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
55   std::string N(Name);
56   
57   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
58   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
59     if (I->first == N)
60       TST.remove(I);
61 }
62
63
64 /*===-- Operations on types -----------------------------------------------===*/
65
66 /*--.. Operations on all types (mostly) ....................................--*/
67
68 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
69   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
70 }
71
72 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
73   DerivedType *Ty = unwrap<DerivedType>(AbstractType);
74   Ty->refineAbstractTypeTo(unwrap(ConcreteType));
75 }
76
77 /*--.. Operations on integer types .........................................--*/
78
79 LLVMTypeRef LLVMInt1Type()  { return (LLVMTypeRef) Type::Int1Ty;  }
80 LLVMTypeRef LLVMInt8Type()  { return (LLVMTypeRef) Type::Int8Ty;  }
81 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
82 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
83 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
84
85 LLVMTypeRef LLVMCreateIntType(unsigned NumBits) {
86   return wrap(IntegerType::get(NumBits));
87 }
88
89 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
90   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
91 }
92
93 /*--.. Operations on real types ............................................--*/
94
95 LLVMTypeRef LLVMFloatType()    { return (LLVMTypeRef) Type::FloatTy;     }
96 LLVMTypeRef LLVMDoubleType()   { return (LLVMTypeRef) Type::DoubleTy;    }
97 LLVMTypeRef LLVMX86FP80Type()  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
98 LLVMTypeRef LLVMFP128Type()    { return (LLVMTypeRef) Type::FP128Ty;     }
99 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
100
101 /*--.. Operations on function types ........................................--*/
102
103 LLVMTypeRef LLVMCreateFunctionType(LLVMTypeRef ReturnType,
104                            LLVMTypeRef *ParamTypes, unsigned ParamCount,
105                            int IsVarArg) {
106   std::vector<const Type*> Tys;
107   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
108     Tys.push_back(unwrap(*I));
109   
110   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
111 }
112
113 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
114   return unwrap<FunctionType>(FunctionTy)->isVarArg();
115 }
116
117 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
118   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
119 }
120
121 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
122   return unwrap<FunctionType>(FunctionTy)->getNumParams();
123 }
124
125 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
126   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
127   for (FunctionType::param_iterator I = Ty->param_begin(),
128                                     E = Ty->param_end(); I != E; ++I)
129     *Dest++ = wrap(*I);
130 }
131
132 /*--.. Operations on struct types ..........................................--*/
133
134 LLVMTypeRef LLVMCreateStructType(LLVMTypeRef *ElementTypes,
135                                  unsigned ElementCount, int Packed) {
136   std::vector<const Type*> Tys;
137   for (LLVMTypeRef *I = ElementTypes,
138                    *E = ElementTypes + ElementCount; I != E; ++I)
139     Tys.push_back(unwrap(*I));
140   
141   return wrap(StructType::get(Tys, Packed != 0));
142 }
143
144 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
145   return unwrap<StructType>(StructTy)->getNumElements();
146 }
147
148 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
149   StructType *Ty = unwrap<StructType>(StructTy);
150   for (FunctionType::param_iterator I = Ty->element_begin(),
151                                     E = Ty->element_end(); I != E; ++I)
152     *Dest++ = wrap(*I);
153 }
154
155 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
156   return unwrap<StructType>(StructTy)->isPacked();
157 }
158
159 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
160
161 LLVMTypeRef LLVMCreateArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
162   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
163 }
164
165 LLVMTypeRef LLVMCreatePointerType(LLVMTypeRef ElementType) {
166   return wrap(PointerType::get(unwrap(ElementType)));
167 }
168
169 LLVMTypeRef LLVMCreateVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
170   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
171 }
172
173 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
174   return wrap(unwrap<SequentialType>(Ty)->getElementType());
175 }
176
177 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
178   return unwrap<ArrayType>(ArrayTy)->getNumElements();
179 }
180
181 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
182   return unwrap<VectorType>(VectorTy)->getNumElements();
183 }
184
185 /*--.. Operations on other types ...........................................--*/
186
187 LLVMTypeRef LLVMVoidType()  { return (LLVMTypeRef) Type::VoidTy;  }
188 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
189
190 LLVMTypeRef LLVMCreateOpaqueType() {
191   return wrap(llvm::OpaqueType::get());
192 }
193
194
195 /*===-- Operations on values ----------------------------------------------===*/
196
197 /*--.. Operations on all values ............................................--*/
198
199 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
200   return wrap(unwrap(Val)->getType());
201 }
202
203 const char *LLVMGetValueName(LLVMValueRef Val) {
204   return unwrap(Val)->getNameStart();
205 }
206
207 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
208   unwrap(Val)->setName(Name);
209 }
210
211 /*--.. Operations on constants of any type .................................--*/
212
213 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty) {
214   return wrap(Constant::getNullValue(unwrap(Ty)));
215 }
216
217 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty) {
218   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
219 }
220
221 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
222   return wrap(UndefValue::get(unwrap(Ty)));
223 }
224
225 int LLVMIsConstant(LLVMValueRef Ty) {
226   return isa<Constant>(unwrap(Ty));
227 }
228
229 int LLVMIsNull(LLVMValueRef Val) {
230   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
231     return C->isNullValue();
232   return false;
233 }
234
235 int LLVMIsUndef(LLVMValueRef Val) {
236   return isa<UndefValue>(unwrap(Val));
237 }
238
239 /*--.. Operations on scalar constants ......................................--*/
240
241 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
242                                 int SignExtend) {
243   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
244 }
245
246 LLVMValueRef LLVMGetRealConstant(LLVMTypeRef RealTy, double N) {
247   return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
248 }
249
250 /*--.. Operations on composite constants ...................................--*/
251
252 LLVMValueRef LLVMGetStringConstant(const char *Str, unsigned Length,
253                                    int DontNullTerminate) {
254   /* Inverted the sense of AddNull because ', 0)' is a
255      better mnemonic for null termination than ', 1)'. */
256   return wrap(ConstantArray::get(std::string(Str, Length),
257                                  DontNullTerminate == 0));
258 }
259
260 LLVMValueRef LLVMGetArrayConstant(LLVMTypeRef ElementTy,
261                                   LLVMValueRef *ConstantVals, unsigned Length) {
262   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
263                                  unwrap<Constant>(ConstantVals, Length),
264                                  Length));
265 }
266
267 LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count,
268                                    int Packed) {
269   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
270                                   Count, Packed != 0));
271 }
272
273 LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals,
274                                    unsigned Size) {
275   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
276                                   Size));
277 }
278
279 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
280
281 int LLVMIsDeclaration(LLVMValueRef Global) {
282   return unwrap<GlobalValue>(Global)->isDeclaration();
283 }
284
285 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
286   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
287 }
288
289 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
290   unwrap<GlobalValue>(Global)
291     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
292 }
293
294 const char *LLVMGetSection(LLVMValueRef Global) {
295   return unwrap<GlobalValue>(Global)->getSection().c_str();
296 }
297
298 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
299   unwrap<GlobalValue>(Global)->setSection(Section);
300 }
301
302 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
303   return static_cast<LLVMVisibility>(
304     unwrap<GlobalValue>(Global)->getVisibility());
305 }
306
307 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
308   unwrap<GlobalValue>(Global)
309     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
310 }
311
312 unsigned LLVMGetAlignment(LLVMValueRef Global) {
313   return unwrap<GlobalValue>(Global)->getAlignment();
314 }
315
316 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
317   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
318 }
319
320 /*--.. Operations on global variables ......................................--*/
321
322 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
323   return wrap(new GlobalVariable(unwrap(Ty), false,
324               GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
325 }
326
327 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
328   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
329 }
330
331 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
332   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
333 }
334
335 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
336   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
337 }
338
339 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
340   unwrap<GlobalVariable>(GlobalVar)
341     ->setInitializer(unwrap<Constant>(ConstantVal));
342 }
343
344 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
345   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
346 }
347
348 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
349   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
350 }
351
352 /*--.. Operations on functions .............................................--*/
353
354 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
355                              LLVMTypeRef FunctionTy) {
356   return wrap(new Function(unwrap<FunctionType>(FunctionTy),
357                            GlobalValue::ExternalLinkage, Name, unwrap(M)));
358 }
359
360 void LLVMDeleteFunction(LLVMValueRef Fn) {
361   unwrap<Function>(Fn)->eraseFromParent();
362 }
363
364 unsigned LLVMCountParams(LLVMValueRef FnRef) {
365   // This function is strictly redundant to
366   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
367   return unwrap<Function>(FnRef)->getArgumentList().size();
368 }
369
370 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
371   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
372   while (index --> 0)
373     AI++;
374   return wrap(AI);
375 }
376
377 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
378   Function *Fn = unwrap<Function>(FnRef);
379   for (Function::arg_iterator I = Fn->arg_begin(),
380                               E = Fn->arg_end(); I != E; I++)
381     *ParamRefs++ = wrap(I);
382 }
383
384 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
385   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
386     return F->getIntrinsicID();
387   return 0;
388 }
389
390 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
391   return unwrap<Function>(Fn)->getCallingConv();
392 }
393
394 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
395   return unwrap<Function>(Fn)->setCallingConv(CC);
396 }
397
398 /*--.. Operations on basic blocks ..........................................--*/
399
400 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
401   return wrap(static_cast<Value*>(unwrap(Bb)));
402 }
403
404 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
405   return isa<BasicBlock>(unwrap(Val));
406 }
407
408 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
409   return wrap(unwrap<BasicBlock>(Val));
410 }
411
412 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
413   return unwrap<Function>(FnRef)->getBasicBlockList().size();
414 }
415
416 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
417   Function *Fn = unwrap<Function>(FnRef);
418   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
419     *BasicBlocksRefs++ = wrap(I);
420 }
421
422 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
423   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
424 }
425
426 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
427   return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
428 }
429
430 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
431                                        const char *Name) {
432   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
433   return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
434                              InsertBeforeBB));
435 }
436
437 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
438   unwrap(BBRef)->eraseFromParent();
439 }
440
441 /*--.. Call and invoke instructions ........................................--*/
442
443 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
444   Value *V = unwrap(Instr);
445   if (CallInst *CI = dyn_cast<CallInst>(V))
446     return CI->getCallingConv();
447   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
448     return II->getCallingConv();
449   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
450   return 0;
451 }
452
453 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
454   Value *V = unwrap(Instr);
455   if (CallInst *CI = dyn_cast<CallInst>(V))
456     return CI->setCallingConv(CC);
457   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
458     return II->setCallingConv(CC);
459   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
460 }
461
462
463 /*===-- Instruction builders ----------------------------------------------===*/
464
465 LLVMBuilderRef LLVMCreateBuilder() {
466   return wrap(new LLVMBuilder());
467 }
468
469 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
470   Instruction *I = unwrap<Instruction>(Instr);
471   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
472 }
473
474 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
475   BasicBlock *BB = unwrap(Block);
476   unwrap(Builder)->SetInsertPoint(BB);
477 }
478
479 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
480   delete unwrap(Builder);
481 }
482
483 /*--.. Instruction builders ................................................--*/
484
485 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
486   return wrap(unwrap(B)->CreateRetVoid());
487 }
488
489 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
490   return wrap(unwrap(B)->CreateRet(unwrap(V)));
491 }
492
493 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
494   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
495 }
496
497 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
498                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
499   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
500 }
501
502 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
503                              LLVMBasicBlockRef Else, unsigned NumCases) {
504   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
505 }
506
507 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
508                              LLVMValueRef *Args, unsigned NumArgs,
509                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
510                              const char *Name) {
511   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
512                                       unwrap(Args), unwrap(Args) + NumArgs,
513                                       Name));
514 }
515
516 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
517   return wrap(unwrap(B)->CreateUnwind());
518 }
519
520 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
521   return wrap(unwrap(B)->CreateUnreachable());
522 }
523
524 /*--.. Arithmetic ..........................................................--*/
525
526 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
527                           const char *Name) {
528   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
529 }
530
531 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
532                           const char *Name) {
533   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
534 }
535
536 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
537                           const char *Name) {
538   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
539 }
540
541 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
542                            const char *Name) {
543   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
544 }
545
546 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
547                            const char *Name) {
548   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
549 }
550
551 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
552                            const char *Name) {
553   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
554 }
555
556 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
557                            const char *Name) {
558   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
559 }
560
561 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
562                            const char *Name) {
563   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
564 }
565
566 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
567                            const char *Name) {
568   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
569 }
570
571 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
572                           const char *Name) {
573   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
574 }
575
576 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
577                            const char *Name) {
578   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
579 }
580
581 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
582                            const char *Name) {
583   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
584 }
585
586 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
587                           const char *Name) {
588   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
589 }
590
591 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
592                          const char *Name) {
593   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
594 }
595
596 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
597                           const char *Name) {
598   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
599 }
600
601 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
602   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
603 }
604
605 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
606   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
607 }
608
609 /*--.. Memory ..............................................................--*/
610
611 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
612                              const char *Name) {
613   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
614 }
615
616 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
617                                   LLVMValueRef Val, const char *Name) {
618   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
619 }
620
621 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
622                              const char *Name) {
623   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
624 }
625
626 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
627                                   LLVMValueRef Val, const char *Name) {
628   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
629 }
630
631 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
632   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
633 }
634
635
636 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
637                            const char *Name) {
638   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
639 }
640
641 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
642                             LLVMValueRef PointerVal) {
643   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
644 }
645
646 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
647                           LLVMValueRef *Indices, unsigned NumIndices,
648                           const char *Name) {
649   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
650                                    unwrap(Indices) + NumIndices, Name));
651 }
652
653 /*--.. Casts ...............................................................--*/
654
655 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
656                             LLVMTypeRef DestTy, const char *Name) {
657   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
658 }
659
660 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
661                            LLVMTypeRef DestTy, const char *Name) {
662   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
663 }
664
665 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
666                            LLVMTypeRef DestTy, const char *Name) {
667   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
668 }
669
670 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
671                              LLVMTypeRef DestTy, const char *Name) {
672   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
673 }
674
675 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
676                              LLVMTypeRef DestTy, const char *Name) {
677   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
678 }
679
680 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
681                              LLVMTypeRef DestTy, const char *Name) {
682   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
683 }
684
685 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
686                              LLVMTypeRef DestTy, const char *Name) {
687   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
688 }
689
690 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
691                               LLVMTypeRef DestTy, const char *Name) {
692   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
693 }
694
695 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
696                             LLVMTypeRef DestTy, const char *Name) {
697   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
698 }
699
700 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
701                                LLVMTypeRef DestTy, const char *Name) {
702   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
703 }
704
705 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
706                                LLVMTypeRef DestTy, const char *Name) {
707   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
708 }
709
710 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
711                               LLVMTypeRef DestTy, const char *Name) {
712   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
713 }
714
715 /*--.. Comparisons .........................................................--*/
716
717 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
718                            LLVMValueRef LHS, LLVMValueRef RHS,
719                            const char *Name) {
720   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
721                                     unwrap(LHS), unwrap(RHS), Name));
722 }
723
724 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
725                            LLVMValueRef LHS, LLVMValueRef RHS,
726                            const char *Name) {
727   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
728                                     unwrap(LHS), unwrap(RHS), Name));
729 }
730
731 /*--.. Miscellaneous instructions ..........................................--*/
732
733 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
734   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
735 }
736
737 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
738                            LLVMValueRef *Args, unsigned NumArgs,
739                            const char *Name) {
740   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
741                                     unwrap(Args) + NumArgs, Name));
742 }
743
744 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
745                              LLVMValueRef Then, LLVMValueRef Else,
746                              const char *Name) {
747   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
748                                       Name));
749 }
750
751 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
752                             LLVMTypeRef Ty, const char *Name) {
753   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
754 }
755
756 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
757                                       LLVMValueRef Index, const char *Name) {
758   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
759                                               Name));
760 }
761
762 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
763                                     LLVMValueRef EltVal, LLVMValueRef Index,
764                                     const char *Name) {
765   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
766                                              unwrap(Index), Name));
767 }
768
769 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
770                                     LLVMValueRef V2, LLVMValueRef Mask,
771                                     const char *Name) {
772   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
773                                              unwrap(Mask), Name));
774 }