1 //===-- Target.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the common infrastructure (including C bindings) for 10 // libLLVMTarget.a, which implements target information. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm-c/Target.h" 15 #include "llvm/Analysis/TargetLibraryInfo.h" 16 #include "llvm/IR/DataLayout.h" 17 #include "llvm/IR/LLVMContext.h" 18 #include "llvm/IR/LegacyPassManager.h" 19 #include "llvm/IR/Value.h" 20 #include "llvm/InitializePasses.h" 21 #include <cstring> 22 23 using namespace llvm; 24 25 // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead. 26 extern "C" LLVMContextRef LLVMGetGlobalContext(void); 27 28 inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) { 29 return reinterpret_cast<TargetLibraryInfoImpl*>(P); 30 } 31 32 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) { 33 TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P); 34 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X); 35 } 36 37 void llvm::initializeTarget(PassRegistry &Registry) { 38 initializeTargetLibraryInfoWrapperPassPass(Registry); 39 initializeTargetTransformInfoWrapperPassPass(Registry); 40 } 41 42 LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) { 43 return wrap(&unwrap(M)->getDataLayout()); 44 } 45 46 void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) { 47 unwrap(M)->setDataLayout(*unwrap(DL)); 48 } 49 50 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) { 51 return wrap(new DataLayout(StringRep)); 52 } 53 54 void LLVMDisposeTargetData(LLVMTargetDataRef TD) { 55 delete unwrap(TD); 56 } 57 58 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI, 59 LLVMPassManagerRef PM) { 60 unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI))); 61 } 62 63 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) { 64 std::string StringRep = unwrap(TD)->getStringRepresentation(); 65 return strdup(StringRep.c_str()); 66 } 67 68 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) { 69 return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian; 70 } 71 72 unsigned LLVMPointerSize(LLVMTargetDataRef TD) { 73 return unwrap(TD)->getPointerSize(0); 74 } 75 76 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) { 77 return unwrap(TD)->getPointerSize(AS); 78 } 79 80 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) { 81 return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()))); 82 } 83 84 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) { 85 return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS)); 86 } 87 88 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) { 89 return wrap(unwrap(TD)->getIntPtrType(*unwrap(C))); 90 } 91 92 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) { 93 return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS)); 94 } 95 96 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) { 97 return unwrap(TD)->getTypeSizeInBits(unwrap(Ty)); 98 } 99 100 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { 101 return unwrap(TD)->getTypeStoreSize(unwrap(Ty)); 102 } 103 104 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { 105 return unwrap(TD)->getTypeAllocSize(unwrap(Ty)); 106 } 107 108 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { 109 return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value(); 110 } 111 112 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { 113 return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value(); 114 } 115 116 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { 117 return unwrap(TD)->getPrefTypeAlign(unwrap(Ty)).value(); 118 } 119 120 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD, 121 LLVMValueRef GlobalVar) { 122 return unwrap(TD) 123 ->getPreferredAlign(unwrap<GlobalVariable>(GlobalVar)) 124 .value(); 125 } 126 127 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy, 128 unsigned long long Offset) { 129 StructType *STy = unwrap<StructType>(StructTy); 130 return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset); 131 } 132 133 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy, 134 unsigned Element) { 135 StructType *STy = unwrap<StructType>(StructTy); 136 return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element); 137 } 138