Commit Graph

404 Commits

Author SHA1 Message Date
Peter Klausler e0e2a11751 [flang][runtime] Make ASSOCIATED() conform with standard
ASSOCIATED() must be false for zero-sized arrays, and the
strides on a dimension are irrelevant when the extent is unitary.

Differential Revision: https://reviews.llvm.org/D127793
2022-06-16 11:39:44 -07:00
Peter Klausler 17853928a6 [flang] Correct implementation of WAIT with no ID
Previous one was returning a bogus error status about a bad WAIT
statement ID number.

Differential Revision: https://reviews.llvm.org/D127979
2022-06-16 10:00:40 -07:00
Peter Klausler cfbde7149d [flang][runtime] Catch more (all?) negative unit number errors
Fortran does have negative unit numbers -- they show up in child I/O
subroutines for defined I/O and for OPEN(NEWUNIT=) -- but the runtime
needs to catch the cases where a negative unit number that wasn't
generated by the runtime is passed in for OPEN or for an I/O statement
that would ordinarily create an anonymous "fort.NNN" file for a
hitherto unseen unit.

Differential Revision: https://reviews.llvm.org/D127788
2022-06-15 17:29:50 -07:00
Peter Klausler 690de85ed8 [flang][runtime] Better error message for mis-ASSIGN'ed FORMAT
When an I/O data transfer statement uses an ASSIGN'ed FORMAT that
has not been ASSIGN'ed to a FORMAT statement, the runtime receives
a zero-length format string.  Distinguish this case from the general
error message about missing parentheses.

Differential Revision: https://reviews.llvm.org/D127797
2022-06-15 14:46:22 -07:00
Peter Klausler db6c3ecd2c [flang][runtime] Make NCOPIES= argument of REPEAT a signed integer, & check it
NCOPIES= is currently a std::size_t in the API.  If a negative value is
used, the memory allocation will fail.  Change it to be a signed integer,
and crash with a message instead if it be negative.

Differential Revision: https://reviews.llvm.org/D127795
2022-06-15 14:29:17 -07:00
Peter Klausler 1a3ac58667 [flang][runtime] Fix handling of output FORMAT('x' 'y')
I'm emitting "x'y" because the space-separated apostrophes are
misinterpreted as being adjacent repeated quotation marks.
Fix to ensure no space skipping is applied when checking for
repeated quotation marks.

Differential Revision: https://reviews.llvm.org/D127792
2022-06-15 14:23:02 -07:00
Peter Klausler f6253eb9ba [flang][runtime] Fix INQUIRE(POS=n) for non-advancing I/O
Position inquiries need to account for offsets in records to be
accurate in the case of non-advancing I/O.

Differential Revision: https://reviews.llvm.org/D127789
2022-06-15 14:14:06 -07:00
Peter Klausler 8ef1075381 [flang][runtime] Signal END from character input
There's code in EditCharacterInput() that causes that template function
to silently return false if it is invoked at the end of the input file.
This overrides other checks that properly call SignalEnd() later.

Differential Revision: https://reviews.llvm.org/D127786
2022-06-15 14:12:38 -07:00
Peter Klausler 6963fb7dfb [flang][runtime] Fix REWIND after non-advancing data transfer
A REWIND of a unit that's in the middle of a record due to a READ
or WRITE statement with ADVANCE='NO' needs to reset the left tab
limit so that the next transfer takes place at the beginning of
the first record.

Differential Revision: https://reviews.llvm.org/D127783
2022-06-15 13:40:49 -07:00
Peter Klausler 142db43b62 [flang][runtime] Allow recovery from BACKSPACE(badUnit)
When an unconnected unit number is used in a BACKSPACE statement
with ERR=, IOSTAT=, &/or IOMSG= control specifiers, don't crash,
but let the program deal with the error.

Differential Revision: https://reviews.llvm.org/D127782
2022-06-15 13:00:54 -07:00
Peter Klausler 6ce0fba0e8 [flang][runtime] Skip remainder of bad input record even with ADVANCE='NO'
After a recoverable error condition in a READ statement with ADVANCE='NO',
skip the remainder of the current record.

Differential Revision: https://reviews.llvm.org/D127433
2022-06-13 16:36:19 -07:00
Peter Klausler 850097d6dc [flang][runtime] Don't loop in runtime if blank appears in BOZ input
The code for scanning BOZ input allows for blanks and tabs to appear,
but can hang if they do and the BOZ input value is not followed by
extra valid digits; the repositioning for the second sweep simply
needed to be done in units of character, not valid digits.

Differential Revision: https://reviews.llvm.org/D127431
2022-06-13 16:26:50 -07:00
Peter Klausler db52dda8ab [flang][runtime] Detect overflow of fixed-sized exponent output field
When Ew.dEe or Gw.dEe output has an exponent requiring more than 'e'
digits, the whole output field must overflow to asterisks.  The runtime
was detecting short fields and padding them with zeroes, but not
overflow.

Differential Revision: https://reviews.llvm.org/D127428
2022-06-13 16:10:32 -07:00
Peter Klausler 4c42e67bf7 [flang][runtime] Fix overflow detection for REAL input
The test for an overflow during decimal->binary conversion was taking
place too late, causing the data not to be rescanned from the beginning.

Differential Revision: https://reviews.llvm.org/D127427
2022-06-13 16:05:11 -07:00
Peter Klausler 3f3edbe5fc [flang][runtime] Don't emit any leading blanks for G0/E0 output editing
There were cases where E0.d output editing (or G0.d editing that is converted
to E0.d) would emit one or more leading blank characters; fix them.

Differential Revision: https://reviews.llvm.org/D127426
2022-06-13 16:03:24 -07:00
Peter Klausler bd577afe8f [flang][runtime] Fix runtime CSHIFT of rank>1 array case of negative shift count
The calculation of the source index was incorrect when a CSHIFT shift
count value is negative, for the implementation of CSHIFT for arrays
with rank >= 2.  (The vector CSHIFT is fine.)

Differential Revision: https://reviews.llvm.org/D127424
2022-06-13 11:34:25 -07:00
Peter Klausler 2a07db4cf6 [flang][runtime] Don't crash after reporting I/O statement errors
When an I/O statement is known to be in a recoverable error state,
it shouldn't cause a crash later in execution because it's not in
an expected non-erroneous processing state.  Add checking for the
ErroneousIoStatementState variant on paths that might otherwise
lead to runtime crashes.

Differential Revision: https://reviews.llvm.org/D127423
2022-06-13 11:24:59 -07:00
Peter Klausler 1444bc8241 [flang][runtime] Fix list-directed/NAMELIST complex input with "r*" repetition count
The little state machine in GetNextDataEdit() wasn't transitioning
properly from reading the imaginary part of the number back to the
real part for the next iteration of a repeated value.

Differential Revision: https://reviews.llvm.org/D127422
2022-06-13 10:50:25 -07:00
Peter Klausler 166d6ed5c7 [flang][runtime] Better (but still synchronous) support for asynchronous I/O
Track pending "asynchronous" I/O operation IDs so that WAIT statements can
report errors about bad ID numbers.

Lowering will need to extended to call GetAsynchronousId() for a READ or
WRITE statement with ID=n.

Differential Revision: https://reviews.llvm.org/D127421
2022-06-13 10:43:14 -07:00
Peter Klausler 2496938522 [flang][runtime] Fix nonadvancing output edge case
When nonadvancing output uses T/TL control edit descriptors to reposition
the record, don't reset the position to the furthest point written at
the end of the write.

Differential Revision: https://reviews.llvm.org/D127420
2022-06-13 10:27:27 -07:00
Markus Mützel 99fe38a13a [flang][runtime] Remove dependency on C++ <mutex> on Windows
The implementation of the Lock class on Windows currently uses C++
mutexes. That introduces a dependency on the C++ runtime on that
platform.

Use a Windows CriticalSection instead of a std::mutex to avoid that
dependency.

This works for me with MinGW (tested in a CLANG64 environment of MSYS2).

See also D126291.

Differential Revision: https://reviews.llvm.org/D127316
2022-06-10 11:25:45 +00:00
Peter Klausler 4daa33f6d1 [flang][runtime] Use __float128 where possible & needed in runtime
On targets with __float128 available and distinct from long double,
use it to support more kind=16 entry points.  This affects mostly
x86-64 targets.  This means that more runtime entry points are
defined for lowering to call.

Delete Common/long-double.h and its LONG_DOUBLE macro in favor of
testing the standard macro LDBL_MANT_DIG.

Differential Revision: https://reviews.llvm.org/D127025
2022-06-05 09:16:57 -07:00
Peter Klausler 03c066ab13 [flang][runtime] Catch OPEN of connected file
Diagnose OPEN(FILE=f) when f is already connected by the same name to
a distinct external I/O unit.

Differential Revision: https://reviews.llvm.org/D127035
2022-06-04 11:06:37 -07:00
Peter Klausler 562fd2c99b [flang][runtime] Emit error message rather than crashing for MOD(ULO)(x,P=0)
Add extra arguments and checks to the runtime support library so that
a call to the intrinsic functions MOD and MODULO with "denominator"
argument P of zero will cause a crash with a source location rather
than an uninformative floating-point error or integer division by
zero signal.

Additional work is required in lowering to (1) pass source file path and
source line number arguments and (2) actually call these runtime
library APIs instead of emitting inline code for MOD &/or MODULO.

Differential Revision: https://reviews.llvm.org/D127034
2022-06-04 11:02:48 -07:00
Peter Klausler 11f928af9b [flang][runtime] Fix deadlock in error recovery
When an external I/O statement is in a recoverable error
state before any data transfers take place (for example,
an unformatted transfer with ERR=/IOSTAT=/IOMSG= attempted on
a formatted unit), ensure that the unit's mutex is still
released at the end of the statement.

Differential Revision: https://reviews.llvm.org/D127032
2022-06-04 09:55:53 -07:00
Peter Klausler 9a163ffe1a [flang][runtime] Fix WRITE after OPEN(.., ACCESS="APPEND")
The initial size of the file was not being captured as the file position
on which the first output buffer should be framed.

Differential Revision: https://reviews.llvm.org/D127029
2022-06-04 09:18:25 -07:00
Peter Klausler dfcccc6dee [flang][runtime] Fix edge case discrepancies with EN output editing
The "engineering" ENw.d output editing descriptor has some difficult
edge case behavior for values that might format into a bunch of 9's
or round up to a 1 for a given scale factor.  Fix the algorithm,
and add tests to protect against regressions.

Differential Revision: https://reviews.llvm.org/D127028
2022-06-04 09:14:05 -07:00
Peter Klausler ea5b205bb8 [flang][runtime] Don't crash after surviving internal output overflow
After the program has survived its attempt to overflow the output buffer
with an internal WRITE using ERR=, IOSTAT=, &/or IOMSG=, don't crash
by accidentally blank-filling the next record that usually doesn't exist.

Differential Revision: https://reviews.llvm.org/D127024
2022-06-04 08:47:13 -07:00
Peter Klausler ea1a69d66d [flang][runtime] Don't let random seed queries change the sequence
When the current seed of the pseudo-random generator is queried
with CALL RANDOM_SEED(GET=n), that query should not change the
stream of pseudo-random numbers produced by CALL RANDOM_NUMBER().

Differential Revision: https://reviews.llvm.org/D127023
2022-06-04 08:01:46 -07:00
Peter Klausler 9c54d76251 [flang][runtime] Signal new I/O error on floating-point input overflow
Besides raising the IEEE floating-point overflow exception, treat
a floating-point overflow on input as an I/O error catchable with
ERR=, IOSTAT=, &/or IOMSG=.

Differential Revision: https://reviews.llvm.org/D127022
2022-06-03 22:55:03 -07:00
Peter Klausler f3278e0f3c [flang][runtime] Ensure that 0. <= RANDOM_NUMBER() < 1.
It was possible for RANDOM_NUMBER() to return 1.0.

Differential Revision: https://reviews.llvm.org/D127020
2022-06-03 22:44:19 -07:00
Peter Klausler aa77cf90aa [flang][runtime] Signal format error when input field width is zero
A data edit descriptor for input may not have a zero field width.

Differential Revision: https://reviews.llvm.org/D127017
2022-06-03 18:11:00 -07:00
Peter Klausler e5a4f730da [flang][runtime] OPEN write-only files
If a file being opened with no ACTION= is write-only then cope with
it rather than defaulting prematurely to treating it as read-only.

Differential Revision: https://reviews.llvm.org/D127015
2022-06-03 18:09:40 -07:00
Peter Klausler 9878facfd0 [flang][runtime] INQUIRE(FILE="...",SIZE=nbytes)
Implement inquire-by-file SIZE= specifier.

Differential Revision: https://reviews.llvm.org/D127014
2022-06-03 18:05:27 -07:00
Peter Klausler da63fee0d0 [flang][runtime] Allow extra character for E0.0 output editing
When the digit count ('d') is zero in E0 editing, allow for one more
output character; otherwise, any - or + sign in the output causes
an output field overflow.

Differential Revision: https://reviews.llvm.org/D127013
2022-06-03 17:41:22 -07:00
Peter Klausler 604016dbe4 [flang][runtime] Fix bug with extra leading zero in octal output
Octal (O) output editing often emits an extra leading 0 digit
due to the total digit count being off by one since word sizes
aren't multiples of three bits.

Differential Revision: https://reviews.llvm.org/D127012
2022-06-03 17:02:07 -07:00
Mehdi Chinoune 360411957b [flang][MSVC] Fix building with `/permissive-` flag
CLOCK_REALTIME is POSIX defined and never available with MSVC, even without /permissive-.
The difference is that the template is never instantiated and the compiler ignores the undefined identifier.

Reviewed By: Meinersbur

Differential Revision: https://reviews.llvm.org/D125262
2022-05-24 23:36:04 -05:00
Peter Klausler cdd54cbdd9 [flang][runtime] Catch decimal integer input overflow
B/O/Z input overflow is already caught, and real input overflow
is signalled as an IEEE arithmetic exception, but regular decimal
integer overflow was silent.

Differential Revision: https://reviews.llvm.org/D126155
2022-05-24 14:07:35 -07:00
Peter Klausler 8527f9e443 [flang][runtime] Handle BACKSPACE after reading past EOF
An external READ(END=) that hits the end of the file must
also note the virtual position of the endfile record that
has just been discovered, so that a later BACKSPACE statement
won't end up at the wrong record.

Differential Revision: https://reviews.llvm.org/D126146
2022-05-24 10:27:50 -07:00
Peter Klausler d90e866a19 [flang][runtime] INQUIRE(UNIT=666,NUMBER=n) must set n=666
Whether a unit number in an inquire-by-unit statement is valid or not,
it should be the value to which the NUMBER= variable is set, not -1.
-1 should be returned to NUMBER= only for an inquire-by-file statement
when the FILE= is not connected to any unit.

Differential Revision: https://reviews.llvm.org/D126145
2022-05-24 09:48:32 -07:00
Peter Klausler deb62f5ad6 [flang][runtime] Clean up asynchronous I/O APIs
Now that the requirements and implementation of asynchronous I/O are
better understood, adjust their I/O runtime APIs.  In particular:
1) Remove the BeginAsynchronousOutput/Input APIs; they're not needed,
   since any data transfer statement might have ASYNCHRONOUS= and
   (if ASYNCHRONOUS='YES') ID= control list specifiers that need to
   at least be checked.
2) Add implementations for BeginWait(All) to check for the error
   case of a bad unit number and nonzero ID=.
3) Rearrange and comment SetAsynchronous so that it's clear that
   it can be called for READ/WRITE as well as for OPEN.

The implementation remains completely synchronous, but should be conforming.
Where opportunities make sense for true asynchronous implementations of
some big block transfers without SIZE= in the future, we'll need to add
a GetAsynchronousId API to capture ID= on a READ or WRITE; add sourceFile
and sourceLine arguments to BeginWait(All) for good error reporting;
track pending operations in unit.h; and add code to force synchronization
to non-asynchronous I/O operations.

Lowering should call SetAsynchronous when ASYNCHRONOUS= appears as
a control list specifier.  It should also set ID=x variables to 0
until such time as we support asynchronous operations, if ever.
This patch only removes the removed APIs from lowering.

Differential Revision: https://reviews.llvm.org/D126143
2022-05-24 07:54:57 -07:00
Peter Klausler e141e719e1 [flang] Fix character length calculation for Unicode component
The character length value in the derived type component information table
entry is already in units of characters, not bytes, so don't divide by the
per-character byte size.

Differential Revision: https://reviews.llvm.org/D126139
2022-05-23 17:48:12 -07:00
Diana Picus 6bcafce103 [flang][Runtime] Use proper prototypes in Fortran_main. NFCI
This is compiled as C code, so it's a good idea to be explicit about the
prototype. Clang complains about this when -Wstrict-prototypes is used.

Differential Revision: https://reviews.llvm.org/D125672
2022-05-18 08:04:09 +00:00
Diana Picus 3d2e05d542 [flang] Install Fortran_main library
At the moment the Fortran_main library is not installed, so it cannot be
found by the driver when run from an install directory. This patch fixes
the issue by replacing llvm_add_library with add_flang_library, which
already contains all the proper incantations for installing a library.
It also enhances add_flang_library to support a STATIC arg which forces
the library to be static even when BUILD_SHARED_LIBS is on.

Differential Revision: https://reviews.llvm.org/D124759

Co-authored-by: Dan Palermo <Dan.Palermo@amd.com>
2022-05-16 08:30:53 +00:00
Peter Steinfeld d4609ae47d [flang] Change "bad kind" messages in the runtime to "not yet implemented"
Similar to change D125046.

If a programmer is able to compile and link a program that contains types that
are not yet supported by the runtime, it must be because they're not yet
implemented.

This change will make it easier to find unimplemented code in tests.

Differential Revision: https://reviews.llvm.org/D125267
2022-05-10 20:08:03 -07:00
Peter Klausler 72831a592e [flang][runtime] BACKSPACE after non-advancing I/O
A BACKSPACE statement on a unit after a READ or WRITE with ADVANCE="NO"
must reset the position to the beginning of the record, not to the
beginning of the previous one.

Differential Revision: https://reviews.llvm.org/D125057
2022-05-09 13:00:41 -07:00
Peter Klausler 28b5e99a4c [flang][runtime] (G0) for CHARACTER means (A), not (A0)
I'm emitting zero characters for (G0) formatting of CHARACTER values
instead of using their lengths to determine the output field width.

Differential Revision: https://reviews.llvm.org/D125056
2022-05-09 13:00:15 -07:00
Peter Klausler fb9ec95cf0 [flang][runtime] Enforce restrictions on unlimited format repetition
A repeated format item group with an unlimited ('*') repetition count
can appear only as the last item at the top level of a format; it can't
be nested in more parentheses and it can't be followed by anything
else.

Differential Revision: https://reviews.llvm.org/D125054
2022-05-09 12:48:23 -07:00
Peter Klausler cea8b8a72d [flang][runtime] Don't pad CHARACTER input at end of record unless PAD='YES'
When formatted CHARACTER input runs into the end of an input record,
the runtime usually fills the remainder of the variable with spaces,
but this should be conditional, and not done when PAD='NO'.

And while here, add some better comments to two members of connection.h
to make their non-obvious relationship more clear.

Differential Revision: https://reviews.llvm.org/D125053
2022-05-09 12:39:43 -07:00
Peter Klausler 2f31b4b10a [flang][runtime] Fix input of NAN(...) on non-fast path
The closing parenthesis needs to be consumed when a NaN
with parenthesized (ignored) information is read on the
real input path that preprocesses input characters before
passing them to the decimal-to-binary converter.

Differential Revision: https://reviews.llvm.org/D125048
2022-05-09 12:38:31 -07:00