1 //===-- runtime/tools.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef FORTRAN_RUNTIME_TOOLS_H_
10 #define FORTRAN_RUNTIME_TOOLS_H_
13 #include "terminator.h"
14 #include "flang/Common/optional.h"
15 #include "flang/Runtime/cpp-type.h"
16 #include "flang/Runtime/descriptor.h"
17 #include "flang/Runtime/freestanding-tools.h"
18 #include "flang/Runtime/memory.h"
22 #include <type_traits>
24 /// \macro RT_PRETTY_FUNCTION
25 /// Gets a user-friendly looking function signature for the current scope
26 /// using the best available method on each platform. The exact format of the
27 /// resulting string is implementation specific and non-portable, so this should
28 /// only be used, for example, for logging or diagnostics.
29 /// Copy of LLVM_PRETTY_FUNCTION
31 #define RT_PRETTY_FUNCTION __FUNCSIG__
32 #elif defined(__GNUC__) || defined(__clang__)
33 #define RT_PRETTY_FUNCTION __PRETTY_FUNCTION__
35 #define RT_PRETTY_FUNCTION __func__
38 #if defined(RT_DEVICE_COMPILATION)
39 // Use the pseudo lock and pseudo file unit implementations
41 #define RT_USE_PSEUDO_LOCK 1
42 #define RT_USE_PSEUDO_FILE_UNIT 1
45 namespace Fortran::runtime
{
49 RT_API_ATTRS
std::size_t TrimTrailingSpaces(const char *, std::size_t);
51 RT_API_ATTRS OwningPtr
<char> SaveDefaultCharacter(
52 const char *, std::size_t, const Terminator
&);
54 // For validating and recognizing default CHARACTER values in a
55 // case-insensitive manner. Returns the zero-based index into the
56 // null-terminated array of upper-case possibilities when the value is valid,
57 // or -1 when it has no match.
58 RT_API_ATTRS
int IdentifyValue(
59 const char *value
, std::size_t length
, const char *possibilities
[]);
61 // Truncates or pads as necessary
62 RT_API_ATTRS
void ToFortranDefaultCharacter(
63 char *to
, std::size_t toLength
, const char *from
);
65 // Utilities for dealing with elemental LOGICAL arguments
66 inline RT_API_ATTRS
bool IsLogicalElementTrue(
67 const Descriptor
&logical
, const SubscriptValue at
[]) {
68 // A LOGICAL value is false if and only if all of its bytes are zero.
69 const char *p
{logical
.Element
<char>(at
)};
70 for (std::size_t j
{logical
.ElementBytes()}; j
-- > 0; ++p
) {
77 inline RT_API_ATTRS
bool IsLogicalScalarTrue(const Descriptor
&logical
) {
78 // A LOGICAL value is false if and only if all of its bytes are zero.
79 const char *p
{logical
.OffsetElement
<char>()};
80 for (std::size_t j
{logical
.ElementBytes()}; j
-- > 0; ++p
) {
88 // Check array conformability; a scalar 'x' conforms. Crashes on error.
89 RT_API_ATTRS
void CheckConformability(const Descriptor
&to
, const Descriptor
&x
,
90 Terminator
&, const char *funcName
, const char *toName
,
91 const char *fromName
);
93 // Helper to store integer value in result[at].
94 template <int KIND
> struct StoreIntegerAt
{
95 RT_API_ATTRS
void operator()(const Fortran::runtime::Descriptor
&result
,
96 std::size_t at
, std::int64_t value
) const {
97 *result
.ZeroBasedIndexedElement
<Fortran::runtime::CppTypeFor
<
98 Fortran::common::TypeCategory::Integer
, KIND
>>(at
) = value
;
102 // Helper to store floating value in result[at].
103 template <int KIND
> struct StoreFloatingPointAt
{
104 RT_API_ATTRS
void operator()(const Fortran::runtime::Descriptor
&result
,
105 std::size_t at
, std::double_t value
) const {
106 *result
.ZeroBasedIndexedElement
<Fortran::runtime::CppTypeFor
<
107 Fortran::common::TypeCategory::Real
, KIND
>>(at
) = value
;
111 // Validate a KIND= argument
112 RT_API_ATTRS
void CheckIntegerKind(
113 Terminator
&, int kind
, const char *intrinsic
);
115 template <typename TO
, typename FROM
>
116 inline RT_API_ATTRS
void PutContiguousConverted(
117 TO
*to
, FROM
*from
, std::size_t count
) {
118 while (count
-- > 0) {
123 static inline RT_API_ATTRS
std::int64_t GetInt64(
124 const char *p
, std::size_t bytes
, Terminator
&terminator
) {
127 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 1> *>(p
);
129 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 2> *>(p
);
131 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 4> *>(p
);
133 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 8> *>(p
);
135 terminator
.Crash("GetInt64: no case for %zd bytes", bytes
);
139 static inline RT_API_ATTRS
Fortran::common::optional
<std::int64_t> GetInt64Safe(
140 const char *p
, std::size_t bytes
, Terminator
&terminator
) {
143 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 1> *>(p
);
145 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 2> *>(p
);
147 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 4> *>(p
);
149 return *reinterpret_cast<const CppTypeFor
<TypeCategory::Integer
, 8> *>(p
);
151 using Int128
= CppTypeFor
<TypeCategory::Integer
, 16>;
152 auto n
{*reinterpret_cast<const Int128
*>(p
)};
153 std::int64_t result
{static_cast<std::int64_t>(n
)};
154 if (static_cast<Int128
>(result
) == n
) {
157 return Fortran::common::nullopt
;
160 terminator
.Crash("GetInt64Safe: no case for %zd bytes", bytes
);
164 template <typename INT
>
165 inline RT_API_ATTRS
bool SetInteger(INT
&x
, int kind
, std::int64_t value
) {
168 reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 1> &>(x
) = value
;
169 return value
== reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 1> &>(x
);
171 reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 2> &>(x
) = value
;
172 return value
== reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 2> &>(x
);
174 reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 4> &>(x
) = value
;
175 return value
== reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 4> &>(x
);
177 reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 8> &>(x
) = value
;
178 return value
== reinterpret_cast<CppTypeFor
<TypeCategory::Integer
, 8> &>(x
);
184 // Maps intrinsic runtime type category and kind values to the appropriate
185 // instantiation of a function object template and calls it with the supplied
187 template <template <TypeCategory
, int> class FUNC
, typename RESULT
,
189 inline RT_API_ATTRS RESULT
ApplyType(
190 TypeCategory cat
, int kind
, Terminator
&terminator
, A
&&...x
) {
192 case TypeCategory::Integer
:
195 return FUNC
<TypeCategory::Integer
, 1>{}(std::forward
<A
>(x
)...);
197 return FUNC
<TypeCategory::Integer
, 2>{}(std::forward
<A
>(x
)...);
199 return FUNC
<TypeCategory::Integer
, 4>{}(std::forward
<A
>(x
)...);
201 return FUNC
<TypeCategory::Integer
, 8>{}(std::forward
<A
>(x
)...);
202 #if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
204 return FUNC
<TypeCategory::Integer
, 16>{}(std::forward
<A
>(x
)...);
207 terminator
.Crash("not yet implemented: INTEGER(KIND=%d)", kind
);
209 case TypeCategory::Real
:
211 #if 0 // TODO: REAL(2 & 3)
213 return FUNC
<TypeCategory::Real
, 2>{}(std::forward
<A
>(x
)...);
215 return FUNC
<TypeCategory::Real
, 3>{}(std::forward
<A
>(x
)...);
218 return FUNC
<TypeCategory::Real
, 4>{}(std::forward
<A
>(x
)...);
220 return FUNC
<TypeCategory::Real
, 8>{}(std::forward
<A
>(x
)...);
222 if constexpr (HasCppTypeFor
<TypeCategory::Real
, 10>) {
223 return FUNC
<TypeCategory::Real
, 10>{}(std::forward
<A
>(x
)...);
227 if constexpr (HasCppTypeFor
<TypeCategory::Real
, 16>) {
228 return FUNC
<TypeCategory::Real
, 16>{}(std::forward
<A
>(x
)...);
232 terminator
.Crash("not yet implemented: REAL(KIND=%d)", kind
);
233 case TypeCategory::Complex
:
235 #if 0 // TODO: COMPLEX(2 & 3)
237 return FUNC
<TypeCategory::Complex
, 2>{}(std::forward
<A
>(x
)...);
239 return FUNC
<TypeCategory::Complex
, 3>{}(std::forward
<A
>(x
)...);
242 return FUNC
<TypeCategory::Complex
, 4>{}(std::forward
<A
>(x
)...);
244 return FUNC
<TypeCategory::Complex
, 8>{}(std::forward
<A
>(x
)...);
246 if constexpr (HasCppTypeFor
<TypeCategory::Real
, 10>) {
247 return FUNC
<TypeCategory::Complex
, 10>{}(std::forward
<A
>(x
)...);
251 if constexpr (HasCppTypeFor
<TypeCategory::Real
, 16>) {
252 return FUNC
<TypeCategory::Complex
, 16>{}(std::forward
<A
>(x
)...);
256 terminator
.Crash("not yet implemented: COMPLEX(KIND=%d)", kind
);
257 case TypeCategory::Character
:
260 return FUNC
<TypeCategory::Character
, 1>{}(std::forward
<A
>(x
)...);
262 return FUNC
<TypeCategory::Character
, 2>{}(std::forward
<A
>(x
)...);
264 return FUNC
<TypeCategory::Character
, 4>{}(std::forward
<A
>(x
)...);
266 terminator
.Crash("not yet implemented: CHARACTER(KIND=%d)", kind
);
268 case TypeCategory::Logical
:
271 return FUNC
<TypeCategory::Logical
, 1>{}(std::forward
<A
>(x
)...);
273 return FUNC
<TypeCategory::Logical
, 2>{}(std::forward
<A
>(x
)...);
275 return FUNC
<TypeCategory::Logical
, 4>{}(std::forward
<A
>(x
)...);
277 return FUNC
<TypeCategory::Logical
, 8>{}(std::forward
<A
>(x
)...);
279 terminator
.Crash("not yet implemented: LOGICAL(KIND=%d)", kind
);
283 "not yet implemented: type category(%d)", static_cast<int>(cat
));
287 // Maps a runtime INTEGER kind value to the appropriate instantiation of
288 // a function object template and calls it with the supplied arguments.
289 template <template <int KIND
> class FUNC
, typename RESULT
, typename
... A
>
290 inline RT_API_ATTRS RESULT
ApplyIntegerKind(
291 int kind
, Terminator
&terminator
, A
&&...x
) {
294 return FUNC
<1>{}(std::forward
<A
>(x
)...);
296 return FUNC
<2>{}(std::forward
<A
>(x
)...);
298 return FUNC
<4>{}(std::forward
<A
>(x
)...);
300 return FUNC
<8>{}(std::forward
<A
>(x
)...);
301 #if defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T
303 return FUNC
<16>{}(std::forward
<A
>(x
)...);
306 terminator
.Crash("not yet implemented: INTEGER(KIND=%d)", kind
);
310 template <template <int KIND
> class FUNC
, typename RESULT
,
311 bool NEEDSMATH
= false, typename
... A
>
312 inline RT_API_ATTRS RESULT
ApplyFloatingPointKind(
313 int kind
, Terminator
&terminator
, A
&&...x
) {
315 #if 0 // TODO: REAL/COMPLEX (2 & 3)
317 return FUNC
<2>{}(std::forward
<A
>(x
)...);
319 return FUNC
<3>{}(std::forward
<A
>(x
)...);
322 return FUNC
<4>{}(std::forward
<A
>(x
)...);
324 return FUNC
<8>{}(std::forward
<A
>(x
)...);
326 if constexpr (HasCppTypeFor
<TypeCategory::Real
, 10>) {
327 return FUNC
<10>{}(std::forward
<A
>(x
)...);
331 if constexpr (HasCppTypeFor
<TypeCategory::Real
, 16>) {
332 // If FUNC implemenation relies on FP math functions,
333 // then we should not be here. The compiler should have
334 // generated a call to an entry in FortranFloat128Math
336 if constexpr (!NEEDSMATH
) {
337 return FUNC
<16>{}(std::forward
<A
>(x
)...);
342 terminator
.Crash("not yet implemented: REAL/COMPLEX(KIND=%d)", kind
);
345 template <template <int KIND
> class FUNC
, typename RESULT
, typename
... A
>
346 inline RT_API_ATTRS RESULT
ApplyCharacterKind(
347 int kind
, Terminator
&terminator
, A
&&...x
) {
350 return FUNC
<1>{}(std::forward
<A
>(x
)...);
352 return FUNC
<2>{}(std::forward
<A
>(x
)...);
354 return FUNC
<4>{}(std::forward
<A
>(x
)...);
356 terminator
.Crash("not yet implemented: CHARACTER(KIND=%d)", kind
);
360 template <template <int KIND
> class FUNC
, typename RESULT
, typename
... A
>
361 inline RT_API_ATTRS RESULT
ApplyLogicalKind(
362 int kind
, Terminator
&terminator
, A
&&...x
) {
365 return FUNC
<1>{}(std::forward
<A
>(x
)...);
367 return FUNC
<2>{}(std::forward
<A
>(x
)...);
369 return FUNC
<4>{}(std::forward
<A
>(x
)...);
371 return FUNC
<8>{}(std::forward
<A
>(x
)...);
373 terminator
.Crash("not yet implemented: LOGICAL(KIND=%d)", kind
);
377 // Calculate result type of (X op Y) for *, //, DOT_PRODUCT, &c.
378 Fortran::common::optional
<
379 std::pair
<TypeCategory
, int>> inline constexpr RT_API_ATTRS
380 GetResultType(TypeCategory xCat
, int xKind
, TypeCategory yCat
, int yKind
) {
381 int maxKind
{std::max(xKind
, yKind
)};
383 case TypeCategory::Integer
:
385 case TypeCategory::Integer
:
386 return std::make_pair(TypeCategory::Integer
, maxKind
);
387 case TypeCategory::Real
:
388 case TypeCategory::Complex
:
389 #if !(defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T)
394 return std::make_pair(yCat
, yKind
);
399 case TypeCategory::Real
:
401 case TypeCategory::Integer
:
402 #if !(defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T)
407 return std::make_pair(TypeCategory::Real
, xKind
);
408 case TypeCategory::Real
:
409 case TypeCategory::Complex
:
410 return std::make_pair(yCat
, maxKind
);
415 case TypeCategory::Complex
:
417 case TypeCategory::Integer
:
418 #if !(defined __SIZEOF_INT128__ && !AVOID_NATIVE_UINT128_T)
423 return std::make_pair(TypeCategory::Complex
, xKind
);
424 case TypeCategory::Real
:
425 case TypeCategory::Complex
:
426 return std::make_pair(TypeCategory::Complex
, maxKind
);
431 case TypeCategory::Character
:
432 if (yCat
== TypeCategory::Character
) {
433 return std::make_pair(TypeCategory::Character
, maxKind
);
435 return Fortran::common::nullopt
;
437 case TypeCategory::Logical
:
438 if (yCat
== TypeCategory::Logical
) {
439 return std::make_pair(TypeCategory::Logical
, maxKind
);
441 return Fortran::common::nullopt
;
446 return Fortran::common::nullopt
;
449 // Accumulate floating-point results in (at least) double precision
450 template <TypeCategory CAT
, int KIND
>
451 using AccumulationType
= CppTypeFor
<CAT
,
452 CAT
== TypeCategory::Real
|| CAT
== TypeCategory::Complex
453 ? std::max(KIND
, static_cast<int>(sizeof(double)))
456 // memchr() for any character type
457 template <typename CHAR
>
458 static inline RT_API_ATTRS
const CHAR
*FindCharacter(
459 const CHAR
*data
, CHAR ch
, std::size_t chars
) {
460 const CHAR
*end
{data
+ chars
};
461 for (const CHAR
*p
{data
}; p
< end
; ++p
) {
470 inline RT_API_ATTRS
const char *FindCharacter(
471 const char *data
, char ch
, std::size_t chars
) {
472 return reinterpret_cast<const char *>(
473 runtime::memchr(data
, static_cast<int>(ch
), chars
));
476 // Copy payload data from one allocated descriptor to another.
477 // Assumes element counts and element sizes match, and that both
478 // descriptors are allocated.
479 RT_API_ATTRS
void ShallowCopyDiscontiguousToDiscontiguous(
480 const Descriptor
&to
, const Descriptor
&from
);
481 RT_API_ATTRS
void ShallowCopyDiscontiguousToContiguous(
482 const Descriptor
&to
, const Descriptor
&from
);
483 RT_API_ATTRS
void ShallowCopyContiguousToDiscontiguous(
484 const Descriptor
&to
, const Descriptor
&from
);
485 RT_API_ATTRS
void ShallowCopy(const Descriptor
&to
, const Descriptor
&from
,
486 bool toIsContiguous
, bool fromIsContiguous
);
487 RT_API_ATTRS
void ShallowCopy(const Descriptor
&to
, const Descriptor
&from
);
489 // Ensures that a character string is null-terminated, allocating a /p length +1
490 // size memory for null-terminator if necessary. Returns the original or a newly
491 // allocated null-terminated string (responsibility for deallocation is on the
493 RT_API_ATTRS
char *EnsureNullTerminated(
494 char *str
, std::size_t length
, Terminator
&terminator
);
496 RT_API_ATTRS
bool IsValidCharDescriptor(const Descriptor
*value
);
498 RT_API_ATTRS
bool IsValidIntDescriptor(const Descriptor
*intVal
);
500 // Copy a null-terminated character array \p rawValue to descriptor \p value.
501 // The copy starts at the given \p offset, if not present then start at 0.
502 // If descriptor `errmsg` is provided, error messages will be stored to it.
503 // Returns stats specified in standard.
504 RT_API_ATTRS
std::int32_t CopyCharsToDescriptor(const Descriptor
&value
,
505 const char *rawValue
, std::size_t rawValueLength
,
506 const Descriptor
*errmsg
= nullptr, std::size_t offset
= 0);
508 RT_API_ATTRS
void StoreIntToDescriptor(
509 const Descriptor
*length
, std::int64_t value
, Terminator
&terminator
);
511 // Defines a utility function for copying and padding characters
512 template <typename TO
, typename FROM
>
513 RT_API_ATTRS
void CopyAndPad(
514 TO
*to
, const FROM
*from
, std::size_t toChars
, std::size_t fromChars
) {
515 if constexpr (sizeof(TO
) != sizeof(FROM
)) {
516 std::size_t copyChars
{std::min(toChars
, fromChars
)};
517 for (std::size_t j
{0}; j
< copyChars
; ++j
) {
520 for (std::size_t j
{copyChars
}; j
< toChars
; ++j
) {
521 to
[j
] = static_cast<TO
>(' ');
523 } else if (toChars
<= fromChars
) {
524 std::memcpy(to
, from
, toChars
* sizeof(TO
));
526 std::memcpy(to
, from
, std::min(toChars
, fromChars
) * sizeof(TO
));
527 for (std::size_t j
{fromChars
}; j
< toChars
; ++j
) {
528 to
[j
] = static_cast<TO
>(' ');
533 RT_API_ATTRS
void CreatePartialReductionResult(Descriptor
&result
,
534 const Descriptor
&x
, std::size_t resultElementSize
, int dim
, Terminator
&,
535 const char *intrinsic
, TypeCode
);
537 } // namespace Fortran::runtime
538 #endif // FORTRAN_RUNTIME_TOOLS_H_