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