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