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

source code of llvm/lib/Target/Target.cpp