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/PassManager.h"
22 #include "llvm/Target/TargetLibraryInfo.h"
23 #include <cstring>
24
25 using namespace llvm;
26
unwrap(LLVMTargetLibraryInfoRef P)27 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
28 return reinterpret_cast<TargetLibraryInfo*>(P);
29 }
30
wrap(const TargetLibraryInfo * P)31 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
32 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
33 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
34 }
35
initializeTarget(PassRegistry & Registry)36 void llvm::initializeTarget(PassRegistry &Registry) {
37 initializeDataLayoutPassPass(Registry);
38 initializeTargetLibraryInfoPass(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 // The DataLayoutPass must now be in sync with the module. Unfortunatelly we
51 // cannot enforce that from the C api.
52 unwrap(PM)->add(new DataLayoutPass(*unwrap(TD)));
53 }
54
LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,LLVMPassManagerRef PM)55 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
56 LLVMPassManagerRef PM) {
57 unwrap(PM)->add(new TargetLibraryInfo(*unwrap(TLI)));
58 }
59
LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD)60 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
61 std::string StringRep = unwrap(TD)->getStringRepresentation();
62 return strdup(StringRep.c_str());
63 }
64
LLVMByteOrder(LLVMTargetDataRef TD)65 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
66 return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
67 }
68
LLVMPointerSize(LLVMTargetDataRef TD)69 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
70 return unwrap(TD)->getPointerSize(0);
71 }
72
LLVMPointerSizeForAS(LLVMTargetDataRef TD,unsigned AS)73 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
74 return unwrap(TD)->getPointerSize(AS);
75 }
76
LLVMIntPtrType(LLVMTargetDataRef TD)77 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
78 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
79 }
80
LLVMIntPtrTypeForAS(LLVMTargetDataRef TD,unsigned AS)81 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
82 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
83 }
84
LLVMIntPtrTypeInContext(LLVMContextRef C,LLVMTargetDataRef TD)85 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
86 return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));
87 }
88
LLVMIntPtrTypeForASInContext(LLVMContextRef C,LLVMTargetDataRef TD,unsigned AS)89 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {
90 return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));
91 }
92
LLVMSizeOfTypeInBits(LLVMTargetDataRef TD,LLVMTypeRef Ty)93 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
94 return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
95 }
96
LLVMStoreSizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)97 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
98 return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
99 }
100
LLVMABISizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)101 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
102 return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
103 }
104
LLVMABIAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)105 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
106 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
107 }
108
LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)109 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
110 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
111 }
112
LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)113 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
114 return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
115 }
116
LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,LLVMValueRef GlobalVar)117 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
118 LLVMValueRef GlobalVar) {
119 return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
120 }
121
LLVMElementAtOffset(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned long long Offset)122 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
123 unsigned long long Offset) {
124 StructType *STy = unwrap<StructType>(StructTy);
125 return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
126 }
127
LLVMOffsetOfElement(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned Element)128 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
129 unsigned Element) {
130 StructType *STy = unwrap<StructType>(StructTy);
131 return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
132 }
133
LLVMDisposeTargetData(LLVMTargetDataRef TD)134 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
135 delete unwrap(TD);
136 }
137