1 //===-- Target.cpp --------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the common infrastructure (including C bindings) for
10 // libLLVMTarget.a, which implements target information.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/Target.h"
15 #include "llvm/Analysis/TargetLibraryInfo.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/LegacyPassManager.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/InitializePasses.h"
26 // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.
27 extern "C" LLVMContextRef
LLVMGetGlobalContext(void);
29 inline TargetLibraryInfoImpl
*unwrap(LLVMTargetLibraryInfoRef P
) {
30 return reinterpret_cast<TargetLibraryInfoImpl
*>(P
);
33 inline LLVMTargetLibraryInfoRef
wrap(const TargetLibraryInfoImpl
*P
) {
34 TargetLibraryInfoImpl
*X
= const_cast<TargetLibraryInfoImpl
*>(P
);
35 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(X
);
38 void llvm::initializeTarget(PassRegistry
&Registry
) {
39 initializeTargetLibraryInfoWrapperPassPass(Registry
);
40 initializeTargetTransformInfoWrapperPassPass(Registry
);
43 LLVMTargetDataRef
LLVMGetModuleDataLayout(LLVMModuleRef M
) {
44 return wrap(&unwrap(M
)->getDataLayout());
47 void LLVMSetModuleDataLayout(LLVMModuleRef M
, LLVMTargetDataRef DL
) {
48 unwrap(M
)->setDataLayout(*unwrap(DL
));
51 LLVMTargetDataRef
LLVMCreateTargetData(const char *StringRep
) {
52 return wrap(new DataLayout(StringRep
));
55 void LLVMDisposeTargetData(LLVMTargetDataRef TD
) {
59 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI
,
60 LLVMPassManagerRef PM
) {
61 unwrap(PM
)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI
)));
64 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD
) {
65 std::string StringRep
= unwrap(TD
)->getStringRepresentation();
66 return strdup(StringRep
.c_str());
69 LLVMByteOrdering
LLVMByteOrder(LLVMTargetDataRef TD
) {
70 return unwrap(TD
)->isLittleEndian() ? LLVMLittleEndian
: LLVMBigEndian
;
73 unsigned LLVMPointerSize(LLVMTargetDataRef TD
) {
74 return unwrap(TD
)->getPointerSize(0);
77 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD
, unsigned AS
) {
78 return unwrap(TD
)->getPointerSize(AS
);
81 LLVMTypeRef
LLVMIntPtrType(LLVMTargetDataRef TD
) {
82 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));
85 LLVMTypeRef
LLVMIntPtrTypeForAS(LLVMTargetDataRef TD
, unsigned AS
) {
86 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS
));
89 LLVMTypeRef
LLVMIntPtrTypeInContext(LLVMContextRef C
, LLVMTargetDataRef TD
) {
90 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(C
)));
93 LLVMTypeRef
LLVMIntPtrTypeForASInContext(LLVMContextRef C
, LLVMTargetDataRef TD
, unsigned AS
) {
94 return wrap(unwrap(TD
)->getIntPtrType(*unwrap(C
), AS
));
97 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
98 return unwrap(TD
)->getTypeSizeInBits(unwrap(Ty
));
101 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
102 return unwrap(TD
)->getTypeStoreSize(unwrap(Ty
));
105 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
106 return unwrap(TD
)->getTypeAllocSize(unwrap(Ty
));
109 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
110 return unwrap(TD
)->getABITypeAlign(unwrap(Ty
)).value();
113 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
114 return unwrap(TD
)->getABITypeAlign(unwrap(Ty
)).value();
117 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD
, LLVMTypeRef Ty
) {
118 return unwrap(TD
)->getPrefTypeAlign(unwrap(Ty
)).value();
121 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD
,
122 LLVMValueRef GlobalVar
) {
124 ->getPreferredAlign(unwrap
<GlobalVariable
>(GlobalVar
))
128 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD
, LLVMTypeRef StructTy
,
129 unsigned long long Offset
) {
130 StructType
*STy
= unwrap
<StructType
>(StructTy
);
131 return unwrap(TD
)->getStructLayout(STy
)->getElementContainingOffset(Offset
);
134 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD
, LLVMTypeRef StructTy
,
136 StructType
*STy
= unwrap
<StructType
>(StructTy
);
137 return unwrap(TD
)->getStructLayout(STy
)->getElementOffset(Element
);