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/CHelpers.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/GlobalVariable.h"
28 /*===-- Operations on modules ---------------------------------------------===*/
30 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
31 return wrap(new Module(ModuleID));
34 void LLVMDisposeModule(LLVMModuleRef M) {
38 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
39 return unwrap(M)->addTypeName(Name, unwrap(Ty));
43 /*===-- Operations on types -----------------------------------------------===*/
45 /*--.. Operations on all types (mostly) ....................................--*/
47 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
48 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
51 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
52 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
53 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
56 /*--.. Operations on integer types .........................................--*/
58 LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
59 LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
60 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
61 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
62 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
64 LLVMTypeRef LLVMCreateIntegerType(unsigned NumBits) {
65 return wrap(IntegerType::get(NumBits));
68 unsigned LLVMGetIntegerTypeWidth(LLVMTypeRef IntegerTy) {
69 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
72 /*--.. Operations on real types ............................................--*/
74 LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
75 LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
76 LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
77 LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
78 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
80 /*--.. Operations on function types ........................................--*/
82 LLVMTypeRef LLVMCreateFunctionType(LLVMTypeRef ReturnType,
83 LLVMTypeRef *ParamTypes, unsigned ParamCount,
85 std::vector<const Type*> Tys;
86 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
87 Tys.push_back(unwrap(*I));
89 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
92 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
93 return unwrap<FunctionType>(FunctionTy)->isVarArg();
96 LLVMTypeRef LLVMGetFunctionReturnType(LLVMTypeRef FunctionTy) {
97 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
100 unsigned LLVMGetFunctionParamCount(LLVMTypeRef FunctionTy) {
101 return unwrap<FunctionType>(FunctionTy)->getNumParams();
104 void LLVMGetFunctionParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
105 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
106 for (FunctionType::param_iterator I = Ty->param_begin(),
107 E = Ty->param_end(); I != E; ++I)
111 /*--.. Operations on struct types ..........................................--*/
113 LLVMTypeRef LLVMCreateStructType(LLVMTypeRef *ElementTypes,
114 unsigned ElementCount, int Packed) {
115 std::vector<const Type*> Tys;
116 for (LLVMTypeRef *I = ElementTypes,
117 *E = ElementTypes + ElementCount; I != E; ++I)
118 Tys.push_back(unwrap(*I));
120 return wrap(StructType::get(Tys, Packed != 0));
123 unsigned LLVMGetStructElementCount(LLVMTypeRef StructTy) {
124 return unwrap<StructType>(StructTy)->getNumElements();
127 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
128 StructType *Ty = unwrap<StructType>(StructTy);
129 for (FunctionType::param_iterator I = Ty->element_begin(),
130 E = Ty->element_end(); I != E; ++I)
134 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
135 return unwrap<StructType>(StructTy)->isPacked();
138 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
140 LLVMTypeRef LLVMCreateArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
141 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
144 LLVMTypeRef LLVMCreatePointerType(LLVMTypeRef ElementType) {
145 return wrap(PointerType::get(unwrap(ElementType)));
148 LLVMTypeRef LLVMCreateVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
149 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
152 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
153 return wrap(unwrap<SequentialType>(Ty)->getElementType());
156 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
157 return unwrap<ArrayType>(ArrayTy)->getNumElements();
160 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
161 return unwrap<VectorType>(VectorTy)->getNumElements();
164 /*--.. Operations on other types ...........................................--*/
166 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
167 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
169 LLVMTypeRef LLVMCreateOpaqueType() {
170 return wrap(llvm::OpaqueType::get());
174 /*===-- Operations on values ----------------------------------------------===*/
176 /*--.. Operations on all values ............................................--*/
178 LLVMTypeRef LLVMGetTypeOfValue(LLVMValueRef Val) {
179 return wrap(unwrap(Val)->getType());
182 const char *LLVMGetValueName(LLVMValueRef Val) {
183 return unwrap(Val)->getNameStart();
186 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
187 unwrap(Val)->setName(Name);
190 /*--.. Operations on constants of any type .................................--*/
192 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty) {
193 return wrap(Constant::getNullValue(unwrap(Ty)));
196 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty) {
197 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
200 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
201 return wrap(UndefValue::get(unwrap(Ty)));
204 int LLVMIsNull(LLVMValueRef Val) {
205 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
206 return C->isNullValue();
210 /*--.. Operations on scalar constants ......................................--*/
212 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
214 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
217 LLVMValueRef LLVMGetRealConstant(LLVMTypeRef RealTy, double N) {
218 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
221 /*--.. Operations on composite constants ...................................--*/
223 LLVMValueRef LLVMGetStringConstant(const char *Str, unsigned Length,
224 int DontNullTerminate) {
225 /* Inverted the sense of AddNull because ', 0)' is a
226 better mnemonic for null termination than ', 1)'. */
227 return wrap(ConstantArray::get(std::string(Str, Length),
228 DontNullTerminate == 0));
231 LLVMValueRef LLVMGetArrayConstant(LLVMTypeRef ElementTy,
232 LLVMValueRef *ConstantVals, unsigned Length) {
233 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
234 unwrap<Constant>(ConstantVals, Length),
238 LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count,
240 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
241 Count, Packed != 0));
244 LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals,
246 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
250 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
252 int LLVMIsDeclaration(LLVMValueRef Global) {
253 return unwrap<GlobalValue>(Global)->isDeclaration();
256 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
257 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
260 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
261 unwrap<GlobalValue>(Global)
262 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
265 const char *LLVMGetSection(LLVMValueRef Global) {
266 return unwrap<GlobalValue>(Global)->getSection().c_str();
269 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
270 unwrap<GlobalValue>(Global)->setSection(Section);
273 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
274 return static_cast<LLVMVisibility>(
275 unwrap<GlobalValue>(Global)->getVisibility());
278 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
279 unwrap<GlobalValue>(Global)
280 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
283 unsigned LLVMGetAlignment(LLVMValueRef Global) {
284 return unwrap<GlobalValue>(Global)->getAlignment();
287 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
288 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
291 /*--.. Operations on global variables ......................................--*/
293 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
294 return wrap(new GlobalVariable(unwrap(Ty), false,
295 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
298 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
299 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
302 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
303 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
306 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
307 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
310 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
311 unwrap<GlobalVariable>(GlobalVar)
312 ->setInitializer(unwrap<Constant>(ConstantVal));
315 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
316 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
319 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
320 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);