1 //===-- runtime/misc-intrinsic.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 #include "flang/Runtime/misc-intrinsic.h" 10 #include "terminator.h" 11 #include "flang/Runtime/descriptor.h" 12 #include <algorithm> 13 #include <cstring> 14 #include <optional> 15 16 namespace Fortran::runtime { 17 18 static void TransferImpl(Descriptor &result, const Descriptor &source, 19 const Descriptor &mold, const char *sourceFile, int line, 20 std::optional<std::int64_t> resultExtent) { 21 int rank{resultExtent.has_value() ? 1 : 0}; 22 std::size_t elementBytes{mold.ElementBytes()}; 23 result.Establish(mold.type(), elementBytes, nullptr, rank, nullptr, 24 CFI_attribute_allocatable, mold.Addendum() != nullptr); 25 if (resultExtent) { 26 result.GetDimension(0).SetBounds(1, *resultExtent); 27 } 28 if (const DescriptorAddendum * addendum{mold.Addendum()}) { 29 *result.Addendum() = *addendum; 30 } 31 if (int stat{result.Allocate()}) { 32 Terminator{sourceFile, line}.Crash( 33 "TRANSFER: could not allocate memory for result; STAT=%d", stat); 34 } 35 char *to{result.OffsetElement<char>()}; 36 std::size_t resultBytes{result.Elements() * result.ElementBytes()}; 37 const std::size_t sourceElementBytes{source.ElementBytes()}; 38 std::size_t sourceElements{source.Elements()}; 39 SubscriptValue sourceAt[maxRank]; 40 source.GetLowerBounds(sourceAt); 41 while (resultBytes > 0 && sourceElements > 0) { 42 std::size_t toMove{std::min(resultBytes, sourceElementBytes)}; 43 std::memcpy(to, source.Element<char>(sourceAt), toMove); 44 to += toMove; 45 resultBytes -= toMove; 46 --sourceElements; 47 source.IncrementSubscripts(sourceAt); 48 } 49 if (resultBytes > 0) { 50 std::memset(to, 0, resultBytes); 51 } 52 } 53 54 extern "C" { 55 56 void RTNAME(Transfer)(Descriptor &result, const Descriptor &source, 57 const Descriptor &mold, const char *sourceFile, int line) { 58 std::optional<std::int64_t> elements; 59 if (mold.rank() > 0) { 60 if (std::size_t sourceElementBytes{ 61 source.Elements() * source.ElementBytes()}) { 62 if (std::size_t moldElementBytes{mold.ElementBytes()}) { 63 elements = static_cast<std::int64_t>( 64 (sourceElementBytes + moldElementBytes - 1) / moldElementBytes); 65 } else { 66 Terminator{sourceFile, line}.Crash("TRANSFER: zero-sized type of MOLD= " 67 "when SOURCE= is not zero-sized"); 68 } 69 } else { 70 elements = 0; 71 } 72 } 73 return TransferImpl( 74 result, source, mold, sourceFile, line, std::move(elements)); 75 } 76 77 void RTNAME(TransferSize)(Descriptor &result, const Descriptor &source, 78 const Descriptor &mold, const char *sourceFile, int line, 79 std::int64_t size) { 80 return TransferImpl(result, source, mold, sourceFile, line, size); 81 } 82 83 } // extern "C" 84 } // namespace Fortran::runtime 85