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