1 //===-- Target.cpp --------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including C bindings) for
10 // libLLVMTarget.a, which implements target information.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-c/Target.h"
15 #include "llvm-c/Initialization.h"
16 #include "llvm/Analysis/TargetLibraryInfo.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/InitializePasses.h"
22 #include <cstring>
23
24 using namespace llvm;
25
26 // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.
27 extern "C" LLVMContextRef LLVMGetGlobalContext(void);
28
unwrap(LLVMTargetLibraryInfoRef P)29 inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {
30 return reinterpret_cast<TargetLibraryInfoImpl*>(P);
31 }
32
wrap(const TargetLibraryInfoImpl * P)33 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) {
34 TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P);
35 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
36 }
37
initializeTarget(PassRegistry & Registry)38 void llvm::initializeTarget(PassRegistry &Registry) {
39 initializeTargetLibraryInfoWrapperPassPass(Registry);
40 initializeTargetTransformInfoWrapperPassPass(Registry);
41 }
42
LLVMInitializeTarget(LLVMPassRegistryRef R)43 void LLVMInitializeTarget(LLVMPassRegistryRef R) {
44 initializeTarget(*unwrap(R));
45 }
46
LLVMGetModuleDataLayout(LLVMModuleRef M)47 LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) {
48 return wrap(&unwrap(M)->getDataLayout());
49 }
50
LLVMSetModuleDataLayout(LLVMModuleRef M,LLVMTargetDataRef DL)51 void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) {
52 unwrap(M)->setDataLayout(*unwrap(DL));
53 }
54
LLVMCreateTargetData(const char * StringRep)55 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
56 return wrap(new DataLayout(StringRep));
57 }
58
LLVMDisposeTargetData(LLVMTargetDataRef TD)59 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
60 delete unwrap(TD);
61 }
62
LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,LLVMPassManagerRef PM)63 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
64 LLVMPassManagerRef PM) {
65 unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI)));
66 }
67
LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD)68 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
69 std::string StringRep = unwrap(TD)->getStringRepresentation();
70 return strdup(StringRep.c_str());
71 }
72
LLVMByteOrder(LLVMTargetDataRef TD)73 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
74 return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
75 }
76
LLVMPointerSize(LLVMTargetDataRef TD)77 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
78 return unwrap(TD)->getPointerSize(0);
79 }
80
LLVMPointerSizeForAS(LLVMTargetDataRef TD,unsigned AS)81 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
82 return unwrap(TD)->getPointerSize(AS);
83 }
84
LLVMIntPtrType(LLVMTargetDataRef TD)85 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
86 return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));
87 }
88
LLVMIntPtrTypeForAS(LLVMTargetDataRef TD,unsigned AS)89 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
90 return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS));
91 }
92
LLVMIntPtrTypeInContext(LLVMContextRef C,LLVMTargetDataRef TD)93 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
94 return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));
95 }
96
LLVMIntPtrTypeForASInContext(LLVMContextRef C,LLVMTargetDataRef TD,unsigned AS)97 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {
98 return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));
99 }
100
LLVMSizeOfTypeInBits(LLVMTargetDataRef TD,LLVMTypeRef Ty)101 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
102 return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
103 }
104
LLVMStoreSizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)105 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
106 return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
107 }
108
LLVMABISizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)109 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
110 return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
111 }
112
LLVMABIAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)113 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
114 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
115 }
116
LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)117 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
118 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
119 }
120
LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)121 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
122 return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
123 }
124
LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,LLVMValueRef GlobalVar)125 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
126 LLVMValueRef GlobalVar) {
127 return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
128 }
129
LLVMElementAtOffset(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned long long Offset)130 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
131 unsigned long long Offset) {
132 StructType *STy = unwrap<StructType>(StructTy);
133 return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
134 }
135
LLVMOffsetOfElement(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned Element)136 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
137 unsigned Element) {
138 StructType *STy = unwrap<StructType>(StructTy);
139 return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
140 }
141