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