308 lines · c
1//===-- lib/runtime/unit.h --------------------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9// Fortran external I/O units10 11#ifndef FLANG_RT_RUNTIME_UNIT_H_12#define FLANG_RT_RUNTIME_UNIT_H_13 14#include "flang-rt/runtime/buffer.h"15#include "flang-rt/runtime/connection.h"16#include "flang-rt/runtime/environment.h"17#include "flang-rt/runtime/file.h"18#include "flang-rt/runtime/format.h"19#include "flang-rt/runtime/io-error.h"20#include "flang-rt/runtime/io-stmt.h"21#include "flang-rt/runtime/lock.h"22#include "flang-rt/runtime/memory.h"23#include "flang-rt/runtime/terminator.h"24#include "flang/Common/constexpr-bitset.h"25#include "flang/Common/optional.h"26#include <cstdlib>27#include <cstring>28#include <flang/Common/variant.h>29 30namespace Fortran::runtime::io {31 32class UnitMap;33class ChildIo;34class ExternalFileUnit;35 36enum FseekWhence {37 FseekSet = 0,38 FseekCurrent = 1,39 FseekEnd = 2,40};41 42RT_OFFLOAD_VAR_GROUP_BEGIN43// Predefined file units.44extern RT_VAR_ATTRS ExternalFileUnit *defaultInput; // unit 545extern RT_VAR_ATTRS ExternalFileUnit *defaultOutput; // unit 646extern RT_VAR_ATTRS ExternalFileUnit *errorOutput; // unit 0 extension47RT_OFFLOAD_VAR_GROUP_END48 49RT_OFFLOAD_API_GROUP_BEGIN50 51#if defined(RT_USE_PSEUDO_FILE_UNIT)52// A flavor of OpenFile class that pretends to be a terminal,53// and only provides basic buffering of the output54// in an internal buffer, and Write's the output55// using std::printf(). Since it does not rely on file system56// APIs, it can be used to implement external output57// for offload devices.58class PseudoOpenFile {59public:60 using FileOffset = std::int64_t;61 62 RT_API_ATTRS int fd() const { return 1 /*stdout*/; }63 RT_API_ATTRS const char *path() const { return nullptr; }64 RT_API_ATTRS std::size_t pathLength() const { return 0; }65 RT_API_ATTRS void set_path(OwningPtr<char> &&, std::size_t bytes) {}66 RT_API_ATTRS bool mayRead() const { return false; }67 RT_API_ATTRS bool mayWrite() const { return true; }68 RT_API_ATTRS bool mayPosition() const { return false; }69 RT_API_ATTRS bool mayAsynchronous() const { return false; }70 RT_API_ATTRS void set_mayAsynchronous(bool yes);71 // Pretend to be a terminal to force the output72 // at the end of IO statement.73 RT_API_ATTRS bool isTerminal() const { return true; }74 RT_API_ATTRS bool isWindowsTextFile() const { return false; }75 RT_API_ATTRS common::optional<FileOffset> knownSize() const;76 RT_API_ATTRS bool IsConnected() const { return false; }77 RT_API_ATTRS void Open(78 OpenStatus, common::optional<Action>, Position, IoErrorHandler &);79 RT_API_ATTRS void Predefine(int fd) {}80 RT_API_ATTRS void Close(CloseStatus, IoErrorHandler &);81 RT_API_ATTRS std::size_t Read(FileOffset, char *, std::size_t minBytes,82 std::size_t maxBytes, IoErrorHandler &);83 RT_API_ATTRS std::size_t Write(84 FileOffset, const char *, std::size_t, IoErrorHandler &);85 RT_API_ATTRS void Truncate(FileOffset, IoErrorHandler &);86 RT_API_ATTRS int ReadAsynchronously(87 FileOffset, char *, std::size_t, IoErrorHandler &);88 RT_API_ATTRS int WriteAsynchronously(89 FileOffset, const char *, std::size_t, IoErrorHandler &);90 RT_API_ATTRS void Wait(int id, IoErrorHandler &);91 RT_API_ATTRS void WaitAll(IoErrorHandler &);92 RT_API_ATTRS Position InquirePosition(FileOffset) const;93};94#endif // defined(RT_USE_PSEUDO_FILE_UNIT)95 96#if !defined(RT_USE_PSEUDO_FILE_UNIT)97using OpenFileClass = OpenFile;98using FileFrameClass = FileFrame<ExternalFileUnit>;99#else // defined(RT_USE_PSEUDO_FILE_UNIT)100using OpenFileClass = PseudoOpenFile;101// Use not so big buffer for the pseudo file unit frame.102using FileFrameClass = FileFrame<ExternalFileUnit, 1024>;103#endif // defined(RT_USE_PSEUDO_FILE_UNIT)104 105class ExternalFileUnit : public ConnectionState,106 public OpenFileClass,107 public FileFrameClass {108public:109 static constexpr int maxAsyncIds{64 * 16};110 111 explicit RT_API_ATTRS ExternalFileUnit(int unitNumber)112 : unitNumber_{unitNumber} {113 isUTF8 = executionEnvironment.defaultUTF8;114 for (int j{0}; 64 * j < maxAsyncIds; ++j) {115 asyncIdAvailable_[j].set();116 }117 asyncIdAvailable_[0].reset(0);118 }119 RT_API_ATTRS ~ExternalFileUnit() {}120 121 RT_API_ATTRS int unitNumber() const { return unitNumber_; }122 RT_API_ATTRS bool swapEndianness() const { return swapEndianness_; }123 RT_API_ATTRS bool createdForInternalChildIo() const {124 return createdForInternalChildIo_;125 }126 127 static RT_API_ATTRS ExternalFileUnit *LookUp(int unit);128 static RT_API_ATTRS ExternalFileUnit *LookUpOrCreate(129 int unit, const Terminator &, bool &wasExtant);130 static RT_API_ATTRS ExternalFileUnit *LookUpOrCreateAnonymous(int unit,131 Direction, common::optional<bool> isUnformatted, IoErrorHandler &);132 static RT_API_ATTRS ExternalFileUnit *LookUp(133 const char *path, std::size_t pathLen);134 static RT_API_ATTRS ExternalFileUnit &CreateNew(int unit, const Terminator &);135 static RT_API_ATTRS ExternalFileUnit *LookUpForClose(int unit);136 static RT_API_ATTRS ExternalFileUnit &NewUnit(137 const Terminator &, bool forChildIo);138 static RT_API_ATTRS void CloseAll(IoErrorHandler &);139 static RT_API_ATTRS void FlushAll(IoErrorHandler &);140 141 // Returns true if an existing unit was closed142 RT_API_ATTRS bool OpenUnit(common::optional<OpenStatus>,143 common::optional<Action>, Position, OwningPtr<char> &&path,144 std::size_t pathLength, Convert, IoErrorHandler &);145 RT_API_ATTRS bool OpenAnonymousUnit(common::optional<OpenStatus>,146 common::optional<Action>, Position, Convert, IoErrorHandler &);147 RT_API_ATTRS void CloseUnit(CloseStatus, IoErrorHandler &);148 RT_API_ATTRS void DestroyClosed();149 150 RT_API_ATTRS Iostat SetDirection(Direction);151 152 template <typename A, typename... X>153 RT_API_ATTRS IoStatementState &BeginIoStatement(154 const Terminator &terminator, X &&...xs) {155 // Take lock_ and hold it until EndIoStatement().156#if USE_PTHREADS157 if (!lock_.TakeIfNoDeadlock()) {158 terminator.Crash("Recursive I/O attempted on unit %d", unitNumber_);159 }160#else161 lock_.Take();162#endif163 A &state{u_.emplace<A>(std::forward<X>(xs)...)};164 directAccessRecWasSet_ = false;165 io_.emplace(state);166 return *io_;167 }168 169 RT_API_ATTRS bool Emit(170 const char *, std::size_t, std::size_t elementBytes, IoErrorHandler &);171 RT_API_ATTRS bool Receive(172 char *, std::size_t, std::size_t elementBytes, IoErrorHandler &);173 RT_API_ATTRS std::size_t GetNextInputBytes(const char *&, IoErrorHandler &);174 RT_API_ATTRS std::size_t ViewBytesInRecord(const char *&, bool forward) const;175 RT_API_ATTRS bool BeginReadingRecord(IoErrorHandler &);176 RT_API_ATTRS void FinishReadingRecord(IoErrorHandler &);177 RT_API_ATTRS bool AdvanceRecord(IoErrorHandler &);178 RT_API_ATTRS void BackspaceRecord(IoErrorHandler &);179 RT_API_ATTRS void FlushOutput(IoErrorHandler &);180 RT_API_ATTRS void FlushIfTerminal(IoErrorHandler &);181 RT_API_ATTRS void Endfile(IoErrorHandler &);182 RT_API_ATTRS void Rewind(IoErrorHandler &);183 RT_API_ATTRS void EndIoStatement();184 RT_API_ATTRS bool SetStreamPos(std::int64_t oneBasedPos, IoErrorHandler &);185 RT_API_ATTRS bool Fseek(186 std::int64_t zeroBasedPos, enum FseekWhence, IoErrorHandler &);187 RT_API_ATTRS bool SetDirectRec(188 std::int64_t, IoErrorHandler &); // one-based, for REC=189 RT_API_ATTRS std::int64_t InquirePos() const {190 // 12.6.2.11 defines POS=1 as the beginning of file191 return frameOffsetInFile_ + recordOffsetInFrame_ + positionInRecord + 1;192 }193 194 RT_API_ATTRS ChildIo *GetChildIo() { return child_.get(); }195 RT_API_ATTRS ChildIo &PushChildIo(IoStatementState &);196 RT_API_ATTRS void PopChildIo(ChildIo &);197 198 RT_API_ATTRS int GetAsynchronousId(IoErrorHandler &);199 RT_API_ATTRS bool Wait(int);200 RT_API_ATTRS Position InquirePosition() const {201 return OpenFileClass::InquirePosition(202 static_cast<std::int64_t>(frameOffsetInFile_ + recordOffsetInFrame_));203 }204 205private:206 static RT_API_ATTRS UnitMap &CreateUnitMap();207 static RT_API_ATTRS UnitMap &GetUnitMap();208 RT_API_ATTRS const char *FrameNextInput(IoErrorHandler &, std::size_t);209 RT_API_ATTRS void SetPosition(std::int64_t zeroBasedPos);210 RT_API_ATTRS void Sought(std::int64_t zeroBasedPos);211 RT_API_ATTRS void BeginSequentialVariableUnformattedInputRecord(212 IoErrorHandler &);213 RT_API_ATTRS void BeginVariableFormattedInputRecord(IoErrorHandler &);214 RT_API_ATTRS void BackspaceFixedRecord(IoErrorHandler &);215 RT_API_ATTRS void BackspaceVariableUnformattedRecord(IoErrorHandler &);216 RT_API_ATTRS void BackspaceVariableFormattedRecord(IoErrorHandler &);217 RT_API_ATTRS bool SetVariableFormattedRecordLength();218 RT_API_ATTRS void DoImpliedEndfile(IoErrorHandler &);219 template <bool ANY_DIR = true, Direction DIR = Direction::Output>220 RT_API_ATTRS void DoEndfile(IoErrorHandler &);221 RT_API_ATTRS void CommitWrites();222 RT_API_ATTRS bool CheckDirectAccess(IoErrorHandler &);223 RT_API_ATTRS void HitEndOnRead(IoErrorHandler &);224 RT_API_ATTRS std::uint32_t ReadHeaderOrFooter(std::int64_t frameOffset);225 226 Lock lock_;227 228 int unitNumber_{-1};229 Direction direction_{Direction::Output};230 bool impliedEndfile_{false}; // sequential/stream output has taken place231 bool beganReadingRecord_{false};232 bool anyWriteSinceLastPositioning_{false};233 bool directAccessRecWasSet_{false}; // REC= appeared234 // Subtle: The beginning of the frame can't be allowed to advance235 // during a single list-directed READ due to the possibility of a236 // multi-record CHARACTER value with a "r*" repeat count. So we237 // manage the frame and the current record therein separately.238 std::int64_t frameOffsetInFile_{0};239 std::size_t recordOffsetInFrame_{0}; // of currentRecordNumber240 bool swapEndianness_{false};241 bool createdForInternalChildIo_{false};242 common::BitSet<64> asyncIdAvailable_[maxAsyncIds / 64];243 244 // When a synchronous I/O statement is in progress on this unit, holds its245 // state.246 std::variant<std::monostate, OpenStatementState, CloseStatementState,247 ExternalFormattedIoStatementState<Direction::Output>,248 ExternalFormattedIoStatementState<Direction::Input>,249 ExternalListIoStatementState<Direction::Output>,250 ExternalListIoStatementState<Direction::Input>,251 ExternalUnformattedIoStatementState<Direction::Output>,252 ExternalUnformattedIoStatementState<Direction::Input>, InquireUnitState,253 ExternalMiscIoStatementState, ErroneousIoStatementState>254 u_;255 256 // Points to the active alternative (if any) in u_ for use as a Cookie257 common::optional<IoStatementState> io_;258 259 // A stack of child I/O pseudo-units for defined I/O that have this260 // unit number.261 OwningPtr<ChildIo> child_;262};263 264// A pseudo-unit for child I/O statements in defined I/O subroutines;265// it forwards operations to the parent I/O statement, which might also266// be a child I/O statement.267class ChildIo {268public:269 RT_API_ATTRS ChildIo(IoStatementState &parent, OwningPtr<ChildIo> &&previous)270 : parent_{parent}, previous_{std::move(previous)} {}271 272 RT_API_ATTRS IoStatementState &parent() const { return parent_; }273 274 RT_API_ATTRS void EndIoStatement();275 276 template <typename A, typename... X>277 RT_API_ATTRS IoStatementState &BeginIoStatement(X &&...xs) {278 A &state{u_.emplace<A>(std::forward<X>(xs)...)};279 io_.emplace(state);280 return *io_;281 }282 283 RT_API_ATTRS OwningPtr<ChildIo> AcquirePrevious() {284 return std::move(previous_);285 }286 287 RT_API_ATTRS Iostat CheckFormattingAndDirection(bool unformatted, Direction);288 289private:290 IoStatementState &parent_;291 OwningPtr<ChildIo> previous_;292 std::variant<std::monostate,293 ChildFormattedIoStatementState<Direction::Output>,294 ChildFormattedIoStatementState<Direction::Input>,295 ChildListIoStatementState<Direction::Output>,296 ChildListIoStatementState<Direction::Input>,297 ChildUnformattedIoStatementState<Direction::Output>,298 ChildUnformattedIoStatementState<Direction::Input>, InquireUnitState,299 ErroneousIoStatementState, ExternalMiscIoStatementState>300 u_;301 common::optional<IoStatementState> io_;302};303 304RT_OFFLOAD_API_GROUP_END305 306} // namespace Fortran::runtime::io307#endif // FLANG_RT_RUNTIME_UNIT_H_308