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, unsigned AddressSpace) {
153 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
156 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
157 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
160 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
161 return wrap(unwrap<SequentialType>(Ty)->getElementType());
164 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
165 return unwrap<ArrayType>(ArrayTy)->getNumElements();
168 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
169 return unwrap<PointerType>(PointerTy)->getAddressSpace();
172 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
173 return unwrap<VectorType>(VectorTy)->getNumElements();
176 /*--.. Operations on other types ...........................................--*/
178 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
179 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
181 LLVMTypeRef LLVMOpaqueType() {
182 return wrap(llvm::OpaqueType::get());
185 /* Operations on type handles */
187 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
188 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
191 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
192 delete unwrap(TypeHandle);
195 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
196 return wrap(unwrap(TypeHandle)->get());
199 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
200 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
204 /*===-- Operations on values ----------------------------------------------===*/
206 /*--.. Operations on all values ............................................--*/
208 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
209 return wrap(unwrap(Val)->getType());
212 const char *LLVMGetValueName(LLVMValueRef Val) {
213 return unwrap(Val)->getNameStart();
216 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
217 unwrap(Val)->setName(Name);
220 void LLVMDumpValue(LLVMValueRef Val) {
224 /*--.. Operations on constants of any type .................................--*/
226 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
227 return wrap(Constant::getNullValue(unwrap(Ty)));
230 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
231 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
234 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
235 return wrap(UndefValue::get(unwrap(Ty)));
238 int LLVMIsConstant(LLVMValueRef Ty) {
239 return isa<Constant>(unwrap(Ty));
242 int LLVMIsNull(LLVMValueRef Val) {
243 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
244 return C->isNullValue();
248 int LLVMIsUndef(LLVMValueRef Val) {
249 return isa<UndefValue>(unwrap(Val));
252 /*--.. Operations on scalar constants ......................................--*/
254 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
256 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
259 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
260 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
263 /*--.. Operations on composite constants ...................................--*/
265 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
266 int DontNullTerminate) {
267 /* Inverted the sense of AddNull because ', 0)' is a
268 better mnemonic for null termination than ', 1)'. */
269 return wrap(ConstantArray::get(std::string(Str, Length),
270 DontNullTerminate == 0));
273 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
274 LLVMValueRef *ConstantVals, unsigned Length) {
275 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
276 unwrap<Constant>(ConstantVals, Length),
280 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
282 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
283 Count, Packed != 0));
286 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
287 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
291 /*--.. Constant expressions ................................................--*/
293 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
294 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
297 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
298 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
301 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
302 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
305 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
306 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
307 unwrap<Constant>(RHSConstant)));
310 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
311 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
312 unwrap<Constant>(RHSConstant)));
315 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
316 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
317 unwrap<Constant>(RHSConstant)));
320 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
321 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
322 unwrap<Constant>(RHSConstant)));
325 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
326 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
327 unwrap<Constant>(RHSConstant)));
330 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
331 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
332 unwrap<Constant>(RHSConstant)));
335 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
336 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
337 unwrap<Constant>(RHSConstant)));
340 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
341 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
342 unwrap<Constant>(RHSConstant)));
345 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
346 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
347 unwrap<Constant>(RHSConstant)));
350 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
351 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
352 unwrap<Constant>(RHSConstant)));
355 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
356 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
357 unwrap<Constant>(RHSConstant)));
360 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
361 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
362 unwrap<Constant>(RHSConstant)));
365 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
366 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
367 return wrap(ConstantExpr::getICmp(Predicate,
368 unwrap<Constant>(LHSConstant),
369 unwrap<Constant>(RHSConstant)));
372 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
373 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
374 return wrap(ConstantExpr::getFCmp(Predicate,
375 unwrap<Constant>(LHSConstant),
376 unwrap<Constant>(RHSConstant)));
379 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
380 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
381 unwrap<Constant>(RHSConstant)));
384 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
385 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
386 unwrap<Constant>(RHSConstant)));
389 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
390 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
391 unwrap<Constant>(RHSConstant)));
394 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
395 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
396 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
397 unwrap<Constant>(ConstantIndices,
402 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
403 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
407 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
408 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
412 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
413 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
417 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
418 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
422 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
423 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
427 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
428 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
432 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
433 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
437 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
438 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
442 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
443 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
447 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
448 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
452 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
453 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
457 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
458 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
462 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
463 LLVMValueRef ConstantIfTrue,
464 LLVMValueRef ConstantIfFalse) {
465 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
466 unwrap<Constant>(ConstantIfTrue),
467 unwrap<Constant>(ConstantIfFalse)));
470 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
471 LLVMValueRef IndexConstant) {
472 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
473 unwrap<Constant>(IndexConstant)));
476 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
477 LLVMValueRef ElementValueConstant,
478 LLVMValueRef IndexConstant) {
479 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
480 unwrap<Constant>(ElementValueConstant),
481 unwrap<Constant>(IndexConstant)));
484 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
485 LLVMValueRef VectorBConstant,
486 LLVMValueRef MaskConstant) {
487 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
488 unwrap<Constant>(VectorBConstant),
489 unwrap<Constant>(MaskConstant)));
492 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
494 int LLVMIsDeclaration(LLVMValueRef Global) {
495 return unwrap<GlobalValue>(Global)->isDeclaration();
498 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
499 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
502 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
503 unwrap<GlobalValue>(Global)
504 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
507 const char *LLVMGetSection(LLVMValueRef Global) {
508 return unwrap<GlobalValue>(Global)->getSection().c_str();
511 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
512 unwrap<GlobalValue>(Global)->setSection(Section);
515 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
516 return static_cast<LLVMVisibility>(
517 unwrap<GlobalValue>(Global)->getVisibility());
520 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
521 unwrap<GlobalValue>(Global)
522 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
525 unsigned LLVMGetAlignment(LLVMValueRef Global) {
526 return unwrap<GlobalValue>(Global)->getAlignment();
529 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
530 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
533 /*--.. Operations on global variables ......................................--*/
535 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
536 return wrap(new GlobalVariable(unwrap(Ty), false,
537 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
540 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
541 return wrap(unwrap(M)->getNamedGlobal(Name));
544 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
545 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
548 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
549 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
552 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
553 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
556 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
557 unwrap<GlobalVariable>(GlobalVar)
558 ->setInitializer(unwrap<Constant>(ConstantVal));
561 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
562 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
565 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
566 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
569 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
570 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
573 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
574 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
577 /*--.. Operations on functions .............................................--*/
579 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
580 LLVMTypeRef FunctionTy) {
581 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
582 GlobalValue::ExternalLinkage, Name, unwrap(M)));
585 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
586 return wrap(unwrap(M)->getFunction(Name));
589 void LLVMDeleteFunction(LLVMValueRef Fn) {
590 unwrap<Function>(Fn)->eraseFromParent();
593 unsigned LLVMCountParams(LLVMValueRef FnRef) {
594 // This function is strictly redundant to
595 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
596 return unwrap<Function>(FnRef)->getArgumentList().size();
599 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
600 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
606 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
607 Function *Fn = unwrap<Function>(FnRef);
608 for (Function::arg_iterator I = Fn->arg_begin(),
609 E = Fn->arg_end(); I != E; I++)
610 *ParamRefs++ = wrap(I);
613 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
614 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
615 return F->getIntrinsicID();
619 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
620 return unwrap<Function>(Fn)->getCallingConv();
623 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
624 return unwrap<Function>(Fn)->setCallingConv(CC);
627 const char *LLVMGetCollector(LLVMValueRef Fn) {
628 Function *F = unwrap<Function>(Fn);
629 return F->hasCollector()? F->getCollector() : 0;
632 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
633 Function *F = unwrap<Function>(Fn);
635 F->setCollector(Coll);
640 /*--.. Operations on basic blocks ..........................................--*/
642 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
643 return wrap(static_cast<Value*>(unwrap(Bb)));
646 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
647 return isa<BasicBlock>(unwrap(Val));
650 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
651 return wrap(unwrap<BasicBlock>(Val));
654 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
655 return unwrap<Function>(FnRef)->getBasicBlockList().size();
658 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
659 Function *Fn = unwrap<Function>(FnRef);
660 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
661 *BasicBlocksRefs++ = wrap(I);
664 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
665 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
668 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
669 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
672 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
674 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
675 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
679 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
680 unwrap(BBRef)->eraseFromParent();
683 /*--.. Call and invoke instructions ........................................--*/
685 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
686 Value *V = unwrap(Instr);
687 if (CallInst *CI = dyn_cast<CallInst>(V))
688 return CI->getCallingConv();
689 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
690 return II->getCallingConv();
691 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
695 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
696 Value *V = unwrap(Instr);
697 if (CallInst *CI = dyn_cast<CallInst>(V))
698 return CI->setCallingConv(CC);
699 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
700 return II->setCallingConv(CC);
701 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
704 /*--.. Operations on phi nodes .............................................--*/
706 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
707 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
708 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
709 for (unsigned I = 0; I != Count; ++I)
710 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
713 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
714 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
717 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
718 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
721 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
722 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
726 /*===-- Instruction builders ----------------------------------------------===*/
728 LLVMBuilderRef LLVMCreateBuilder() {
729 return wrap(new LLVMBuilder());
732 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
733 Instruction *I = unwrap<Instruction>(Instr);
734 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
737 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
738 BasicBlock *BB = unwrap(Block);
739 unwrap(Builder)->SetInsertPoint(BB);
742 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
743 delete unwrap(Builder);
746 /*--.. Instruction builders ................................................--*/
748 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
749 return wrap(unwrap(B)->CreateRetVoid());
752 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
753 return wrap(unwrap(B)->CreateRet(unwrap(V)));
756 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
757 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
760 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
761 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
762 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
765 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
766 LLVMBasicBlockRef Else, unsigned NumCases) {
767 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
770 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
771 LLVMValueRef *Args, unsigned NumArgs,
772 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
774 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
775 unwrap(Args), unwrap(Args) + NumArgs,
779 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
780 return wrap(unwrap(B)->CreateUnwind());
783 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
784 return wrap(unwrap(B)->CreateUnreachable());
787 /*--.. Arithmetic ..........................................................--*/
789 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
791 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
794 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
796 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
799 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
801 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
804 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
806 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
809 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
811 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
814 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
816 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
819 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
821 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
824 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
826 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
829 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
831 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
834 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
836 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
839 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
841 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
844 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
846 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
849 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
851 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
854 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
856 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
859 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
861 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
864 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
865 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
868 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
869 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
872 /*--.. Memory ..............................................................--*/
874 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
876 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
879 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
880 LLVMValueRef Val, const char *Name) {
881 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
884 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
886 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
889 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
890 LLVMValueRef Val, const char *Name) {
891 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
894 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
895 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
899 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
901 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
904 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
905 LLVMValueRef PointerVal) {
906 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
909 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
910 LLVMValueRef *Indices, unsigned NumIndices,
912 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
913 unwrap(Indices) + NumIndices, Name));
916 /*--.. Casts ...............................................................--*/
918 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
919 LLVMTypeRef DestTy, const char *Name) {
920 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
923 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
924 LLVMTypeRef DestTy, const char *Name) {
925 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
928 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
929 LLVMTypeRef DestTy, const char *Name) {
930 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
933 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
934 LLVMTypeRef DestTy, const char *Name) {
935 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
938 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
939 LLVMTypeRef DestTy, const char *Name) {
940 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
943 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
944 LLVMTypeRef DestTy, const char *Name) {
945 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
948 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
949 LLVMTypeRef DestTy, const char *Name) {
950 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
953 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
954 LLVMTypeRef DestTy, const char *Name) {
955 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
958 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
959 LLVMTypeRef DestTy, const char *Name) {
960 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
963 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
964 LLVMTypeRef DestTy, const char *Name) {
965 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
968 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
969 LLVMTypeRef DestTy, const char *Name) {
970 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
973 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
974 LLVMTypeRef DestTy, const char *Name) {
975 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
978 /*--.. Comparisons .........................................................--*/
980 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
981 LLVMValueRef LHS, LLVMValueRef RHS,
983 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
984 unwrap(LHS), unwrap(RHS), Name));
987 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
988 LLVMValueRef LHS, LLVMValueRef RHS,
990 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
991 unwrap(LHS), unwrap(RHS), Name));
994 /*--.. Miscellaneous instructions ..........................................--*/
996 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
997 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1000 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1001 LLVMValueRef *Args, unsigned NumArgs,
1003 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1004 unwrap(Args) + NumArgs, Name));
1007 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1008 LLVMValueRef Then, LLVMValueRef Else,
1010 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1014 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1015 LLVMTypeRef Ty, const char *Name) {
1016 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1019 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1020 LLVMValueRef Index, const char *Name) {
1021 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1025 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1026 LLVMValueRef EltVal, LLVMValueRef Index,
1028 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1029 unwrap(Index), Name));
1032 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1033 LLVMValueRef V2, LLVMValueRef Mask,
1035 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1036 unwrap(Mask), Name));
1040 /*===-- Module providers --------------------------------------------------===*/
1042 LLVMModuleProviderRef
1043 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1044 return wrap(new ExistingModuleProvider(unwrap(M)));
1047 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {