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   if (mold.rank() > 0) {
59     std::size_t moldElementBytes{mold.ElementBytes()};
60     std::size_t elements{
61         (source.Elements() * source.ElementBytes() + moldElementBytes - 1) /
62         moldElementBytes};
63     return TransferImpl(result, source, mold, sourceFile, line,
64         static_cast<std::int64_t>(elements));
65   } else {
66     return TransferImpl(result, source, mold, sourceFile, line, {});
67   }
68 }
69 
70 void RTNAME(TransferSize)(Descriptor &result, const Descriptor &source,
71     const Descriptor &mold, const char *sourceFile, int line,
72     std::int64_t size) {
73   return TransferImpl(result, source, mold, sourceFile, line, size);
74 }
75 
76 } // extern "C"
77 } // namespace Fortran::runtime
78