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 LLVMIsConstant(LLVMValueRef Ty) {
205 return isa<Constant>(unwrap(Ty));
208 int LLVMIsNull(LLVMValueRef Val) {
209 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
210 return C->isNullValue();
214 int LLVMIsUndef(LLVMValueRef Val) {
215 return isa<UndefValue>(unwrap(Val));
218 /*--.. Operations on scalar constants ......................................--*/
220 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
222 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
225 LLVMValueRef LLVMGetRealConstant(LLVMTypeRef RealTy, double N) {
226 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
229 /*--.. Operations on composite constants ...................................--*/
231 LLVMValueRef LLVMGetStringConstant(const char *Str, unsigned Length,
232 int DontNullTerminate) {
233 /* Inverted the sense of AddNull because ', 0)' is a
234 better mnemonic for null termination than ', 1)'. */
235 return wrap(ConstantArray::get(std::string(Str, Length),
236 DontNullTerminate == 0));
239 LLVMValueRef LLVMGetArrayConstant(LLVMTypeRef ElementTy,
240 LLVMValueRef *ConstantVals, unsigned Length) {
241 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
242 unwrap<Constant>(ConstantVals, Length),
246 LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count,
248 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
249 Count, Packed != 0));
252 LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals,
254 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
258 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
260 int LLVMIsDeclaration(LLVMValueRef Global) {
261 return unwrap<GlobalValue>(Global)->isDeclaration();
264 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
265 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
268 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
269 unwrap<GlobalValue>(Global)
270 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
273 const char *LLVMGetSection(LLVMValueRef Global) {
274 return unwrap<GlobalValue>(Global)->getSection().c_str();
277 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
278 unwrap<GlobalValue>(Global)->setSection(Section);
281 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
282 return static_cast<LLVMVisibility>(
283 unwrap<GlobalValue>(Global)->getVisibility());
286 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
287 unwrap<GlobalValue>(Global)
288 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
291 unsigned LLVMGetAlignment(LLVMValueRef Global) {
292 return unwrap<GlobalValue>(Global)->getAlignment();
295 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
296 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
299 /*--.. Operations on global variables ......................................--*/
301 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
302 return wrap(new GlobalVariable(unwrap(Ty), false,
303 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
306 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
307 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
310 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
311 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
314 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
315 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
318 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
319 unwrap<GlobalVariable>(GlobalVar)
320 ->setInitializer(unwrap<Constant>(ConstantVal));
323 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
324 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
327 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
328 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);