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/InitializePasses.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Target/TargetLibraryInfo.h"
22 #include <cstring>
23
24 using namespace llvm;
25
initializeTarget(PassRegistry & Registry)26 void llvm::initializeTarget(PassRegistry &Registry) {
27 initializeDataLayoutPass(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 DataLayout(StringRep));
37 }
38
LLVMAddTargetData(LLVMTargetDataRef TD,LLVMPassManagerRef PM)39 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
40 unwrap(PM)->add(new DataLayout(*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(0);
59 }
60
LLVMPointerSizeForAS(LLVMTargetDataRef TD,unsigned AS)61 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
62 return unwrap(TD)->getPointerSize(AS);
63 }
64
LLVMIntPtrType(LLVMTargetDataRef TD)65 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
66 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
67 }
68
LLVMIntPtrTypeForAS(LLVMTargetDataRef TD,unsigned AS)69 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
70 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
71 }
72
LLVMSizeOfTypeInBits(LLVMTargetDataRef TD,LLVMTypeRef Ty)73 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
74 return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
75 }
76
LLVMStoreSizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)77 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
78 return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
79 }
80
LLVMABISizeOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)81 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
82 return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
83 }
84
LLVMABIAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)85 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
86 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
87 }
88
LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)89 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
90 return unwrap(TD)->getCallFrameTypeAlignment(unwrap(Ty));
91 }
92
LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD,LLVMTypeRef Ty)93 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
94 return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
95 }
96
LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,LLVMValueRef GlobalVar)97 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
98 LLVMValueRef GlobalVar) {
99 return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
100 }
101
LLVMElementAtOffset(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned long long Offset)102 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
103 unsigned long long Offset) {
104 StructType *STy = unwrap<StructType>(StructTy);
105 return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
106 }
107
LLVMOffsetOfElement(LLVMTargetDataRef TD,LLVMTypeRef StructTy,unsigned Element)108 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
109 unsigned Element) {
110 StructType *STy = unwrap<StructType>(StructTy);
111 return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
112 }
113
LLVMDisposeTargetData(LLVMTargetDataRef TD)114 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
115 delete unwrap(TD);
116 }
117