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/InitializePasses.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetLibraryInfo.h"
21 #include "llvm/LLVMContext.h"
22 #include <cstring>
23
24 using namespace llvm;
25
initializeTarget(PassRegistry & Registry)26 void llvm::initializeTarget(PassRegistry &Registry) {
27 initializeTargetDataPass(Registry);
28 initializeTargetLibraryInfoPass(Registry);
29 }
30
LLVMInitializeTarget(LLVMPassRegistryRef R)31 void LLVMInitializeTarget(LLVMPassRegistryRef R) {
32 initializeTarget(*unwrap(R));
33 }
34
LLVMCreateTargetData(const char * StringRep)35 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
36 return wrap(new TargetData(StringRep));
37 }
38
LLVMAddTargetData(LLVMTargetDataRef TD,LLVMPassManagerRef PM)39 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
40 unwrap(PM)->add(new TargetData(*unwrap(TD)));
41 }
42
LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,LLVMPassManagerRef PM)43 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
44 LLVMPassManagerRef PM) {
45 unwrap(PM)->add(new TargetLibraryInfo(*unwrap(TLI)));
46 }
47
LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD)48 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
49 std::string StringRep = unwrap(TD)->getStringRepresentation();
50 return strdup(StringRep.c_str());
51 }
52
LLVMByteOrder(LLVMTargetDataRef TD)53 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
54 return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
55 }
56
LLVMPointerSize(LLVMTargetDataRef TD)57 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
58 return unwrap(TD)->getPointerSize();
59 }
60
LLVMIntPtrType(LLVMTargetDataRef TD)61 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
62 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
63 }
64
LLVMSizeOfTypeInBits(LLVMTargetDataRef TD,LLVMTypeRef Ty)65 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
66 return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
67 }
68
LLVMStoreSizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)69 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
70 return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
71 }
72
LLVMABISizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)73 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
74 return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
75 }
76
LLVMABIAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)77 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
78 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
79 }
80
LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)81 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
82 return unwrap(TD)->getCallFrameTypeAlignment(unwrap(Ty));
83 }
84
LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)85 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
86 return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
87 }
88
LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,LLVMValueRef GlobalVar)89 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
90 LLVMValueRef GlobalVar) {
91 return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
92 }
93
LLVMElementAtOffset(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned long long Offset)94 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
95 unsigned long long Offset) {
96 StructType *STy = unwrap<StructType>(StructTy);
97 return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
98 }
99
LLVMOffsetOfElement(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned Element)100 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
101 unsigned Element) {
102 StructType *STy = unwrap<StructType>(StructTy);
103 return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
104 }
105
LLVMDisposeTargetData(LLVMTargetDataRef TD)106 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
107 delete unwrap(TD);
108 }
109