[flang] Placeholder for standard module iso_fortran_env

Original-commit: flang-compiler/f18@386ebb0490
Reviewed-on: https://github.com/flang-compiler/f18/pull/485
Tree-same-pre-rewrite: false
This commit is contained in:
peter klausler 2019-06-06 13:42:33 -07:00
parent 433b9e110e
commit d8f72a3105
12 changed files with 128 additions and 55 deletions

View File

@ -29,12 +29,6 @@ IntrinsicTypeDefaultKinds &IntrinsicTypeDefaultKinds::set_defaultIntegerKind(
return *this;
}
IntrinsicTypeDefaultKinds &IntrinsicTypeDefaultKinds::set_subscriptIntegerKind(
int k) {
subscriptIntegerKind_ = k;
return *this;
}
IntrinsicTypeDefaultKinds &IntrinsicTypeDefaultKinds::set_defaultRealKind(
int k) {
defaultRealKind_ = k;

View File

@ -16,21 +16,25 @@
#define FORTRAN_COMMON_DEFAULT_KINDS_H_
#include "Fortran.h"
#include <cstdint>
// Represent the default values of the kind parameters of the
// various intrinsic types. These can be configured by means of
// the compiler command line.
// various intrinsic types. Most of these can be configured by
// means of the compiler command line; subscriptIntegerKind,
// however, is fixed at 8 because all address calculations are
// 64-bit safe.
namespace Fortran::common {
using SubscriptCIntType = std::int64_t;
class IntrinsicTypeDefaultKinds {
public:
IntrinsicTypeDefaultKinds();
int subscriptIntegerKind() const { return subscriptIntegerKind_; }
static constexpr int subscriptIntegerKind() { return 8; }
int doublePrecisionKind() const { return doublePrecisionKind_; }
int quadPrecisionKind() const { return quadPrecisionKind_; }
IntrinsicTypeDefaultKinds &set_defaultIntegerKind(int);
IntrinsicTypeDefaultKinds &set_subscriptIntegerKind(int);
IntrinsicTypeDefaultKinds &set_defaultRealKind(int);
IntrinsicTypeDefaultKinds &set_doublePrecisionKind(int);
IntrinsicTypeDefaultKinds &set_quadPrecisionKind(int);
@ -46,7 +50,6 @@ private:
// storage unit, so their kinds are also forced. Default COMPLEX must always
// comprise two default REAL components.
int defaultIntegerKind_{4};
int subscriptIntegerKind_{8}; // for large arrays
int defaultRealKind_{defaultIntegerKind_};
int doublePrecisionKind_{2 * defaultRealKind_};
int quadPrecisionKind_{2 * doublePrecisionKind_};

View File

@ -1,4 +1,4 @@
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -35,14 +35,14 @@ void RealFlagWarnings(
}
}
std::int64_t &FoldingContext::StartImpliedDo(
parser::CharBlock name, std::int64_t n) {
common::SubscriptCIntType &FoldingContext::StartImpliedDo(
parser::CharBlock name, common::SubscriptCIntType n) {
auto pair{impliedDos_.insert(std::make_pair(name, n))};
CHECK(pair.second);
return pair.first->second;
}
std::optional<std::int64_t> FoldingContext::GetImpliedDo(
std::optional<common::SubscriptCIntType> FoldingContext::GetImpliedDo(
parser::CharBlock name) const {
if (auto iter{impliedDos_.find(name)}; iter != impliedDos_.cend()) {
return {iter->second};

View File

@ -17,6 +17,7 @@
#include "intrinsics-library.h"
#include "../common/Fortran.h"
#include "../common/default-kinds.h"
#include "../common/enum-set.h"
#include "../common/idioms.h"
#include "../common/indirection.h"
@ -228,11 +229,13 @@ public:
return hostIntrinsicsLibrary_;
}
std::int64_t &StartImpliedDo(parser::CharBlock, std::int64_t = 1);
std::optional<std::int64_t> GetImpliedDo(parser::CharBlock) const;
common::SubscriptCIntType &StartImpliedDo(
parser::CharBlock, common::SubscriptCIntType = 1);
std::optional<common::SubscriptCIntType> GetImpliedDo(
parser::CharBlock) const;
void EndImpliedDo(parser::CharBlock);
std::map<parser::CharBlock, std::int64_t> &impliedDos() {
std::map<parser::CharBlock, common::SubscriptCIntType> &impliedDos() {
return impliedDos_;
}
@ -247,7 +250,7 @@ private:
bool flushSubnormalsToZero_{false};
bool bigEndian_{false};
const semantics::DerivedTypeSpec *pdtInstance_{nullptr};
std::map<parser::CharBlock, std::int64_t> impliedDos_;
std::map<parser::CharBlock, common::SubscriptCIntType> impliedDos_;
HostIntrinsicProceduresLibrary hostIntrinsicsLibrary_;
};

View File

@ -109,23 +109,23 @@ auto Constant<T>::Reshape(ConstantSubscripts &&dims) const -> Constant {
template<int KIND>
Constant<Type<TypeCategory::Character, KIND>>::Constant(
const Scalar<Result> &str)
: values_{str}, length_{static_cast<std::int64_t>(values_.size())} {}
: values_{str}, length_{static_cast<LengthCIntType>(values_.size())} {}
template<int KIND>
Constant<Type<TypeCategory::Character, KIND>>::Constant(Scalar<Result> &&str)
: values_{std::move(str)}, length_{
static_cast<std::int64_t>(values_.size())} {}
static_cast<LengthCIntType>(values_.size())} {}
template<int KIND>
Constant<Type<TypeCategory::Character, KIND>>::Constant(std::int64_t len,
Constant<Type<TypeCategory::Character, KIND>>::Constant(LengthCIntType len,
std::vector<Scalar<Result>> &&strings, ConstantSubscripts &&dims)
: length_{len}, shape_{std::move(dims)} {
CHECK(strings.size() == TotalElementCount(shape_));
values_.assign(strings.size() * length_,
static_cast<typename Scalar<Result>::value_type>(' '));
std::int64_t at{0};
LengthCIntType at{0};
for (const auto &str : strings) {
auto strLen{static_cast<std::int64_t>(str.size())};
auto strLen{static_cast<LengthCIntType>(str.size())};
if (strLen > length_) {
values_.replace(at, length_, str.substr(0, length_));
} else {
@ -133,7 +133,7 @@ Constant<Type<TypeCategory::Character, KIND>>::Constant(std::int64_t len,
}
at += length_;
}
CHECK(at == static_cast<std::int64_t>(values_.size()));
CHECK(at == static_cast<LengthCIntType>(values_.size()));
}
template<int KIND> Constant<Type<TypeCategory::Character, KIND>>::~Constant() {}
@ -148,7 +148,7 @@ std::size_t Constant<Type<TypeCategory::Character, KIND>>::size() const {
if (length_ == 0) {
return TotalElementCount(shape_);
} else {
return static_cast<std::int64_t>(values_.size()) / length_;
return static_cast<LengthCIntType>(values_.size()) / length_;
}
}
@ -165,7 +165,7 @@ auto Constant<Type<TypeCategory::Character, KIND>>::Reshape(
std::size_t n{TotalElementCount(dims)};
CHECK(!empty() || n == 0);
std::vector<Element> elements;
std::int64_t at{0}, limit{static_cast<std::int64_t>(values_.size())};
LengthCIntType at{0}, limit{static_cast<LengthCIntType>(values_.size())};
while (n-- > 0) {
elements.push_back(values_.substr(at, length_));
at += length_;

View File

@ -17,6 +17,7 @@
#include "formatting.h"
#include "type.h"
#include "../common/default-kinds.h"
#include <map>
#include <ostream>
#include <vector>
@ -35,12 +36,14 @@ template<typename> class Constant;
// When describing shapes of constants or specifying 1-based subscript
// values as indices into constants, use a vector of integers.
using ConstantSubscript = std::int64_t;
using ConstantSubscript = common::SubscriptCIntType;
using ConstantSubscripts = std::vector<ConstantSubscript>;
inline int GetRank(const ConstantSubscripts &s) {
return static_cast<int>(s.size());
}
using LengthCIntType = common::SubscriptCIntType;
std::size_t TotalElementCount(const ConstantSubscripts &);
inline ConstantSubscripts InitialSubscripts(int rank) {
@ -127,7 +130,7 @@ public:
CLASS_BOILERPLATE(Constant)
explicit Constant(const Scalar<Result> &);
explicit Constant(Scalar<Result> &&);
Constant(std::int64_t, std::vector<Element> &&, ConstantSubscripts &&);
Constant(LengthCIntType, std::vector<Element> &&, ConstantSubscripts &&);
~Constant();
int Rank() const { return GetRank(shape_); }
@ -138,7 +141,7 @@ public:
std::size_t size() const;
const ConstantSubscripts &shape() const { return shape_; }
std::int64_t LEN() const { return length_; }
LengthCIntType LEN() const { return length_; }
std::optional<Scalar<Result>> GetScalarValue() const {
if (shape_.empty()) {
@ -160,7 +163,7 @@ public:
private:
Scalar<Result> values_; // one contiguous string
std::int64_t length_;
LengthCIntType length_;
ConstantSubscripts shape_;
};

View File

@ -267,8 +267,8 @@ static inline Expr<TR> FoldElementalIntrinsicHelper(FoldingContext &context,
}
// Build and return constant result
if constexpr (TR::category == TypeCategory::Character) {
std::int64_t len{
static_cast<std::int64_t>(results.size() ? results[0].length() : 0)};
auto len{static_cast<LengthCIntType>(
results.size() ? results[0].length() : 0)};
return Expr<TR>{Constant<TR>{len, std::move(results), std::move(shape)}};
} else {
return Expr<TR>{Constant<TR>{std::move(results), std::move(shape)}};
@ -966,8 +966,8 @@ static std::optional<Constant<SubscriptInteger>> GetConstantSubscript(
}
},
[](Triplet &triplet) -> std::optional<Constant<SubscriptInteger>> {
std::optional<std::int64_t> lbi{1}, ubi;
std::optional<std::int64_t> stride{ToInt64(triplet.stride())};
std::optional<ConstantSubscript> lbi{1}, ubi;
std::optional<ConstantSubscript> stride{ToInt64(triplet.stride())};
if (auto lower{triplet.lower()}) {
lbi = ToInt64(*lower);
}
@ -1005,7 +1005,7 @@ std::optional<Constant<T>> ApplySubscripts(parser::ContextualMessages &messages,
for (const auto &ss : subscripts) {
CHECK(ss.Rank() <= 1);
if (ss.Rank() == 1) {
resultShape.push_back(static_cast<std::int64_t>(ss.size()));
resultShape.push_back(static_cast<ConstantSubscript>(ss.size()));
elements *= ss.size();
}
}
@ -1234,7 +1234,8 @@ Expr<T> FoldOperation(FoldingContext &context, Designator<T> &&designator) {
Expr<ImpliedDoIndex::Result> FoldOperation(
FoldingContext &context, ImpliedDoIndex &&iDo) {
if (std::optional<std::int64_t> value{context.GetImpliedDo(iDo.name)}) {
if (std::optional<common::SubscriptCIntType> value{
context.GetImpliedDo(iDo.name)}) {
return Expr<ImpliedDoIndex::Result>{*value};
} else {
return Expr<ImpliedDoIndex::Result>{std::move(iDo)};
@ -1248,13 +1249,13 @@ public:
Expr<T> FoldArray(ArrayConstructor<T> &&array) {
// Calls FoldArray(const ArrayConstructorValues<T> &) below
if (FoldArray(array)) {
auto n{static_cast<std::int64_t>(elements_.size())};
auto n{static_cast<ConstantSubscript>(elements_.size())};
if constexpr (std::is_same_v<T, SomeDerived>) {
return Expr<T>{Constant<T>{array.GetType().GetDerivedTypeSpec(),
std::move(elements_), ConstantSubscripts{n}}};
} else if constexpr (T::category == TypeCategory::Character) {
auto length{Fold(context_, common::Clone(array.LEN()))};
if (std::optional<std::int64_t> lengthValue{ToInt64(length)}) {
if (std::optional<LengthCIntType> lengthValue{ToInt64(length)}) {
return Expr<T>{Constant<T>{
*lengthValue, std::move(elements_), ConstantSubscripts{n}}};
}
@ -1295,14 +1296,14 @@ private:
Fold(context_, Expr<SubscriptInteger>{iDo.upper()})};
Expr<SubscriptInteger> stride{
Fold(context_, Expr<SubscriptInteger>{iDo.stride()})};
std::optional<std::int64_t> start{ToInt64(lower)}, end{ToInt64(upper)},
step{ToInt64(stride)};
std::optional<common::SubscriptCIntType> start{ToInt64(lower)},
end{ToInt64(upper)}, step{ToInt64(stride)};
if (start.has_value() && end.has_value() && step.has_value()) {
if (*step == 0) {
return false;
}
bool result{true};
std::int64_t &j{context_.StartImpliedDo(iDo.name(), *start)};
common::SubscriptCIntType &j{context_.StartImpliedDo(iDo.name(), *start)};
if (*step > 0) {
for (; j <= *end; j += *step) {
result &= FoldArray(iDo.values());
@ -2066,14 +2067,14 @@ Expr<Type<TypeCategory::Character, KIND>> FoldOperation(
}
using Result = Type<TypeCategory::Character, KIND>;
if (auto folded{OperandsAreConstants(x)}) {
auto oldLength{static_cast<std::int64_t>(folded->first.size())};
auto oldLength{static_cast<LengthCIntType>(folded->first.size())};
auto newLength{folded->second.ToInt64()};
if (newLength < oldLength) {
folded->first.erase(newLength);
} else {
folded->first.append(newLength - oldLength, ' ');
}
CHECK(static_cast<std::int64_t>(folded->first.size()) == newLength);
CHECK(static_cast<LengthCIntType>(folded->first.size()) == newLength);
return Expr<Result>{Constant<Result>{std::move(folded->first)}};
}
return Expr<Result>{std::move(x)};

View File

@ -84,8 +84,8 @@ std::ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
if (Rank() > 0) {
o << '[' << GetType().AsFortran(std::to_string(length_)) << "::";
}
auto total{static_cast<std::int64_t>(size())};
for (std::int64_t j{0}; j < total; ++j) {
auto total{static_cast<LengthCIntType>(size())};
for (LengthCIntType j{0}; j < total; ++j) {
Scalar<Result> value{values_.substr(j * length_, length_)};
if (j > 0) {
o << ',';

View File

@ -174,7 +174,7 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
lower_ = AsExpr(Constant<SubscriptInteger>{1});
}
lower_.value() = evaluate::Fold(context, std::move(lower_.value().value()));
std::optional<std::int64_t> lbi{ToInt64(lower_.value().value())};
std::optional<ConstantSubscript> lbi{ToInt64(lower_.value().value())};
if (lbi.has_value() && *lbi < 1) {
context.messages().Say(
"Lower bound (%jd) on substring is less than one"_en_US,
@ -186,9 +186,9 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
upper_ = upper();
}
upper_.value() = evaluate::Fold(context, std::move(upper_.value().value()));
if (std::optional<std::int64_t> ubi{ToInt64(upper_.value().value())}) {
if (std::optional<ConstantSubscript> ubi{ToInt64(upper_.value().value())}) {
auto *literal{std::get_if<StaticDataObject::Pointer>(&parent_)};
std::optional<std::int64_t> length;
std::optional<LengthCIntType> length;
if (literal != nullptr) {
length = (*literal)->data().size();
} else if (const Symbol * symbol{GetLastSymbol()}) {
@ -206,7 +206,8 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
} else if (length.has_value() && *ubi > *length) {
context.messages().Say("Upper bound (%jd) on substring is greater "
"than character length (%jd)"_en_US,
static_cast<std::intmax_t>(*ubi), static_cast<std::int64_t>(*length));
static_cast<std::intmax_t>(*ubi),
static_cast<std::intmax_t>(*length));
*ubi = *length;
}
if (lbi.has_value() && literal != nullptr) {
@ -222,7 +223,7 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
}
parent_ = newStaticData;
lower_ = AsExpr(Constant<SubscriptInteger>{1});
std::int64_t length = newStaticData->data().size();
LengthCIntType length = newStaticData->data().size();
upper_ = AsExpr(Constant<SubscriptInteger>{length});
switch (width) {
case 1:

View File

@ -0,0 +1,72 @@
! Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
! See Fortran 2018, clause 16.10.2
! TODO: These are placeholder values so that some tests can be run.
module iso_fortran_env
integer, parameter :: atomic_int_kind = 8
integer, parameter :: atomic_logical_kind = 8
integer, parameter :: character_kinds(:) = [1, 2, 4]
integer, parameter :: int8 = 1, int16 = 2, int32 = 4, int64 = 8, int128 = 16
integer, parameter :: integer_kinds(:) = [int8, int16, int32, int64, int128]
integer, parameter :: logical_kinds(:) = [1, 2, 4, 8]
integer, parameter :: real16 = 2, real32 = 4, real64 = 8, real80 = 10, real128 = 16
integer, parameter :: real_kinds(:) = [real16, 3, real32, real64, real80, real128]
integer, parameter :: current_team = -1, initial_team = -2, parent_team = -3
integer, parameter :: input_unit = 5, output_unit = 6
integer, parameter :: iostat_end = -1, iostat_eor = -2
integer, parameter :: iostat_inquire_internal_unit = -1
integer, parameter :: character_storage_size = 8
integer, parameter :: file_storage_size = 8
integer, parameter :: numeric_storage_size = 32
integer, parameter :: stat_failed_image = -1
integer, parameter :: stat_locked = 2
integer, parameter :: stat_locked_other_image = 3
integer, parameter :: stat_stopped_image = 4
integer, parameter :: stat_unlocked = 5
integer, parameter :: stat_unlocked_failed_image = 6
type :: event_type
private
integer(kind=atomic_int_kind) :: count = 0
end type event_type
type :: lock_type
private
integer(kind=atomic_int_kind) :: count = 0
end type lock_type
type :: team_type
private
integer(kind=int64) :: id = 0
end type team_type
contains
character(len=80) function compiler_options()
compiler_options = 'COMPILER_OPTIONS() not yet implemented'
end function compiler_options
character(len=80) function compiler_version()
compiler_version = 'f18 in development'
end function compiler_version
end module iso_fortran_env

View File

@ -407,8 +407,6 @@ int main(int argc, char *const argv[]) {
defaultKinds.set_defaultRealKind(8);
} else if (arg == "-i8" || arg == "-fdefault-integer-8") {
defaultKinds.set_defaultIntegerKind(8);
} else if (arg == "-fno-large-arrays") {
defaultKinds.set_subscriptIntegerKind(4);
} else if (arg == "-help" || arg == "--help" || arg == "-?") {
std::cerr
<< "f18-parse-demo options:\n"

View File

@ -464,8 +464,6 @@ int main(int argc, char *const argv[]) {
defaultKinds.set_defaultRealKind(8);
} else if (arg == "-i8" || arg == "-fdefault-integer-8") {
defaultKinds.set_defaultIntegerKind(8);
} else if (arg == "-fno-large-arrays") {
defaultKinds.set_subscriptIntegerKind(4);
} else if (arg == "-module") {
driver.moduleDirectory = args.front();
args.pop_front();