1 //===-- Target.cpp --------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including C bindings) for
11 // libLLVMTarget.a, which implements target information.
13 //===----------------------------------------------------------------------===//
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"
27 inline DataLayout *unwrap(LLVMTargetDataRef P) {
28 return reinterpret_cast<DataLayout*>(P);
31 inline LLVMTargetDataRef wrap(const DataLayout *P) {
32 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
35 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
36 return reinterpret_cast<TargetLibraryInfo*>(P);
39 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
40 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
41 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
44 void llvm::initializeTarget(PassRegistry &Registry) {
45 initializeDataLayoutPass(Registry);
46 initializeTargetLibraryInfoPass(Registry);
49 void LLVMInitializeTarget(LLVMPassRegistryRef R) {
50 initializeTarget(*unwrap(R));
53 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
54 return wrap(new DataLayout(StringRep));
57 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
58 unwrap(PM)->add(new DataLayout(*unwrap(TD)));
61 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
62 LLVMPassManagerRef PM) {
63 unwrap(PM)->add(new TargetLibraryInfo(*unwrap(TLI)));
66 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
67 std::string StringRep = unwrap(TD)->getStringRepresentation();
68 return strdup(StringRep.c_str());
71 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
72 return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
75 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
76 return unwrap(TD)->getPointerSize(0);
79 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
80 return unwrap(TD)->getPointerSize(AS);
83 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
84 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
87 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
88 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
91 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
92 return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
95 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
96 return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
99 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
100 return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
103 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
104 return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
107 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
108 return unwrap(TD)->getCallFrameTypeAlignment(unwrap(Ty));
111 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
112 return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
115 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
116 LLVMValueRef GlobalVar) {
117 return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
120 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
121 unsigned long long Offset) {
122 StructType *STy = unwrap<StructType>(StructTy);
123 return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
126 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
128 StructType *STy = unwrap<StructType>(StructTy);
129 return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
132 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {