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