Commit Graph

420 Commits

Author SHA1 Message Date
Peter Klausler be68a6adfb [flang] Error detection/avoidance for TRANSFER with empty MOLD= type
When MOLD= is an array and there is no SIZE= in a call to TRANSFER(),
the size of an element of the MOLD= is used as the denominator in a
division to establish the extent of the vector result.  When the
total storage size of the SOURCE= is known to be zero, the result is
empty and no division is needed.

To avoid a division by zero at runtime, we need to check for a zero-sized
MOLD= element type when the storage size of SOURCE= is nonzero and there
is no SIZE=.  Further, in the compilation-time rewriting of calls to
SHAPE(TRANSFER(...)) and SIZE(TRANSFER(...)) for constant folding and
simplification purposes, we can't replace the call with an arithmetic
element count expression when the storage size of SOURCE= is not known
to be zero and the element size of MOLD= is not known to be nonzero at
compilation time.

These changes mostly affect tests using a MOLD= argument that is an
assumed-length character.

Differential Revision: https://reviews.llvm.org/D129680
2022-07-13 16:50:57 -07:00
Peter Klausler cb193931fa [flang][runtime] Complete list-directed character input with DECIMAL='COMMA'
Most of the infrastructure for DECIMAL='COMMA' mode was in place
in the I/O runtime support library, but I dropped the ball for
list-directed character input, which has its own detection of
input separators.  Finish the job.

Differential Revision: https://reviews.llvm.org/D129679
2022-07-13 16:37:44 -07:00
Peter Klausler 1a65d09dcf [flang][runtime] Keep frame buffer in sync with file when truncating
When the I/O runtime is truncating an external file due to an
implied ENDFILE or explicit ENDFILE, ensure that the unit's frame
buffer for the file discards any data that have become obsolete.

This bug caused trouble with ACCESS='STREAM' I/O using POS= on
a WRITE, but it may have not been limited to that scenario.

Differential Revision: https://reviews.llvm.org/D129673
2022-07-13 16:12:12 -07:00
Peter Klausler faffcc3a46 [flang][runtime] Refine list-directed REAL(2) output
The rule used by list-directed REAL output editing to select
between Ew.d and Fw.d output editing breaks down for 16-bit
floating-point data, since the number of significant decimal
digits is so low that Ew,d output editing is nearly always selected.
Cap the test so that five-digit values will be output with Fw.d
editing.

Differential Revision: https://reviews.llvm.org/D129672
2022-07-13 16:10:46 -07:00
Peter Steinfeld e3611f82fc [flang] Fix typo in runtime message
The title says it.

Differential Revision: https://reviews.llvm.org/D129329
2022-07-07 14:10:25 -07:00
Peter Steinfeld 9f9e9d9cfc [flang] SET_EXPONENT(-0.0) should return -0.0
Section 16.9.171 says:

If X has the value zero, the result has the same value as X

So if X is -0.0, SET_EXPONENT should return -0.0.

Differential Revision: https://reviews.llvm.org/D129309
2022-07-07 12:43:30 -07:00
Peter Klausler 7c708adb4a [flang][runtime] Trim FORMATs echoed to error messages
Since dynamic FORMAT strings usually come from blank-padded fixed-length
CHARACTER variables, trim leading and trailing blanks from them when they
are echoed to error messages for better readability.

Differential Revision: https://reviews.llvm.org/D129024
2022-07-06 18:55:03 -07:00
Peter Klausler 061df07389 [flang][runtime] Fix directed UP/DOWN rounding edge case for Fw.d output
When Fw.d output editing takes place with directed rounding, make sure that
nonzero values that would normally be converted to zero round up (or down,
depending on the sign) to a scaled 1.

Differential Revision: https://reviews.llvm.org/D129021
2022-07-06 17:35:34 -07:00
Peter Klausler 715283aa17 [flang] Cope with overflow in real MOD/MODULO
In folding and in the runtime library for real MOD/MODULO(A,P),
detect overflow from the division A/P and return a properly signed
zero result.  (When A/P overflows and both A and P are finite numbers
with nonzero P, the quotient would be a large integer when rounded to
the precision of the floating-point representation.)

Differential Revision: https://reviews.llvm.org/D129020
2022-07-06 17:31:11 -07:00
Peter Klausler 0508fd5935 [flang][runtime] Make ENDFILE work after non-advancing READ
An ENDFILE statement executed when a non-advancing READ has
left the unit in the middle of a record must truncate the file
at that position.

Differential Revision: https://reviews.llvm.org/D129019
2022-07-06 17:30:33 -07:00
Peter Klausler 53dca2e6ec [flang][runtime] Emit "0.0E+0" for (1PG0.0) editing of 0.0
When a scale factor is in effect, respect it for G0.0 output editing.

Differential Revision: https://reviews.llvm.org/D128761
2022-06-29 11:42:39 -07:00
Peter Klausler df6afee985 [flang][runtime] Improve G0 output editing
G0 output editing should never overflow an output field and fill it
with asterisks.  It should also never elide the "E" in an exponent
field, even if it has more than three digits.

Differential Revision: https://reviews.llvm.org/D128396
2022-06-23 11:27:25 -07:00
Peter Klausler c078e464a0 [flang][runtime] FLUSH(bad or unconnected unit number) is an error
Some I/O control statements are no-ops when attempted on a bad or
unconnected UNIT=, but the standard says that FLUSH is an error
in that case.

Differential Revision: https://reviews.llvm.org/D128392
2022-06-23 11:08:53 -07:00
Peter Klausler ede4213169 [flang][runtime] Handle READ of non-UTF-8 data into multi-byte CHARACTER
When a READ statement reads into a CHARACTER(2 or 4) variable from a
unit whose encoding is not UTF-8, don't copy bytes directly; they must
each be zero-extended.

Differential Revision: https://reviews.llvm.org/D128390
2022-06-23 10:02:14 -07:00
Peter Klausler 1650fb8a53 [flang][runtime] Respect PAD='NO' on READ/WRITE
The check for the PAD= setting should examine the mutable modes
of the current I/O statement, not the persistent modes of the
I/O unit.

Differential Revision: https://reviews.llvm.org/D128389
2022-06-23 09:50:22 -07:00
Peter Klausler d771245a9d [flang] Fix READ/WRITE with POS= on stream units, with refactoring
First, ExternalFileUnit::SetPosition was being used both as a utility
within the class' member functions as well as an API from I/O statement
processing.  Make it private, and add APIs for SetStreamPos and SetDirectRec.

Second, ensure that SetStreamPos for POS= positioning in a stream
doesn't leave the current record number and endfile record number
in an arbitrary state.  In stream I/O they are used only to manage
end-of-file detection, and shouldn't produce false positive results
from IsAtEnd() after repositioning.

Differential Revision: https://reviews.llvm.org/D128388
2022-06-23 09:16:49 -07:00
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