2019-12-21 04:52:07 +08:00
|
|
|
//===-- runtime/descriptor.h ------------------------------------*- C++ -*-===//
|
2018-05-17 01:22:33 +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-05-17 01:22:33 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-17 01:22:33 +08:00
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_DESCRIPTOR_H_
|
|
|
|
#define FORTRAN_RUNTIME_DESCRIPTOR_H_
|
|
|
|
|
|
|
|
// Defines data structures used during execution of a Fortran program
|
2018-05-18 02:32:23 +08:00
|
|
|
// to implement nontrivial dummy arguments, pointers, allocatables,
|
|
|
|
// function results, and the special behaviors of instances of derived types.
|
2018-05-17 01:22:33 +08:00
|
|
|
// This header file includes and extends the published language
|
|
|
|
// interoperability header that is required by the Fortran 2018 standard
|
|
|
|
// as a subset of definitions suitable for exposure to user C/C++ code.
|
2018-05-18 02:32:23 +08:00
|
|
|
// User C code is welcome to depend on that ISO_Fortran_binding.h file,
|
|
|
|
// but should never reference this internal header.
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
#include "memory.h"
|
2018-08-01 07:46:30 +08:00
|
|
|
#include "type-code.h"
|
2020-01-28 04:57:59 +08:00
|
|
|
#include "flang/ISO_Fortran_binding.h"
|
2018-08-01 07:46:30 +08:00
|
|
|
#include <cassert>
|
2018-05-17 01:22:33 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstddef>
|
2020-02-14 06:41:56 +08:00
|
|
|
#include <cstdio>
|
2018-08-01 07:46:30 +08:00
|
|
|
#include <cstring>
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2020-12-08 06:46:24 +08:00
|
|
|
namespace Fortran::runtime::typeInfo {
|
|
|
|
using TypeParameterValue = std::int64_t;
|
|
|
|
class DerivedType;
|
|
|
|
} // namespace Fortran::runtime::typeInfo
|
|
|
|
|
2018-05-17 01:22:33 +08:00
|
|
|
namespace Fortran::runtime {
|
|
|
|
|
2018-07-27 07:07:50 +08:00
|
|
|
using SubscriptValue = ISO::CFI_index_t;
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2018-08-01 07:46:30 +08:00
|
|
|
static constexpr int maxRank{CFI_MAX_RANK};
|
|
|
|
|
2018-08-02 01:55:46 +08:00
|
|
|
// A C++ view of the sole interoperable standard descriptor (ISO::CFI_cdesc_t)
|
2018-06-19 02:03:43 +08:00
|
|
|
// and its type and per-dimension information.
|
2018-05-17 01:22:33 +08:00
|
|
|
|
|
|
|
class Dimension {
|
|
|
|
public:
|
2018-07-27 07:07:50 +08:00
|
|
|
SubscriptValue LowerBound() const { return raw_.lower_bound; }
|
|
|
|
SubscriptValue Extent() const { return raw_.extent; }
|
|
|
|
SubscriptValue UpperBound() const { return LowerBound() + Extent() - 1; }
|
|
|
|
SubscriptValue ByteStride() const { return raw_.sm; }
|
2018-05-17 01:31:35 +08:00
|
|
|
|
2020-11-11 07:13:02 +08:00
|
|
|
Dimension &SetBounds(SubscriptValue lower, SubscriptValue upper) {
|
|
|
|
raw_.lower_bound = lower;
|
|
|
|
raw_.extent = upper >= lower ? upper - lower + 1 : 0;
|
|
|
|
return *this;
|
|
|
|
}
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
Dimension &SetLowerBound(SubscriptValue lower) {
|
|
|
|
raw_.lower_bound = lower;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Dimension &SetUpperBound(SubscriptValue upper) {
|
|
|
|
auto lower{raw_.lower_bound};
|
|
|
|
raw_.extent = upper >= lower ? upper - lower + 1 : 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Dimension &SetExtent(SubscriptValue extent) {
|
|
|
|
raw_.extent = extent;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-11-11 07:13:02 +08:00
|
|
|
Dimension &SetByteStride(SubscriptValue bytes) {
|
|
|
|
raw_.sm = bytes;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-17 01:22:33 +08:00
|
|
|
private:
|
2018-05-18 02:32:23 +08:00
|
|
|
ISO::CFI_dim_t raw_;
|
2018-05-17 01:22:33 +08:00
|
|
|
};
|
|
|
|
|
2018-07-27 07:07:50 +08:00
|
|
|
// The storage for this object follows the last used dim[] entry in a
|
2018-08-01 07:46:30 +08:00
|
|
|
// Descriptor (CFI_cdesc_t) generic descriptor. Space matters here, since
|
|
|
|
// descriptors serve as POINTER and ALLOCATABLE components of derived type
|
|
|
|
// instances. The presence of this structure is implied by the flag
|
2019-08-29 00:14:46 +08:00
|
|
|
// CFI_cdesc_t.f18Addendum, and the number of elements in the len_[]
|
2020-12-08 06:46:24 +08:00
|
|
|
// array is determined by derivedType_->LenParameters().
|
2018-07-27 07:07:50 +08:00
|
|
|
class DescriptorAddendum {
|
2018-05-17 01:22:33 +08:00
|
|
|
public:
|
2018-08-02 01:55:46 +08:00
|
|
|
enum Flags {
|
|
|
|
StaticDescriptor = 0x001,
|
2020-03-29 12:00:16 +08:00
|
|
|
ImplicitAllocatable = 0x002, // compiler-created allocatable
|
|
|
|
DoNotFinalize = 0x004, // compiler temporary
|
|
|
|
Target = 0x008, // TARGET attribute
|
2018-08-02 01:55:46 +08:00
|
|
|
};
|
2018-07-27 07:07:50 +08:00
|
|
|
|
2018-08-03 02:45:11 +08:00
|
|
|
explicit DescriptorAddendum(
|
2020-12-08 06:46:24 +08:00
|
|
|
const typeInfo::DerivedType *dt = nullptr, std::uint64_t flags = 0)
|
2020-03-29 12:00:16 +08:00
|
|
|
: derivedType_{dt}, flags_{flags} {}
|
2021-04-03 00:30:31 +08:00
|
|
|
DescriptorAddendum &operator=(const DescriptorAddendum &);
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2020-12-08 06:46:24 +08:00
|
|
|
const typeInfo::DerivedType *derivedType() const { return derivedType_; }
|
|
|
|
DescriptorAddendum &set_derivedType(const typeInfo::DerivedType *dt) {
|
2018-08-03 02:45:11 +08:00
|
|
|
derivedType_ = dt;
|
2018-08-01 07:46:30 +08:00
|
|
|
return *this;
|
2018-07-27 07:07:50 +08:00
|
|
|
}
|
2018-08-02 01:55:46 +08:00
|
|
|
std::uint64_t &flags() { return flags_; }
|
|
|
|
const std::uint64_t &flags() const { return flags_; }
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2020-12-08 06:46:24 +08:00
|
|
|
std::size_t LenParameters() const;
|
2018-08-03 02:45:11 +08:00
|
|
|
|
2020-12-08 06:46:24 +08:00
|
|
|
typeInfo::TypeParameterValue LenParameterValue(int which) const {
|
|
|
|
return len_[which];
|
|
|
|
}
|
2018-07-27 07:07:50 +08:00
|
|
|
static constexpr std::size_t SizeInBytes(int lenParameters) {
|
2020-12-08 06:46:24 +08:00
|
|
|
return sizeof(DescriptorAddendum) - sizeof(typeInfo::TypeParameterValue) +
|
|
|
|
lenParameters * sizeof(typeInfo::TypeParameterValue);
|
2018-07-27 07:07:50 +08:00
|
|
|
}
|
|
|
|
std::size_t SizeInBytes() const;
|
|
|
|
|
2020-12-08 06:46:24 +08:00
|
|
|
void SetLenParameterValue(int which, typeInfo::TypeParameterValue x) {
|
2018-07-27 07:07:50 +08:00
|
|
|
len_[which] = x;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
void Dump(FILE * = stdout) const;
|
2018-08-03 08:04:31 +08:00
|
|
|
|
2018-07-27 07:07:50 +08:00
|
|
|
private:
|
2020-12-08 06:46:24 +08:00
|
|
|
const typeInfo::DerivedType *derivedType_;
|
2018-08-02 01:55:46 +08:00
|
|
|
std::uint64_t flags_{0};
|
2020-12-08 06:46:24 +08:00
|
|
|
typeInfo::TypeParameterValue len_[1]; // must be the last component
|
2018-07-27 07:07:50 +08:00
|
|
|
// The LEN type parameter values can also include captured values of
|
|
|
|
// specification expressions that were used for bounds and for LEN type
|
|
|
|
// parameters of components. The values have been truncated to the LEN
|
|
|
|
// type parameter's type, if shorter than 64 bits, then sign-extended.
|
|
|
|
};
|
|
|
|
|
2018-07-28 07:52:17 +08:00
|
|
|
// A C++ view of a standard descriptor object.
|
|
|
|
class Descriptor {
|
2018-07-27 07:07:50 +08:00
|
|
|
public:
|
2018-08-01 07:46:30 +08:00
|
|
|
// Be advised: this class type is not suitable for use when allocating
|
|
|
|
// a descriptor -- it is a dynamic view of the common descriptor format.
|
|
|
|
// If used in a simple declaration of a local variable or dynamic allocation,
|
2018-08-03 08:04:31 +08:00
|
|
|
// the size is going to be correct only by accident, since the true size of
|
|
|
|
// a descriptor depends on the number of its dimensions and the presence and
|
|
|
|
// size of an addendum, which depends on the type of the data.
|
2018-08-02 01:55:46 +08:00
|
|
|
// Use the class template StaticDescriptor (below) to declare a descriptor
|
|
|
|
// whose type and rank are fixed and known at compilation time. Use the
|
|
|
|
// Create() static member functions otherwise to dynamically allocate a
|
|
|
|
// descriptor.
|
2018-08-03 08:04:31 +08:00
|
|
|
|
|
|
|
Descriptor() {
|
|
|
|
// Minimal initialization to prevent the destructor from running amuck
|
|
|
|
// later if the descriptor is never established.
|
|
|
|
raw_.base_addr = nullptr;
|
|
|
|
raw_.f18Addendum = false;
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
Descriptor(const Descriptor &);
|
2018-08-01 07:46:30 +08:00
|
|
|
~Descriptor();
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
Descriptor &operator=(const Descriptor &);
|
2018-08-01 07:46:30 +08:00
|
|
|
|
[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
|
|
|
static constexpr std::size_t BytesFor(TypeCategory category, int kind) {
|
|
|
|
return category == TypeCategory::Complex ? kind * 2 : kind;
|
|
|
|
}
|
|
|
|
|
2018-08-03 02:45:11 +08:00
|
|
|
void Establish(TypeCode t, std::size_t elementBytes, void *p = nullptr,
|
2018-08-02 01:55:46 +08:00
|
|
|
int rank = maxRank, const SubscriptValue *extent = nullptr,
|
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other,
|
|
|
|
bool addendum = false);
|
2018-08-03 02:45:11 +08:00
|
|
|
void Establish(TypeCategory, int kind, void *p = nullptr, int rank = maxRank,
|
2018-08-02 01:55:46 +08:00
|
|
|
const SubscriptValue *extent = nullptr,
|
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other,
|
|
|
|
bool addendum = false);
|
[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
|
|
|
void Establish(int characterKind, std::size_t characters, void *p = nullptr,
|
|
|
|
int rank = maxRank, const SubscriptValue *extent = nullptr,
|
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other,
|
|
|
|
bool addendum = false);
|
2020-12-08 06:46:24 +08:00
|
|
|
void Establish(const typeInfo::DerivedType &dt, void *p = nullptr,
|
|
|
|
int rank = maxRank, const SubscriptValue *extent = nullptr,
|
2018-08-02 01:55:46 +08:00
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other);
|
2018-07-28 07:52:17 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
static OwningPtr<Descriptor> Create(TypeCode t, std::size_t elementBytes,
|
2018-08-03 08:04:31 +08:00
|
|
|
void *p = nullptr, int rank = maxRank,
|
|
|
|
const SubscriptValue *extent = nullptr,
|
[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
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other,
|
|
|
|
int derivedTypeLenParameters = 0);
|
2020-02-14 06:41:56 +08:00
|
|
|
static OwningPtr<Descriptor> Create(TypeCategory, int kind, void *p = nullptr,
|
|
|
|
int rank = maxRank, const SubscriptValue *extent = nullptr,
|
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other);
|
[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
|
|
|
static OwningPtr<Descriptor> Create(int characterKind,
|
|
|
|
SubscriptValue characters, void *p = nullptr, int rank = maxRank,
|
|
|
|
const SubscriptValue *extent = nullptr,
|
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other);
|
2020-12-08 06:46:24 +08:00
|
|
|
static OwningPtr<Descriptor> Create(const typeInfo::DerivedType &dt,
|
|
|
|
void *p = nullptr, int rank = maxRank,
|
|
|
|
const SubscriptValue *extent = nullptr,
|
2018-08-02 01:55:46 +08:00
|
|
|
ISO::CFI_attribute_t attribute = CFI_attribute_other);
|
2018-08-01 07:46:30 +08:00
|
|
|
|
2018-07-27 07:07:50 +08:00
|
|
|
ISO::CFI_cdesc_t &raw() { return raw_; }
|
|
|
|
const ISO::CFI_cdesc_t &raw() const { return raw_; }
|
2018-05-17 01:22:33 +08:00
|
|
|
std::size_t ElementBytes() const { return raw_.elem_len; }
|
|
|
|
int rank() const { return raw_.rank; }
|
|
|
|
TypeCode type() const { return TypeCode{raw_.type}; }
|
|
|
|
|
2018-07-28 07:52:17 +08:00
|
|
|
Descriptor &set_base_addr(void *p) {
|
2018-07-27 07:07:50 +08:00
|
|
|
raw_.base_addr = p;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:55:46 +08:00
|
|
|
bool IsPointer() const { return raw_.attribute == CFI_attribute_pointer; }
|
2018-05-17 01:22:33 +08:00
|
|
|
bool IsAllocatable() const {
|
2018-08-02 01:55:46 +08:00
|
|
|
return raw_.attribute == CFI_attribute_allocatable;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
2018-08-03 08:04:31 +08:00
|
|
|
bool IsAllocated() const { return raw_.base_addr != nullptr; }
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2018-05-18 02:32:23 +08:00
|
|
|
Dimension &GetDimension(int dim) {
|
|
|
|
return *reinterpret_cast<Dimension *>(&raw_.dim[dim]);
|
|
|
|
}
|
2018-05-17 01:22:33 +08:00
|
|
|
const Dimension &GetDimension(int dim) const {
|
|
|
|
return *reinterpret_cast<const Dimension *>(&raw_.dim[dim]);
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:07:50 +08:00
|
|
|
std::size_t SubscriptByteOffset(
|
|
|
|
int dim, SubscriptValue subscriptValue) const {
|
|
|
|
const Dimension &dimension{GetDimension(dim)};
|
|
|
|
return (subscriptValue - dimension.LowerBound()) * dimension.ByteStride();
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
std::size_t SubscriptsToByteOffset(const SubscriptValue subscript[]) const {
|
2018-08-03 02:45:11 +08:00
|
|
|
std::size_t offset{0};
|
|
|
|
for (int j{0}; j < raw_.rank; ++j) {
|
|
|
|
offset += SubscriptByteOffset(j, subscript[j]);
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
template <typename A = char> A *OffsetElement(std::size_t offset = 0) const {
|
2018-08-03 02:45:11 +08:00
|
|
|
return reinterpret_cast<A *>(
|
|
|
|
reinterpret_cast<char *>(raw_.base_addr) + offset);
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
template <typename A> A *Element(const SubscriptValue subscript[]) const {
|
2020-02-14 06:41:56 +08:00
|
|
|
return OffsetElement<A>(SubscriptsToByteOffset(subscript));
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> A *ZeroBasedIndexedElement(std::size_t n) const {
|
2020-02-14 06:41:56 +08:00
|
|
|
SubscriptValue at[maxRank];
|
|
|
|
if (SubscriptsForZeroBasedElementNumber(at, n)) {
|
|
|
|
return Element<A>(at);
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
return nullptr;
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 01:37:03 +08:00
|
|
|
int GetLowerBounds(SubscriptValue subscript[]) const {
|
2018-08-03 02:45:11 +08:00
|
|
|
for (int j{0}; j < raw_.rank; ++j) {
|
2020-02-14 06:41:56 +08:00
|
|
|
subscript[j] = GetDimension(j).LowerBound();
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
2021-05-21 01:37:03 +08:00
|
|
|
return raw_.rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetShape(SubscriptValue subscript[]) const {
|
|
|
|
for (int j{0}; j < raw_.rank; ++j) {
|
|
|
|
subscript[j] = GetDimension(j).Extent();
|
|
|
|
}
|
|
|
|
return raw_.rank;
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
// When the passed subscript vector contains the last (or first)
|
|
|
|
// subscripts of the array, these wrap the subscripts around to
|
|
|
|
// their first (or last) values and return false.
|
|
|
|
bool IncrementSubscripts(
|
[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
|
|
|
SubscriptValue[], const int *permutation = nullptr) const;
|
2020-02-14 06:41:56 +08:00
|
|
|
bool DecrementSubscripts(
|
[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
|
|
|
SubscriptValue[], const int *permutation = nullptr) const;
|
2020-02-14 06:41:56 +08:00
|
|
|
// False when out of range.
|
|
|
|
bool SubscriptsForZeroBasedElementNumber(SubscriptValue *,
|
|
|
|
std::size_t elementNumber, const int *permutation = nullptr) const;
|
|
|
|
std::size_t ZeroBasedElementNumber(
|
|
|
|
const SubscriptValue *, const int *permutation = nullptr) const;
|
|
|
|
|
2018-05-18 02:32:23 +08:00
|
|
|
DescriptorAddendum *Addendum() {
|
2018-08-02 01:55:46 +08:00
|
|
|
if (raw_.f18Addendum != 0) {
|
2018-05-18 02:32:23 +08:00
|
|
|
return reinterpret_cast<DescriptorAddendum *>(&GetDimension(rank()));
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const DescriptorAddendum *Addendum() const {
|
2018-08-02 01:55:46 +08:00
|
|
|
if (raw_.f18Addendum != 0) {
|
2018-05-17 01:31:35 +08:00
|
|
|
return reinterpret_cast<const DescriptorAddendum *>(
|
|
|
|
&GetDimension(rank()));
|
2018-05-17 01:22:33 +08:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 07:25:26 +08:00
|
|
|
// Returns size in bytes of the descriptor (not the data)
|
2018-07-27 07:07:50 +08:00
|
|
|
static constexpr std::size_t SizeInBytes(
|
2018-08-02 01:55:46 +08:00
|
|
|
int rank, bool addendum = false, int lengthTypeParameters = 0) {
|
2018-07-28 07:52:17 +08:00
|
|
|
std::size_t bytes{sizeof(Descriptor) - sizeof(Dimension)};
|
2018-07-27 07:07:50 +08:00
|
|
|
bytes += rank * sizeof(Dimension);
|
2018-08-02 01:55:46 +08:00
|
|
|
if (addendum || lengthTypeParameters > 0) {
|
2018-07-27 07:07:50 +08:00
|
|
|
bytes += DescriptorAddendum::SizeInBytes(lengthTypeParameters);
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
2018-08-03 02:45:11 +08:00
|
|
|
|
2018-05-17 01:22:33 +08:00
|
|
|
std::size_t SizeInBytes() const;
|
|
|
|
|
2018-08-03 02:45:11 +08:00
|
|
|
std::size_t Elements() const;
|
|
|
|
|
[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
|
|
|
// TODO: SOURCE= and MOLD=
|
2020-11-11 07:13:02 +08:00
|
|
|
int Allocate();
|
[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 Allocate(const SubscriptValue lb[], const SubscriptValue ub[]);
|
2018-08-03 08:04:31 +08:00
|
|
|
int Deallocate(bool finalize = true);
|
2020-12-08 06:46:24 +08:00
|
|
|
void Destroy(bool finalize = true) const;
|
2018-08-03 08:04:31 +08:00
|
|
|
|
|
|
|
bool IsContiguous(int leadingDimensions = maxRank) const {
|
|
|
|
auto bytes{static_cast<SubscriptValue>(ElementBytes())};
|
|
|
|
for (int j{0}; j < leadingDimensions && j < raw_.rank; ++j) {
|
|
|
|
const Dimension &dim{GetDimension(j)};
|
|
|
|
if (bytes != dim.ByteStride()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bytes *= dim.Extent();
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
2018-08-03 08:04:31 +08:00
|
|
|
return true;
|
2018-08-03 02:45:11 +08:00
|
|
|
}
|
|
|
|
|
2021-05-06 02:37:49 +08:00
|
|
|
// Establishes a pointer to a section or element.
|
|
|
|
bool EstablishPointerSection(const Descriptor &source,
|
|
|
|
const SubscriptValue *lower = nullptr,
|
|
|
|
const SubscriptValue *upper = nullptr,
|
|
|
|
const SubscriptValue *stride = nullptr);
|
2018-07-27 07:07:50 +08:00
|
|
|
|
2021-05-06 02:37:49 +08:00
|
|
|
void Check() const;
|
2018-07-27 07:07:50 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
void Dump(FILE * = stdout) const;
|
2018-08-03 08:04:31 +08:00
|
|
|
|
2018-05-17 01:22:33 +08:00
|
|
|
private:
|
2018-05-18 02:32:23 +08:00
|
|
|
ISO::CFI_cdesc_t raw_;
|
2018-05-17 01:22:33 +08:00
|
|
|
};
|
2018-07-28 07:52:17 +08:00
|
|
|
static_assert(sizeof(Descriptor) == sizeof(ISO::CFI_cdesc_t));
|
2018-05-17 01:22:33 +08:00
|
|
|
|
2018-08-01 07:46:30 +08:00
|
|
|
// Properly configured instances of StaticDescriptor will occupy the
|
2018-08-03 02:45:11 +08:00
|
|
|
// exact amount of storage required for the descriptor, its dimensional
|
|
|
|
// information, and possible addendum. To build such a static descriptor,
|
|
|
|
// declare an instance of StaticDescriptor<>, extract a reference to its
|
|
|
|
// descriptor via the descriptor() accessor, and then built a Descriptor
|
|
|
|
// therein via descriptor.Establish(), e.g.:
|
2018-08-02 01:55:46 +08:00
|
|
|
// StaticDescriptor<R,A,LP> statDesc;
|
2018-08-01 07:46:30 +08:00
|
|
|
// Descriptor &descriptor{statDesc.descriptor()};
|
|
|
|
// descriptor.Establish( ... );
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int MAX_RANK = maxRank, bool ADDENDUM = false, int MAX_LEN_PARMS = 0>
|
2018-07-28 07:52:17 +08:00
|
|
|
class alignas(Descriptor) StaticDescriptor {
|
2018-07-27 07:07:50 +08:00
|
|
|
public:
|
|
|
|
static constexpr int maxRank{MAX_RANK};
|
|
|
|
static constexpr int maxLengthTypeParameters{MAX_LEN_PARMS};
|
2018-08-02 01:55:46 +08:00
|
|
|
static constexpr bool hasAddendum{ADDENDUM || MAX_LEN_PARMS > 0};
|
2018-08-01 07:46:30 +08:00
|
|
|
static constexpr std::size_t byteSize{
|
|
|
|
Descriptor::SizeInBytes(maxRank, hasAddendum, maxLengthTypeParameters)};
|
2018-07-27 07:07:50 +08:00
|
|
|
|
2018-08-03 08:04:31 +08:00
|
|
|
StaticDescriptor() { new (storage_) Descriptor{}; }
|
|
|
|
|
|
|
|
~StaticDescriptor() { descriptor().~Descriptor(); }
|
|
|
|
|
2018-08-01 07:46:30 +08:00
|
|
|
Descriptor &descriptor() { return *reinterpret_cast<Descriptor *>(storage_); }
|
2018-07-28 07:52:17 +08:00
|
|
|
const Descriptor &descriptor() const {
|
2018-08-01 07:46:30 +08:00
|
|
|
return *reinterpret_cast<const Descriptor *>(storage_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check() {
|
|
|
|
assert(descriptor().rank() <= maxRank);
|
2018-08-03 02:45:11 +08:00
|
|
|
assert(descriptor().SizeInBytes() <= byteSize);
|
2018-08-01 07:46:30 +08:00
|
|
|
if (DescriptorAddendum * addendum{descriptor().Addendum()}) {
|
2018-08-03 02:45:11 +08:00
|
|
|
assert(hasAddendum);
|
2020-12-08 06:46:24 +08:00
|
|
|
assert(addendum->LenParameters() <= maxLengthTypeParameters);
|
2018-08-01 07:46:30 +08:00
|
|
|
} else {
|
|
|
|
assert(!hasAddendum);
|
|
|
|
assert(maxLengthTypeParameters == 0);
|
|
|
|
}
|
2018-08-03 02:45:11 +08:00
|
|
|
descriptor().Check();
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-07-27 07:07:50 +08:00
|
|
|
char storage_[byteSize];
|
2018-05-17 01:22:33 +08:00
|
|
|
};
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime
|
|
|
|
#endif // FORTRAN_RUNTIME_DESCRIPTOR_H_
|