2020-01-28 10:18:45 +08:00
|
|
|
//===-- runtime/transformational.cpp --------------------------------------===//
|
2018-08-03 02:45:11 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-08-03 02:45:11 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-08-03 02:45:11 +08:00
|
|
|
|
2018-08-03 08:04:31 +08:00
|
|
|
#include "transformational.h"
|
2020-02-14 06:41:56 +08:00
|
|
|
#include "memory.h"
|
|
|
|
#include "terminator.h"
|
2018-08-03 02:45:11 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
namespace Fortran::runtime {
|
|
|
|
|
|
|
|
static inline std::int64_t GetInt64(const char *p, std::size_t bytes) {
|
|
|
|
switch (bytes) {
|
2020-03-29 12:00:16 +08:00
|
|
|
case 1:
|
|
|
|
return *reinterpret_cast<const std::int8_t *>(p);
|
|
|
|
case 2:
|
|
|
|
return *reinterpret_cast<const std::int16_t *>(p);
|
|
|
|
case 4:
|
|
|
|
return *reinterpret_cast<const std::int32_t *>(p);
|
|
|
|
case 8:
|
|
|
|
return *reinterpret_cast<const std::int64_t *>(p);
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
Terminator terminator{__FILE__, __LINE__};
|
|
|
|
terminator.Crash("no case for %dz bytes", bytes);
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// F2018 16.9.163
|
2020-02-14 06:41:56 +08:00
|
|
|
OwningPtr<Descriptor> RESHAPE(const Descriptor &source, const Descriptor &shape,
|
|
|
|
const Descriptor *pad, const Descriptor *order) {
|
2018-08-03 02:45:11 +08:00
|
|
|
// Compute and check the rank of the result.
|
2020-02-14 06:41:56 +08:00
|
|
|
Terminator terminator{__FILE__, __LINE__};
|
|
|
|
RUNTIME_CHECK(terminator, shape.rank() == 1);
|
|
|
|
RUNTIME_CHECK(terminator, shape.type().IsInteger());
|
2018-08-03 02:45:11 +08:00
|
|
|
SubscriptValue resultRank{shape.GetDimension(0).Extent()};
|
2020-02-14 06:41:56 +08:00
|
|
|
RUNTIME_CHECK(terminator,
|
|
|
|
resultRank >= 0 && resultRank <= static_cast<SubscriptValue>(maxRank));
|
2018-08-03 02:45:11 +08:00
|
|
|
|
|
|
|
// Extract and check the shape of the result; compute its element count.
|
2020-03-29 12:00:16 +08:00
|
|
|
SubscriptValue lowerBound[maxRank]; // all 1's
|
2018-08-03 02:45:11 +08:00
|
|
|
SubscriptValue resultExtent[maxRank];
|
|
|
|
std::size_t shapeElementBytes{shape.ElementBytes()};
|
|
|
|
std::size_t resultElements{1};
|
|
|
|
SubscriptValue shapeSubscript{shape.GetDimension(0).LowerBound()};
|
|
|
|
for (SubscriptValue j{0}; j < resultRank; ++j, ++shapeSubscript) {
|
2018-08-03 08:04:31 +08:00
|
|
|
lowerBound[j] = 1;
|
2018-08-03 02:45:11 +08:00
|
|
|
resultExtent[j] =
|
|
|
|
GetInt64(shape.Element<char>(&shapeSubscript), shapeElementBytes);
|
2020-02-14 06:41:56 +08:00
|
|
|
RUNTIME_CHECK(terminator, resultExtent[j] >= 0);
|
2018-08-03 02:45:11 +08:00
|
|
|
resultElements *= resultExtent[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there are sufficient elements in the SOURCE=, or that
|
|
|
|
// the optional PAD= argument is present and nonempty.
|
2018-08-03 08:04:31 +08:00
|
|
|
std::size_t elementBytes{source.ElementBytes()};
|
2018-08-03 02:45:11 +08:00
|
|
|
std::size_t sourceElements{source.Elements()};
|
|
|
|
std::size_t padElements{pad ? pad->Elements() : 0};
|
|
|
|
if (resultElements < sourceElements) {
|
2020-02-14 06:41:56 +08:00
|
|
|
RUNTIME_CHECK(terminator, padElements > 0);
|
|
|
|
RUNTIME_CHECK(terminator, pad->ElementBytes() == elementBytes);
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract and check the optional ORDER= argument, which must be a
|
|
|
|
// permutation of [1..resultRank].
|
|
|
|
int dimOrder[maxRank];
|
2019-11-10 01:29:31 +08:00
|
|
|
if (order) {
|
2020-02-14 06:41:56 +08:00
|
|
|
RUNTIME_CHECK(terminator, order->rank() == 1);
|
|
|
|
RUNTIME_CHECK(terminator, order->type().IsInteger());
|
|
|
|
RUNTIME_CHECK(terminator, order->GetDimension(0).Extent() == resultRank);
|
|
|
|
std::uint64_t values{0};
|
2018-08-03 02:45:11 +08:00
|
|
|
SubscriptValue orderSubscript{order->GetDimension(0).LowerBound()};
|
|
|
|
for (SubscriptValue j{0}; j < resultRank; ++j, ++orderSubscript) {
|
2020-02-14 06:41:56 +08:00
|
|
|
auto k{GetInt64(
|
|
|
|
order->OffsetElement<char>(orderSubscript), shapeElementBytes)};
|
|
|
|
RUNTIME_CHECK(
|
|
|
|
terminator, k >= 1 && k <= resultRank && !((values >> k) & 1));
|
|
|
|
values |= std::uint64_t{1} << k;
|
2018-08-03 02:45:11 +08:00
|
|
|
dimOrder[k - 1] = j;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int j{0}; j < resultRank; ++j) {
|
|
|
|
dimOrder[j] = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and populate the result's descriptor.
|
|
|
|
const DescriptorAddendum *sourceAddendum{source.Addendum()};
|
|
|
|
const DerivedType *sourceDerivedType{
|
|
|
|
sourceAddendum ? sourceAddendum->derivedType() : nullptr};
|
2020-02-14 06:41:56 +08:00
|
|
|
OwningPtr<Descriptor> result;
|
2019-11-10 01:29:31 +08:00
|
|
|
if (sourceDerivedType) {
|
2018-08-03 08:04:31 +08:00
|
|
|
result = Descriptor::Create(*sourceDerivedType, nullptr, resultRank,
|
|
|
|
resultExtent, CFI_attribute_allocatable);
|
2018-08-03 02:45:11 +08:00
|
|
|
} else {
|
2018-08-03 08:04:31 +08:00
|
|
|
result = Descriptor::Create(source.type(), elementBytes, nullptr,
|
|
|
|
resultRank, resultExtent,
|
2020-03-29 12:00:16 +08:00
|
|
|
CFI_attribute_allocatable); // TODO rearrange these arguments
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
DescriptorAddendum *resultAddendum{result->Addendum()};
|
2020-02-14 06:41:56 +08:00
|
|
|
RUNTIME_CHECK(terminator, resultAddendum);
|
2018-08-03 02:45:11 +08:00
|
|
|
resultAddendum->flags() |= DescriptorAddendum::DoNotFinalize;
|
2019-11-10 01:29:31 +08:00
|
|
|
if (sourceDerivedType) {
|
2018-08-03 02:45:11 +08:00
|
|
|
std::size_t lenParameters{sourceDerivedType->lenParameters()};
|
|
|
|
for (std::size_t j{0}; j < lenParameters; ++j) {
|
|
|
|
resultAddendum->SetLenParameterValue(
|
|
|
|
j, sourceAddendum->LenParameterValue(j));
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 08:04:31 +08:00
|
|
|
// Allocate storage for the result's data.
|
[flang] More Fortran runtime support for CHARACTER operations
Summary:
- Remove C++ library dependence from lock.h
- Implement LEN_TRIM, REPEAT, ADJUSTL, ADJUSTR, MAX/MIN
intrinsic functions for CHARACTER
Reviewers: tskeith, PeteSteinfeld, sscalpone, schweitz, DavidTruby
Reviewed By: PeteSteinfeld
Subscribers: llvm-commits, flang-commits
Tags: #flang, #llvm
Differential Revision: https://reviews.llvm.org/D82054
2020-06-18 04:17:24 +08:00
|
|
|
int status{result->Allocate(lowerBound, resultExtent)};
|
2018-08-03 08:04:31 +08:00
|
|
|
if (status != CFI_SUCCESS) {
|
2020-02-14 06:41:56 +08:00
|
|
|
terminator.Crash("RESHAPE: Allocate failed (error %d)", status);
|
2018-08-03 08:04:31 +08:00
|
|
|
}
|
2018-08-03 02:45:11 +08:00
|
|
|
|
|
|
|
// Populate the result's elements.
|
|
|
|
SubscriptValue resultSubscript[maxRank];
|
|
|
|
result->GetLowerBounds(resultSubscript);
|
|
|
|
SubscriptValue sourceSubscript[maxRank];
|
|
|
|
source.GetLowerBounds(sourceSubscript);
|
|
|
|
std::size_t resultElement{0};
|
|
|
|
std::size_t elementsFromSource{std::min(resultElements, sourceElements)};
|
|
|
|
for (; resultElement < elementsFromSource; ++resultElement) {
|
|
|
|
std::memcpy(result->Element<void>(resultSubscript),
|
|
|
|
source.Element<const void>(sourceSubscript), elementBytes);
|
|
|
|
source.IncrementSubscripts(sourceSubscript);
|
|
|
|
result->IncrementSubscripts(resultSubscript, dimOrder);
|
|
|
|
}
|
|
|
|
if (resultElement < resultElements) {
|
|
|
|
// Remaining elements come from the optional PAD= argument.
|
|
|
|
SubscriptValue padSubscript[maxRank];
|
|
|
|
pad->GetLowerBounds(padSubscript);
|
|
|
|
for (; resultElement < resultElements; ++resultElement) {
|
|
|
|
std::memcpy(result->Element<void>(resultSubscript),
|
|
|
|
pad->Element<const void>(padSubscript), elementBytes);
|
|
|
|
pad->IncrementSubscripts(padSubscript);
|
|
|
|
result->IncrementSubscripts(resultSubscript, dimOrder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime
|