| 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/Module.h" |
| 20 | #include "llvm/IR/Value.h" |
| 21 | #include "llvm/InitializePasses.h" |
| 22 | #include <cstring> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead. |
| 27 | extern "C" LLVMContextRef LLVMGetGlobalContext(void); |
| 28 | |
| 29 | inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) { |
| 30 | return reinterpret_cast<TargetLibraryInfoImpl*>(P); |
| 31 | } |
| 32 | |
| 33 | inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) { |
| 34 | TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P); |
| 35 | return reinterpret_cast<LLVMTargetLibraryInfoRef>(X); |
| 36 | } |
| 37 | |
| 38 | void llvm::initializeTarget(PassRegistry &Registry) { |
| 39 | initializeTargetLibraryInfoWrapperPassPass(Registry); |
| 40 | initializeTargetTransformInfoWrapperPassPass(Registry); |
| 41 | } |
| 42 | |
| 43 | LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) { |
| 44 | return wrap(P: &unwrap(P: M)->getDataLayout()); |
| 45 | } |
| 46 | |
| 47 | void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) { |
| 48 | unwrap(P: M)->setDataLayout(*unwrap(P: DL)); |
| 49 | } |
| 50 | |
| 51 | LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) { |
| 52 | return wrap(P: new DataLayout(StringRep)); |
| 53 | } |
| 54 | |
| 55 | void LLVMDisposeTargetData(LLVMTargetDataRef TD) { |
| 56 | delete unwrap(P: TD); |
| 57 | } |
| 58 | |
| 59 | void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI, |
| 60 | LLVMPassManagerRef PM) { |
| 61 | unwrap(P: PM)->add(P: new TargetLibraryInfoWrapperPass(*unwrap(P: TLI))); |
| 62 | } |
| 63 | |
| 64 | char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) { |
| 65 | std::string StringRep = unwrap(P: TD)->getStringRepresentation(); |
| 66 | return strdup(s: StringRep.c_str()); |
| 67 | } |
| 68 | |
| 69 | LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) { |
| 70 | return unwrap(P: TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian; |
| 71 | } |
| 72 | |
| 73 | unsigned LLVMPointerSize(LLVMTargetDataRef TD) { |
| 74 | return unwrap(P: TD)->getPointerSize(AS: 0); |
| 75 | } |
| 76 | |
| 77 | unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) { |
| 78 | return unwrap(P: TD)->getPointerSize(AS); |
| 79 | } |
| 80 | |
| 81 | LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) { |
| 82 | return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: LLVMGetGlobalContext()))); |
| 83 | } |
| 84 | |
| 85 | LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) { |
| 86 | return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: LLVMGetGlobalContext()), AddressSpace: AS)); |
| 87 | } |
| 88 | |
| 89 | LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) { |
| 90 | return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: C))); |
| 91 | } |
| 92 | |
| 93 | LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) { |
| 94 | return wrap(P: unwrap(P: TD)->getIntPtrType(C&: *unwrap(P: C), AddressSpace: AS)); |
| 95 | } |
| 96 | |
| 97 | unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) { |
| 98 | return unwrap(P: TD)->getTypeSizeInBits(Ty: unwrap(P: Ty)); |
| 99 | } |
| 100 | |
| 101 | unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { |
| 102 | return unwrap(P: TD)->getTypeStoreSize(Ty: unwrap(P: Ty)); |
| 103 | } |
| 104 | |
| 105 | unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { |
| 106 | return unwrap(P: TD)->getTypeAllocSize(Ty: unwrap(P: Ty)); |
| 107 | } |
| 108 | |
| 109 | unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { |
| 110 | return unwrap(P: TD)->getABITypeAlign(Ty: unwrap(P: Ty)).value(); |
| 111 | } |
| 112 | |
| 113 | unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { |
| 114 | return unwrap(P: TD)->getABITypeAlign(Ty: unwrap(P: Ty)).value(); |
| 115 | } |
| 116 | |
| 117 | unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) { |
| 118 | return unwrap(P: TD)->getPrefTypeAlign(Ty: unwrap(P: Ty)).value(); |
| 119 | } |
| 120 | |
| 121 | unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD, |
| 122 | LLVMValueRef GlobalVar) { |
| 123 | return unwrap(P: TD) |
| 124 | ->getPreferredAlign(GV: unwrap<GlobalVariable>(P: GlobalVar)) |
| 125 | .value(); |
| 126 | } |
| 127 | |
| 128 | unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy, |
| 129 | unsigned long long Offset) { |
| 130 | StructType *STy = unwrap<StructType>(P: StructTy); |
| 131 | return unwrap(P: TD)->getStructLayout(Ty: STy)->getElementContainingOffset(FixedOffset: Offset); |
| 132 | } |
| 133 | |
| 134 | unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy, |
| 135 | unsigned Element) { |
| 136 | StructType *STy = unwrap<StructType>(P: StructTy); |
| 137 | return unwrap(P: TD)->getStructLayout(Ty: STy)->getElementOffset(Idx: Element); |
| 138 | } |
| 139 | |