1 //===-- Target.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 common infrastructure (including C bindings) for
11 // libLLVMTarget.a, which implements target information.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Target.h"
16 #include "llvm-c/Initialization.h"
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/IR/Value.h"
22 #include "llvm/InitializePasses.h"
27 // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.
28 extern "C" LLVMContextRef
LLVMGetGlobalContext(void);
30 inline TargetLibraryInfoImpl
*unwrap(LLVMTargetLibraryInfoRef P
) {
31 return reinterpret_cast<TargetLibraryInfoImpl
*>(P
);
34 inline LLVMTargetLibraryInfoRef
wrap(const TargetLibraryInfoImpl
*P
) {
35 TargetLibraryInfoImpl
*X
= const_cast<TargetLibraryInfoImpl
*>(P
);
36 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(X
);
39 void llvm::initializeTarget(PassRegistry
&Registry
) {
40 initializeTargetLibraryInfoWrapperPassPass(Registry
);
41 initializeTargetTransformInfoWrapperPassPass(Registry
);
44 void LLVMInitializeTarget(LLVMPassRegistryRef R
) {
45 initializeTarget(*unwrap(R
));
48 LLVMTargetDataRef
LLVMGetModuleDataLayout(LLVMModuleRef M
) {
49 return wrap(&unwrap(M
)->getDataLayout());
52 void LLVMSetModuleDataLayout(LLVMModuleRef M
, LLVMTargetDataRef DL
) {
53 unwrap(M
)->setDataLayout(*unwrap(DL
));
56 LLVMTargetDataRef
LLVMCreateTargetData(const char *StringRep
) {
57 return wrap(new DataLayout(StringRep
));
60 void LLVMDisposeTargetData(LLVMTargetDataRef TD
) {
64 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI
,
65 LLVMPassManagerRef PM
) {
66 unwrap(PM
)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI
)));
69 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD
) {
70 std::string StringRep
= unwrap(TD
)->getStringRepresentation();
71 return strdup(StringRep
.c_str());
74 LLVMByteOrdering
LLVMByteOrder(LLVMTargetDataRef TD
) {
75 return unwrap(TD
)->isLittleEndian() ? LLVMLittleEndian
: LLVMBigEndian
;
78 unsigned LLVMPointerSize(LLVMTargetDataRef TD
) {
79 return unwrap(TD
)->getPointerSize(0);
82 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD
, unsigned AS
) {
83 return unwrap(TD
)->getPointerSize(AS
);
86 LLVMTypeRef
LLVMIntPtrType(LLVMTargetDataRef TD
) {
87 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));
90 LLVMTypeRef
LLVMIntPtrTypeForAS(LLVMTargetDataRef TD
, unsigned AS
) {
91 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS
));
94 LLVMTypeRef
LLVMIntPtrTypeInContext(LLVMContextRef C
, LLVMTargetDataRef TD
) {
95 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(C
)));
98 LLVMTypeRef
LLVMIntPtrTypeForASInContext(LLVMContextRef C
, LLVMTargetDataRef TD
, unsigned AS
) {
99 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(C
), AS
));
102 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
103 return unwrap(TD
)->getTypeSizeInBits(unwrap(Ty
));
106 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
107 return unwrap(TD
)->getTypeStoreSize(unwrap(Ty
));
110 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
111 return unwrap(TD
)->getTypeAllocSize(unwrap(Ty
));
114 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
115 return unwrap(TD
)->getABITypeAlignment(unwrap(Ty
));
118 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
119 return unwrap(TD
)->getABITypeAlignment(unwrap(Ty
));
122 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
123 return unwrap(TD
)->getPrefTypeAlignment(unwrap(Ty
));
126 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD
,
127 LLVMValueRef GlobalVar
) {
128 return unwrap(TD
)->getPreferredAlignment(unwrap
<GlobalVariable
>(GlobalVar
));
131 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD
, LLVMTypeRef StructTy
,
132 unsigned long long Offset
) {
133 StructType
*STy
= unwrap
<StructType
>(StructTy
);
134 return unwrap(TD
)->getStructLayout(STy
)->getElementContainingOffset(Offset
);
137 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD
, LLVMTypeRef StructTy
,
139 StructType
*STy
= unwrap
<StructType
>(StructTy
);
140 return unwrap(TD
)->getStructLayout(STy
)->getElementOffset(Element
);