1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
22 #include "llvm/Support/MemoryBuffer.h"
29 /*===-- Error handling ----------------------------------------------------===*/
31 void LLVMDisposeMessage(char *Message) {
36 /*===-- Operations on modules ---------------------------------------------===*/
38 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
39 return wrap(new Module(ModuleID));
42 void LLVMDisposeModule(LLVMModuleRef M) {
46 /*--.. Data layout .........................................................--*/
47 const char * LLVMGetDataLayout(LLVMModuleRef M) {
48 return unwrap(M)->getDataLayout().c_str();
51 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
52 unwrap(M)->setDataLayout(Triple);
55 /*--.. Target triple .......................................................--*/
56 const char * LLVMGetTarget(LLVMModuleRef M) {
57 return unwrap(M)->getTargetTriple().c_str();
60 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
61 unwrap(M)->setTargetTriple(Triple);
64 /*--.. Type names ..........................................................--*/
65 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
66 return unwrap(M)->addTypeName(Name, unwrap(Ty));
69 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
72 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
73 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
79 /*===-- Operations on types -----------------------------------------------===*/
81 /*--.. Operations on all types (mostly) ....................................--*/
83 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
84 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
87 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
88 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
89 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
92 /*--.. Operations on integer types .........................................--*/
94 LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
95 LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
96 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
97 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
98 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
100 LLVMTypeRef LLVMIntType(unsigned NumBits) {
101 return wrap(IntegerType::get(NumBits));
104 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
105 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
108 /*--.. Operations on real types ............................................--*/
110 LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
111 LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
112 LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
113 LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
114 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
116 /*--.. Operations on function types ........................................--*/
118 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
119 LLVMTypeRef *ParamTypes, unsigned ParamCount,
121 std::vector<const Type*> Tys;
122 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
123 Tys.push_back(unwrap(*I));
125 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
128 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
129 return unwrap<FunctionType>(FunctionTy)->isVarArg();
132 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
133 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
136 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
137 return unwrap<FunctionType>(FunctionTy)->getNumParams();
140 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
141 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
142 for (FunctionType::param_iterator I = Ty->param_begin(),
143 E = Ty->param_end(); I != E; ++I)
147 /*--.. Operations on struct types ..........................................--*/
149 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
150 unsigned ElementCount, int Packed) {
151 std::vector<const Type*> Tys;
152 for (LLVMTypeRef *I = ElementTypes,
153 *E = ElementTypes + ElementCount; I != E; ++I)
154 Tys.push_back(unwrap(*I));
156 return wrap(StructType::get(Tys, Packed != 0));
159 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
160 return unwrap<StructType>(StructTy)->getNumElements();
163 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
164 StructType *Ty = unwrap<StructType>(StructTy);
165 for (FunctionType::param_iterator I = Ty->element_begin(),
166 E = Ty->element_end(); I != E; ++I)
170 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
171 return unwrap<StructType>(StructTy)->isPacked();
174 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
176 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
177 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
180 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
181 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
184 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
185 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
188 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
189 return wrap(unwrap<SequentialType>(Ty)->getElementType());
192 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
193 return unwrap<ArrayType>(ArrayTy)->getNumElements();
196 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
197 return unwrap<PointerType>(PointerTy)->getAddressSpace();
200 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
201 return unwrap<VectorType>(VectorTy)->getNumElements();
204 /*--.. Operations on other types ...........................................--*/
206 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
207 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
209 LLVMTypeRef LLVMOpaqueType() {
210 return wrap(llvm::OpaqueType::get());
213 /* Operations on type handles */
215 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
216 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
219 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
220 delete unwrap(TypeHandle);
223 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
224 return wrap(unwrap(TypeHandle)->get());
227 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
228 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
232 /*===-- Operations on values ----------------------------------------------===*/
234 /*--.. Operations on all values ............................................--*/
236 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
237 return wrap(unwrap(Val)->getType());
240 const char *LLVMGetValueName(LLVMValueRef Val) {
241 return unwrap(Val)->getNameStart();
244 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
245 unwrap(Val)->setName(Name);
248 void LLVMDumpValue(LLVMValueRef Val) {
252 /*--.. Operations on constants of any type .................................--*/
254 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
255 return wrap(Constant::getNullValue(unwrap(Ty)));
258 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
259 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
262 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
263 return wrap(UndefValue::get(unwrap(Ty)));
266 int LLVMIsConstant(LLVMValueRef Ty) {
267 return isa<Constant>(unwrap(Ty));
270 int LLVMIsNull(LLVMValueRef Val) {
271 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
272 return C->isNullValue();
276 int LLVMIsUndef(LLVMValueRef Val) {
277 return isa<UndefValue>(unwrap(Val));
280 /*--.. Operations on scalar constants ......................................--*/
282 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
284 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
287 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
288 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
291 /*--.. Operations on composite constants ...................................--*/
293 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
294 int DontNullTerminate) {
295 /* Inverted the sense of AddNull because ', 0)' is a
296 better mnemonic for null termination than ', 1)'. */
297 return wrap(ConstantArray::get(std::string(Str, Length),
298 DontNullTerminate == 0));
301 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
302 LLVMValueRef *ConstantVals, unsigned Length) {
303 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
304 unwrap<Constant>(ConstantVals, Length),
308 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
310 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
311 Count, Packed != 0));
314 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
315 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
319 /*--.. Constant expressions ................................................--*/
321 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
322 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
325 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
326 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
329 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
330 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
333 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
334 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
335 unwrap<Constant>(RHSConstant)));
338 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
339 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
340 unwrap<Constant>(RHSConstant)));
343 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
344 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
345 unwrap<Constant>(RHSConstant)));
348 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
349 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
350 unwrap<Constant>(RHSConstant)));
353 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
354 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
355 unwrap<Constant>(RHSConstant)));
358 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
359 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
360 unwrap<Constant>(RHSConstant)));
363 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
364 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
365 unwrap<Constant>(RHSConstant)));
368 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
369 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
370 unwrap<Constant>(RHSConstant)));
373 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
374 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
375 unwrap<Constant>(RHSConstant)));
378 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
379 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
380 unwrap<Constant>(RHSConstant)));
383 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
384 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
385 unwrap<Constant>(RHSConstant)));
388 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
389 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
390 unwrap<Constant>(RHSConstant)));
393 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
394 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
395 return wrap(ConstantExpr::getICmp(Predicate,
396 unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
400 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
401 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
402 return wrap(ConstantExpr::getFCmp(Predicate,
403 unwrap<Constant>(LHSConstant),
404 unwrap<Constant>(RHSConstant)));
407 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
408 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
409 unwrap<Constant>(RHSConstant)));
412 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
413 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
414 unwrap<Constant>(RHSConstant)));
417 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
418 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
419 unwrap<Constant>(RHSConstant)));
422 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
423 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
424 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
425 unwrap<Constant>(ConstantIndices,
430 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
431 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
435 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
436 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
440 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
441 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
445 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
446 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
450 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
451 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
455 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
456 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
460 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
461 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
465 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
466 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
470 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
471 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
475 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
476 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
480 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
481 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
485 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
486 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
490 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
491 LLVMValueRef ConstantIfTrue,
492 LLVMValueRef ConstantIfFalse) {
493 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
494 unwrap<Constant>(ConstantIfTrue),
495 unwrap<Constant>(ConstantIfFalse)));
498 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
499 LLVMValueRef IndexConstant) {
500 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
501 unwrap<Constant>(IndexConstant)));
504 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
505 LLVMValueRef ElementValueConstant,
506 LLVMValueRef IndexConstant) {
507 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
508 unwrap<Constant>(ElementValueConstant),
509 unwrap<Constant>(IndexConstant)));
512 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
513 LLVMValueRef VectorBConstant,
514 LLVMValueRef MaskConstant) {
515 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
516 unwrap<Constant>(VectorBConstant),
517 unwrap<Constant>(MaskConstant)));
520 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
522 int LLVMIsDeclaration(LLVMValueRef Global) {
523 return unwrap<GlobalValue>(Global)->isDeclaration();
526 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
527 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
530 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
531 unwrap<GlobalValue>(Global)
532 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
535 const char *LLVMGetSection(LLVMValueRef Global) {
536 return unwrap<GlobalValue>(Global)->getSection().c_str();
539 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
540 unwrap<GlobalValue>(Global)->setSection(Section);
543 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
544 return static_cast<LLVMVisibility>(
545 unwrap<GlobalValue>(Global)->getVisibility());
548 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
549 unwrap<GlobalValue>(Global)
550 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
553 unsigned LLVMGetAlignment(LLVMValueRef Global) {
554 return unwrap<GlobalValue>(Global)->getAlignment();
557 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
558 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
561 /*--.. Operations on global variables ......................................--*/
563 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
564 return wrap(new GlobalVariable(unwrap(Ty), false,
565 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
568 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
569 return wrap(unwrap(M)->getNamedGlobal(Name));
572 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
573 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
576 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
577 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
580 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
581 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
584 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
585 unwrap<GlobalVariable>(GlobalVar)
586 ->setInitializer(unwrap<Constant>(ConstantVal));
589 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
590 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
593 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
594 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
597 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
598 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
601 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
602 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
605 /*--.. Operations on functions .............................................--*/
607 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
608 LLVMTypeRef FunctionTy) {
609 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
610 GlobalValue::ExternalLinkage, Name, unwrap(M)));
613 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
614 return wrap(unwrap(M)->getFunction(Name));
617 void LLVMDeleteFunction(LLVMValueRef Fn) {
618 unwrap<Function>(Fn)->eraseFromParent();
621 unsigned LLVMCountParams(LLVMValueRef FnRef) {
622 // This function is strictly redundant to
623 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
624 return unwrap<Function>(FnRef)->getArgumentList().size();
627 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
628 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
634 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
635 Function *Fn = unwrap<Function>(FnRef);
636 for (Function::arg_iterator I = Fn->arg_begin(),
637 E = Fn->arg_end(); I != E; I++)
638 *ParamRefs++ = wrap(I);
641 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
642 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
643 return F->getIntrinsicID();
647 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
648 return unwrap<Function>(Fn)->getCallingConv();
651 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
652 return unwrap<Function>(Fn)->setCallingConv(CC);
655 const char *LLVMGetCollector(LLVMValueRef Fn) {
656 Function *F = unwrap<Function>(Fn);
657 return F->hasCollector()? F->getCollector() : 0;
660 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
661 Function *F = unwrap<Function>(Fn);
663 F->setCollector(Coll);
668 /*--.. Operations on basic blocks ..........................................--*/
670 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
671 return wrap(static_cast<Value*>(unwrap(Bb)));
674 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
675 return isa<BasicBlock>(unwrap(Val));
678 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
679 return wrap(unwrap<BasicBlock>(Val));
682 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
683 return unwrap<Function>(FnRef)->getBasicBlockList().size();
686 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
687 Function *Fn = unwrap<Function>(FnRef);
688 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
689 *BasicBlocksRefs++ = wrap(I);
692 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
693 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
696 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
697 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
700 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
702 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
703 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
707 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
708 unwrap(BBRef)->eraseFromParent();
711 /*--.. Call and invoke instructions ........................................--*/
713 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
714 Value *V = unwrap(Instr);
715 if (CallInst *CI = dyn_cast<CallInst>(V))
716 return CI->getCallingConv();
717 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
718 return II->getCallingConv();
719 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
723 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
724 Value *V = unwrap(Instr);
725 if (CallInst *CI = dyn_cast<CallInst>(V))
726 return CI->setCallingConv(CC);
727 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
728 return II->setCallingConv(CC);
729 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
732 /*--.. Operations on phi nodes .............................................--*/
734 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
735 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
736 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
737 for (unsigned I = 0; I != Count; ++I)
738 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
741 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
742 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
745 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
746 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
749 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
750 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
754 /*===-- Instruction builders ----------------------------------------------===*/
756 LLVMBuilderRef LLVMCreateBuilder() {
757 return wrap(new LLVMFoldingBuilder());
760 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
761 Instruction *I = unwrap<Instruction>(Instr);
762 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
765 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
766 BasicBlock *BB = unwrap(Block);
767 unwrap(Builder)->SetInsertPoint(BB);
770 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
771 delete unwrap(Builder);
774 /*--.. Instruction builders ................................................--*/
776 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
777 return wrap(unwrap(B)->CreateRetVoid());
780 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
781 return wrap(unwrap(B)->CreateRet(unwrap(V)));
784 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
785 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
788 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
789 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
790 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
793 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
794 LLVMBasicBlockRef Else, unsigned NumCases) {
795 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
798 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
799 LLVMValueRef *Args, unsigned NumArgs,
800 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
802 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
803 unwrap(Args), unwrap(Args) + NumArgs,
807 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
808 return wrap(unwrap(B)->CreateUnwind());
811 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
812 return wrap(unwrap(B)->CreateUnreachable());
815 /*--.. Arithmetic ..........................................................--*/
817 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
819 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
822 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
824 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
827 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
829 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
832 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
834 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
837 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
839 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
842 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
844 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
847 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
849 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
852 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
854 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
857 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
859 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
862 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
864 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
867 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
869 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
872 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
874 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
877 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
879 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
882 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
884 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
887 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
889 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
892 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
893 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
896 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
897 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
900 /*--.. Memory ..............................................................--*/
902 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
904 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
907 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
908 LLVMValueRef Val, const char *Name) {
909 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
912 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
914 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
917 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
918 LLVMValueRef Val, const char *Name) {
919 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
922 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
923 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
927 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
929 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
932 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
933 LLVMValueRef PointerVal) {
934 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
937 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
938 LLVMValueRef *Indices, unsigned NumIndices,
940 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
941 unwrap(Indices) + NumIndices, Name));
944 /*--.. Casts ...............................................................--*/
946 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
947 LLVMTypeRef DestTy, const char *Name) {
948 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
951 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
952 LLVMTypeRef DestTy, const char *Name) {
953 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
956 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
957 LLVMTypeRef DestTy, const char *Name) {
958 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
961 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
962 LLVMTypeRef DestTy, const char *Name) {
963 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
966 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
967 LLVMTypeRef DestTy, const char *Name) {
968 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
971 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
972 LLVMTypeRef DestTy, const char *Name) {
973 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
976 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
977 LLVMTypeRef DestTy, const char *Name) {
978 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
981 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
982 LLVMTypeRef DestTy, const char *Name) {
983 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
986 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
987 LLVMTypeRef DestTy, const char *Name) {
988 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
991 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
992 LLVMTypeRef DestTy, const char *Name) {
993 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
996 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
997 LLVMTypeRef DestTy, const char *Name) {
998 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1001 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1002 LLVMTypeRef DestTy, const char *Name) {
1003 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1006 /*--.. Comparisons .........................................................--*/
1008 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1009 LLVMValueRef LHS, LLVMValueRef RHS,
1011 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1012 unwrap(LHS), unwrap(RHS), Name));
1015 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1016 LLVMValueRef LHS, LLVMValueRef RHS,
1018 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1019 unwrap(LHS), unwrap(RHS), Name));
1022 /*--.. Miscellaneous instructions ..........................................--*/
1024 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1025 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1028 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1029 LLVMValueRef *Args, unsigned NumArgs,
1031 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1032 unwrap(Args) + NumArgs, Name));
1035 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1036 LLVMValueRef Then, LLVMValueRef Else,
1038 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1042 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1043 LLVMTypeRef Ty, const char *Name) {
1044 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1047 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1048 LLVMValueRef Index, const char *Name) {
1049 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1053 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1054 LLVMValueRef EltVal, LLVMValueRef Index,
1056 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1057 unwrap(Index), Name));
1060 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1061 LLVMValueRef V2, LLVMValueRef Mask,
1063 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1064 unwrap(Mask), Name));
1068 /*===-- Module providers --------------------------------------------------===*/
1070 LLVMModuleProviderRef
1071 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1072 return wrap(new ExistingModuleProvider(unwrap(M)));
1075 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1080 /*===-- Memory buffers ----------------------------------------------------===*/
1082 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1083 LLVMMemoryBufferRef *OutMemBuf,
1084 char **OutMessage) {
1086 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1087 *OutMemBuf = wrap(MB);
1091 *OutMessage = strdup(Error.c_str());
1095 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1096 char **OutMessage) {
1097 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1098 *OutMemBuf = wrap(MB);
1102 *OutMessage = strdup("stdin is empty.");
1106 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1107 delete unwrap(MemBuf);