1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
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 "llvm/ModuleProvider.h"
27 /*===-- Operations on modules ---------------------------------------------===*/
29 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
30 return wrap(new Module(ModuleID));
33 void LLVMDisposeModule(LLVMModuleRef M) {
37 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
38 return unwrap(M)->addTypeName(Name, unwrap(Ty));
41 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
44 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
45 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
51 /*===-- Operations on types -----------------------------------------------===*/
53 /*--.. Operations on all types (mostly) ....................................--*/
55 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
56 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
59 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
60 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
61 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
64 /*--.. Operations on integer types .........................................--*/
66 LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
67 LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
68 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
69 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
70 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
72 LLVMTypeRef LLVMIntType(unsigned NumBits) {
73 return wrap(IntegerType::get(NumBits));
76 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
77 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
80 /*--.. Operations on real types ............................................--*/
82 LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
83 LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
84 LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
85 LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
86 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
88 /*--.. Operations on function types ........................................--*/
90 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
91 LLVMTypeRef *ParamTypes, unsigned ParamCount,
93 std::vector<const Type*> Tys;
94 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
95 Tys.push_back(unwrap(*I));
97 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
100 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
101 return unwrap<FunctionType>(FunctionTy)->isVarArg();
104 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
105 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
108 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
109 return unwrap<FunctionType>(FunctionTy)->getNumParams();
112 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
113 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
114 for (FunctionType::param_iterator I = Ty->param_begin(),
115 E = Ty->param_end(); I != E; ++I)
119 /*--.. Operations on struct types ..........................................--*/
121 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
122 unsigned ElementCount, int Packed) {
123 std::vector<const Type*> Tys;
124 for (LLVMTypeRef *I = ElementTypes,
125 *E = ElementTypes + ElementCount; I != E; ++I)
126 Tys.push_back(unwrap(*I));
128 return wrap(StructType::get(Tys, Packed != 0));
131 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
132 return unwrap<StructType>(StructTy)->getNumElements();
135 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
136 StructType *Ty = unwrap<StructType>(StructTy);
137 for (FunctionType::param_iterator I = Ty->element_begin(),
138 E = Ty->element_end(); I != E; ++I)
142 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
143 return unwrap<StructType>(StructTy)->isPacked();
146 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
148 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
149 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
152 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType) {
153 // FIXME: Needst to handle address spaces
154 return wrap(PointerType::getUnqual(unwrap(ElementType)));
157 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
158 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
161 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
162 return wrap(unwrap<SequentialType>(Ty)->getElementType());
165 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
166 return unwrap<ArrayType>(ArrayTy)->getNumElements();
169 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
170 return unwrap<VectorType>(VectorTy)->getNumElements();
173 /*--.. Operations on other types ...........................................--*/
175 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
176 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
178 LLVMTypeRef LLVMOpaqueType() {
179 return wrap(llvm::OpaqueType::get());
182 /* Operations on type handles */
184 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
185 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
188 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
189 delete unwrap(TypeHandle);
192 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
193 return wrap(unwrap(TypeHandle)->get());
196 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
197 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
201 /*===-- Operations on values ----------------------------------------------===*/
203 /*--.. Operations on all values ............................................--*/
205 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
206 return wrap(unwrap(Val)->getType());
209 const char *LLVMGetValueName(LLVMValueRef Val) {
210 return unwrap(Val)->getNameStart();
213 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
214 unwrap(Val)->setName(Name);
217 void LLVMDumpValue(LLVMValueRef Val) {
221 /*--.. Operations on constants of any type .................................--*/
223 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
224 return wrap(Constant::getNullValue(unwrap(Ty)));
227 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
228 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
231 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
232 return wrap(UndefValue::get(unwrap(Ty)));
235 int LLVMIsConstant(LLVMValueRef Ty) {
236 return isa<Constant>(unwrap(Ty));
239 int LLVMIsNull(LLVMValueRef Val) {
240 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
241 return C->isNullValue();
245 int LLVMIsUndef(LLVMValueRef Val) {
246 return isa<UndefValue>(unwrap(Val));
249 /*--.. Operations on scalar constants ......................................--*/
251 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
253 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
256 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
257 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
260 /*--.. Operations on composite constants ...................................--*/
262 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
263 int DontNullTerminate) {
264 /* Inverted the sense of AddNull because ', 0)' is a
265 better mnemonic for null termination than ', 1)'. */
266 return wrap(ConstantArray::get(std::string(Str, Length),
267 DontNullTerminate == 0));
270 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
271 LLVMValueRef *ConstantVals, unsigned Length) {
272 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
273 unwrap<Constant>(ConstantVals, Length),
277 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
279 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
280 Count, Packed != 0));
283 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
284 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
288 /*--.. Constant expressions ................................................--*/
290 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
291 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
294 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
295 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
298 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
299 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
302 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
303 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
304 unwrap<Constant>(RHSConstant)));
307 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
308 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
309 unwrap<Constant>(RHSConstant)));
312 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
313 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
314 unwrap<Constant>(RHSConstant)));
317 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
318 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
319 unwrap<Constant>(RHSConstant)));
322 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
323 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
324 unwrap<Constant>(RHSConstant)));
327 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
328 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
329 unwrap<Constant>(RHSConstant)));
332 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
333 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
334 unwrap<Constant>(RHSConstant)));
337 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
338 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
339 unwrap<Constant>(RHSConstant)));
342 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
343 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
344 unwrap<Constant>(RHSConstant)));
347 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
348 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
349 unwrap<Constant>(RHSConstant)));
352 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
353 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
354 unwrap<Constant>(RHSConstant)));
357 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
358 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
359 unwrap<Constant>(RHSConstant)));
362 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
363 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
364 return wrap(ConstantExpr::getICmp(Predicate,
365 unwrap<Constant>(LHSConstant),
366 unwrap<Constant>(RHSConstant)));
369 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
370 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
371 return wrap(ConstantExpr::getFCmp(Predicate,
372 unwrap<Constant>(LHSConstant),
373 unwrap<Constant>(RHSConstant)));
376 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
377 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
378 unwrap<Constant>(RHSConstant)));
381 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
382 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
383 unwrap<Constant>(RHSConstant)));
386 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
387 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
388 unwrap<Constant>(RHSConstant)));
391 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
392 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
393 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
394 unwrap<Constant>(ConstantIndices,
399 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
400 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
404 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
405 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
409 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
410 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
414 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
415 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
419 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
420 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
424 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
425 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
429 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
430 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
434 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
435 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
439 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
440 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
444 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
445 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
449 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
450 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
454 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
455 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
459 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
460 LLVMValueRef ConstantIfTrue,
461 LLVMValueRef ConstantIfFalse) {
462 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
463 unwrap<Constant>(ConstantIfTrue),
464 unwrap<Constant>(ConstantIfFalse)));
467 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
468 LLVMValueRef IndexConstant) {
469 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
470 unwrap<Constant>(IndexConstant)));
473 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
474 LLVMValueRef ElementValueConstant,
475 LLVMValueRef IndexConstant) {
476 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
477 unwrap<Constant>(ElementValueConstant),
478 unwrap<Constant>(IndexConstant)));
481 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
482 LLVMValueRef VectorBConstant,
483 LLVMValueRef MaskConstant) {
484 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
485 unwrap<Constant>(VectorBConstant),
486 unwrap<Constant>(MaskConstant)));
489 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
491 int LLVMIsDeclaration(LLVMValueRef Global) {
492 return unwrap<GlobalValue>(Global)->isDeclaration();
495 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
496 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
499 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
500 unwrap<GlobalValue>(Global)
501 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
504 const char *LLVMGetSection(LLVMValueRef Global) {
505 return unwrap<GlobalValue>(Global)->getSection().c_str();
508 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
509 unwrap<GlobalValue>(Global)->setSection(Section);
512 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
513 return static_cast<LLVMVisibility>(
514 unwrap<GlobalValue>(Global)->getVisibility());
517 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
518 unwrap<GlobalValue>(Global)
519 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
522 unsigned LLVMGetAlignment(LLVMValueRef Global) {
523 return unwrap<GlobalValue>(Global)->getAlignment();
526 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
527 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
530 /*--.. Operations on global variables ......................................--*/
532 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
533 return wrap(new GlobalVariable(unwrap(Ty), false,
534 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
537 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
538 return wrap(unwrap(M)->getNamedGlobal(Name));
541 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
542 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
545 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
546 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
549 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
550 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
553 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
554 unwrap<GlobalVariable>(GlobalVar)
555 ->setInitializer(unwrap<Constant>(ConstantVal));
558 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
559 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
562 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
563 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
566 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
567 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
570 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
571 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
574 /*--.. Operations on functions .............................................--*/
576 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
577 LLVMTypeRef FunctionTy) {
578 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
579 GlobalValue::ExternalLinkage, Name, unwrap(M)));
582 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
583 return wrap(unwrap(M)->getFunction(Name));
586 void LLVMDeleteFunction(LLVMValueRef Fn) {
587 unwrap<Function>(Fn)->eraseFromParent();
590 unsigned LLVMCountParams(LLVMValueRef FnRef) {
591 // This function is strictly redundant to
592 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
593 return unwrap<Function>(FnRef)->getArgumentList().size();
596 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
597 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
603 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
604 Function *Fn = unwrap<Function>(FnRef);
605 for (Function::arg_iterator I = Fn->arg_begin(),
606 E = Fn->arg_end(); I != E; I++)
607 *ParamRefs++ = wrap(I);
610 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
611 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
612 return F->getIntrinsicID();
616 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
617 return unwrap<Function>(Fn)->getCallingConv();
620 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
621 return unwrap<Function>(Fn)->setCallingConv(CC);
624 const char *LLVMGetCollector(LLVMValueRef Fn) {
625 Function *F = unwrap<Function>(Fn);
626 return F->hasCollector()? F->getCollector() : 0;
629 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
630 Function *F = unwrap<Function>(Fn);
632 F->setCollector(Coll);
637 /*--.. Operations on basic blocks ..........................................--*/
639 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
640 return wrap(static_cast<Value*>(unwrap(Bb)));
643 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
644 return isa<BasicBlock>(unwrap(Val));
647 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
648 return wrap(unwrap<BasicBlock>(Val));
651 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
652 return unwrap<Function>(FnRef)->getBasicBlockList().size();
655 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
656 Function *Fn = unwrap<Function>(FnRef);
657 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
658 *BasicBlocksRefs++ = wrap(I);
661 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
662 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
665 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
666 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
669 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
671 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
672 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
676 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
677 unwrap(BBRef)->eraseFromParent();
680 /*--.. Call and invoke instructions ........................................--*/
682 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
683 Value *V = unwrap(Instr);
684 if (CallInst *CI = dyn_cast<CallInst>(V))
685 return CI->getCallingConv();
686 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
687 return II->getCallingConv();
688 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
692 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
693 Value *V = unwrap(Instr);
694 if (CallInst *CI = dyn_cast<CallInst>(V))
695 return CI->setCallingConv(CC);
696 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
697 return II->setCallingConv(CC);
698 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
701 /*--.. Operations on phi nodes .............................................--*/
703 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
704 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
705 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
706 for (unsigned I = 0; I != Count; ++I)
707 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
710 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
711 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
714 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
715 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
718 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
719 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
723 /*===-- Instruction builders ----------------------------------------------===*/
725 LLVMBuilderRef LLVMCreateBuilder() {
726 return wrap(new LLVMBuilder());
729 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
730 Instruction *I = unwrap<Instruction>(Instr);
731 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
734 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
735 BasicBlock *BB = unwrap(Block);
736 unwrap(Builder)->SetInsertPoint(BB);
739 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
740 delete unwrap(Builder);
743 /*--.. Instruction builders ................................................--*/
745 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
746 return wrap(unwrap(B)->CreateRetVoid());
749 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
750 return wrap(unwrap(B)->CreateRet(unwrap(V)));
753 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
754 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
757 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
758 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
759 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
762 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
763 LLVMBasicBlockRef Else, unsigned NumCases) {
764 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
767 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
768 LLVMValueRef *Args, unsigned NumArgs,
769 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
771 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
772 unwrap(Args), unwrap(Args) + NumArgs,
776 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
777 return wrap(unwrap(B)->CreateUnwind());
780 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
781 return wrap(unwrap(B)->CreateUnreachable());
784 /*--.. Arithmetic ..........................................................--*/
786 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
788 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
791 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
793 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
796 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
798 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
801 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
803 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
806 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
808 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
811 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
813 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
816 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
818 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
821 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
823 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
826 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
828 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
831 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
833 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
836 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
838 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
841 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
843 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
846 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
848 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
851 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
853 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
856 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
858 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
861 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
862 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
865 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
866 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
869 /*--.. Memory ..............................................................--*/
871 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
873 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
876 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
877 LLVMValueRef Val, const char *Name) {
878 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
881 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
883 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
886 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
887 LLVMValueRef Val, const char *Name) {
888 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
891 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
892 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
896 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
898 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
901 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
902 LLVMValueRef PointerVal) {
903 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
906 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
907 LLVMValueRef *Indices, unsigned NumIndices,
909 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
910 unwrap(Indices) + NumIndices, Name));
913 /*--.. Casts ...............................................................--*/
915 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
916 LLVMTypeRef DestTy, const char *Name) {
917 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
920 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
921 LLVMTypeRef DestTy, const char *Name) {
922 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
925 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
926 LLVMTypeRef DestTy, const char *Name) {
927 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
930 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
931 LLVMTypeRef DestTy, const char *Name) {
932 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
935 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
936 LLVMTypeRef DestTy, const char *Name) {
937 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
940 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
941 LLVMTypeRef DestTy, const char *Name) {
942 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
945 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
946 LLVMTypeRef DestTy, const char *Name) {
947 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
950 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
951 LLVMTypeRef DestTy, const char *Name) {
952 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
955 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
956 LLVMTypeRef DestTy, const char *Name) {
957 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
960 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
961 LLVMTypeRef DestTy, const char *Name) {
962 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
965 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
966 LLVMTypeRef DestTy, const char *Name) {
967 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
970 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
971 LLVMTypeRef DestTy, const char *Name) {
972 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
975 /*--.. Comparisons .........................................................--*/
977 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
978 LLVMValueRef LHS, LLVMValueRef RHS,
980 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
981 unwrap(LHS), unwrap(RHS), Name));
984 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
985 LLVMValueRef LHS, LLVMValueRef RHS,
987 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
988 unwrap(LHS), unwrap(RHS), Name));
991 /*--.. Miscellaneous instructions ..........................................--*/
993 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
994 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
997 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
998 LLVMValueRef *Args, unsigned NumArgs,
1000 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1001 unwrap(Args) + NumArgs, Name));
1004 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1005 LLVMValueRef Then, LLVMValueRef Else,
1007 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1011 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1012 LLVMTypeRef Ty, const char *Name) {
1013 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1016 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1017 LLVMValueRef Index, const char *Name) {
1018 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1022 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1023 LLVMValueRef EltVal, LLVMValueRef Index,
1025 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1026 unwrap(Index), Name));
1029 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1030 LLVMValueRef V2, LLVMValueRef Mask,
1032 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1033 unwrap(Mask), Name));
1037 /*===-- Module providers --------------------------------------------------===*/
1039 LLVMModuleProviderRef
1040 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1041 return wrap(new ExistingModuleProvider(unwrap(M)));
1044 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {