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,
569 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
570 return wrap(unwrap(M)->getNamedGlobal(Name));
573 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
574 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
577 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
578 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
581 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
582 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
585 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
586 unwrap<GlobalVariable>(GlobalVar)
587 ->setInitializer(unwrap<Constant>(ConstantVal));
590 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
591 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
594 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
595 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
598 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
599 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
602 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
603 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
606 /*--.. Operations on functions .............................................--*/
608 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
609 LLVMTypeRef FunctionTy) {
610 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
611 GlobalValue::ExternalLinkage, Name, unwrap(M)));
614 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
615 return wrap(unwrap(M)->getFunction(Name));
618 void LLVMDeleteFunction(LLVMValueRef Fn) {
619 unwrap<Function>(Fn)->eraseFromParent();
622 unsigned LLVMCountParams(LLVMValueRef FnRef) {
623 // This function is strictly redundant to
624 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
625 return unwrap<Function>(FnRef)->getArgumentList().size();
628 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
629 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
635 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
636 Function *Fn = unwrap<Function>(FnRef);
637 for (Function::arg_iterator I = Fn->arg_begin(),
638 E = Fn->arg_end(); I != E; I++)
639 *ParamRefs++ = wrap(I);
642 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
643 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
644 return F->getIntrinsicID();
648 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
649 return unwrap<Function>(Fn)->getCallingConv();
652 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
653 return unwrap<Function>(Fn)->setCallingConv(CC);
656 const char *LLVMGetCollector(LLVMValueRef Fn) {
657 Function *F = unwrap<Function>(Fn);
658 return F->hasCollector()? F->getCollector() : 0;
661 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
662 Function *F = unwrap<Function>(Fn);
664 F->setCollector(Coll);
669 /*--.. Operations on basic blocks ..........................................--*/
671 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
672 return wrap(static_cast<Value*>(unwrap(Bb)));
675 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
676 return isa<BasicBlock>(unwrap(Val));
679 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
680 return wrap(unwrap<BasicBlock>(Val));
683 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
684 return unwrap<Function>(FnRef)->getBasicBlockList().size();
687 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
688 Function *Fn = unwrap<Function>(FnRef);
689 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
690 *BasicBlocksRefs++ = wrap(I);
693 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
694 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
697 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
698 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
701 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
703 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
704 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
708 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
709 unwrap(BBRef)->eraseFromParent();
712 /*--.. Call and invoke instructions ........................................--*/
714 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
715 Value *V = unwrap(Instr);
716 if (CallInst *CI = dyn_cast<CallInst>(V))
717 return CI->getCallingConv();
718 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
719 return II->getCallingConv();
720 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
724 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
725 Value *V = unwrap(Instr);
726 if (CallInst *CI = dyn_cast<CallInst>(V))
727 return CI->setCallingConv(CC);
728 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
729 return II->setCallingConv(CC);
730 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
733 /*--.. Operations on phi nodes .............................................--*/
735 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
736 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
737 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
738 for (unsigned I = 0; I != Count; ++I)
739 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
742 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
743 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
746 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
747 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
750 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
751 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
755 /*===-- Instruction builders ----------------------------------------------===*/
757 LLVMBuilderRef LLVMCreateBuilder() {
758 return wrap(new LLVMFoldingBuilder());
761 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
762 Instruction *I = unwrap<Instruction>(Instr);
763 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
766 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
767 BasicBlock *BB = unwrap(Block);
768 unwrap(Builder)->SetInsertPoint(BB);
771 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
772 delete unwrap(Builder);
775 /*--.. Instruction builders ................................................--*/
777 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
778 return wrap(unwrap(B)->CreateRetVoid());
781 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
782 return wrap(unwrap(B)->CreateRet(unwrap(V)));
785 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
786 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
789 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
790 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
791 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
794 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
795 LLVMBasicBlockRef Else, unsigned NumCases) {
796 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
799 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
800 LLVMValueRef *Args, unsigned NumArgs,
801 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
803 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
804 unwrap(Args), unwrap(Args) + NumArgs,
808 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
809 return wrap(unwrap(B)->CreateUnwind());
812 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
813 return wrap(unwrap(B)->CreateUnreachable());
816 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
817 LLVMBasicBlockRef Dest) {
818 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
821 /*--.. Arithmetic ..........................................................--*/
823 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
825 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
828 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
830 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
833 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
835 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
838 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
840 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
843 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
845 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
848 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
850 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
853 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
855 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
858 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
860 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
863 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
865 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
868 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
870 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
873 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
875 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
878 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
880 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
883 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
885 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
888 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
890 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
893 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
895 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
898 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
899 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
902 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
903 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
906 /*--.. Memory ..............................................................--*/
908 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
910 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
913 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
914 LLVMValueRef Val, const char *Name) {
915 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
918 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
920 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
923 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
924 LLVMValueRef Val, const char *Name) {
925 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
928 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
929 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
933 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
935 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
938 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
939 LLVMValueRef PointerVal) {
940 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
943 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
944 LLVMValueRef *Indices, unsigned NumIndices,
946 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
947 unwrap(Indices) + NumIndices, Name));
950 /*--.. Casts ...............................................................--*/
952 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
953 LLVMTypeRef DestTy, const char *Name) {
954 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
957 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
958 LLVMTypeRef DestTy, const char *Name) {
959 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
962 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
963 LLVMTypeRef DestTy, const char *Name) {
964 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
967 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
968 LLVMTypeRef DestTy, const char *Name) {
969 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
972 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
973 LLVMTypeRef DestTy, const char *Name) {
974 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
977 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
978 LLVMTypeRef DestTy, const char *Name) {
979 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
982 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
983 LLVMTypeRef DestTy, const char *Name) {
984 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
987 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
988 LLVMTypeRef DestTy, const char *Name) {
989 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
992 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
993 LLVMTypeRef DestTy, const char *Name) {
994 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
997 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
998 LLVMTypeRef DestTy, const char *Name) {
999 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1002 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1003 LLVMTypeRef DestTy, const char *Name) {
1004 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1007 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1008 LLVMTypeRef DestTy, const char *Name) {
1009 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1012 /*--.. Comparisons .........................................................--*/
1014 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1015 LLVMValueRef LHS, LLVMValueRef RHS,
1017 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1018 unwrap(LHS), unwrap(RHS), Name));
1021 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1022 LLVMValueRef LHS, LLVMValueRef RHS,
1024 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1025 unwrap(LHS), unwrap(RHS), Name));
1028 /*--.. Miscellaneous instructions ..........................................--*/
1030 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1031 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1034 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1035 LLVMValueRef *Args, unsigned NumArgs,
1037 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1038 unwrap(Args) + NumArgs, Name));
1041 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1042 LLVMValueRef Then, LLVMValueRef Else,
1044 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1048 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1049 LLVMTypeRef Ty, const char *Name) {
1050 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1053 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1054 LLVMValueRef Index, const char *Name) {
1055 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1059 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1060 LLVMValueRef EltVal, LLVMValueRef Index,
1062 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1063 unwrap(Index), Name));
1066 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1067 LLVMValueRef V2, LLVMValueRef Mask,
1069 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1070 unwrap(Mask), Name));
1074 /*===-- Module providers --------------------------------------------------===*/
1076 LLVMModuleProviderRef
1077 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1078 return wrap(new ExistingModuleProvider(unwrap(M)));
1081 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1086 /*===-- Memory buffers ----------------------------------------------------===*/
1088 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1089 LLVMMemoryBufferRef *OutMemBuf,
1090 char **OutMessage) {
1092 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1093 *OutMemBuf = wrap(MB);
1097 *OutMessage = strdup(Error.c_str());
1101 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1102 char **OutMessage) {
1103 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1104 *OutMemBuf = wrap(MB);
1108 *OutMessage = strdup("stdin is empty.");
1112 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1113 delete unwrap(MemBuf);