2021-09-02 16:14:01 +08:00
|
|
|
//===-- runtime/stop.cpp --------------------------------------------------===//
|
2020-01-09 05:27:32 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-01-09 05:27:32 +08:00
|
|
|
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/stop.h"
|
2021-11-11 03:55:46 +08:00
|
|
|
#include "environment.h"
|
2020-07-05 02:06:28 +08:00
|
|
|
#include "file.h"
|
2020-01-24 08:59:27 +08:00
|
|
|
#include "io-error.h"
|
2020-01-09 05:27:32 +08:00
|
|
|
#include "terminator.h"
|
2020-01-24 08:59:27 +08:00
|
|
|
#include "unit.h"
|
2020-01-09 05:27:32 +08:00
|
|
|
#include <cfenv>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
static void DescribeIEEESignaledExceptions() {
|
2020-03-29 12:00:16 +08:00
|
|
|
#ifdef fetestexcept // a macro in some environments; omit std::
|
2020-01-10 06:01:40 +08:00
|
|
|
auto excepts{fetestexcept(FE_ALL_EXCEPT)};
|
|
|
|
#else
|
|
|
|
auto excepts{std::fetestexcept(FE_ALL_EXCEPT)};
|
|
|
|
#endif
|
|
|
|
if (excepts) {
|
2020-01-09 05:27:32 +08:00
|
|
|
std::fputs("IEEE arithmetic exceptions signaled:", stderr);
|
|
|
|
if (excepts & FE_DIVBYZERO) {
|
|
|
|
std::fputs(" DIVBYZERO", stderr);
|
|
|
|
}
|
|
|
|
if (excepts & FE_INEXACT) {
|
|
|
|
std::fputs(" INEXACT", stderr);
|
|
|
|
}
|
|
|
|
if (excepts & FE_INVALID) {
|
|
|
|
std::fputs(" INVALID", stderr);
|
|
|
|
}
|
|
|
|
if (excepts & FE_OVERFLOW) {
|
|
|
|
std::fputs(" OVERFLOW", stderr);
|
|
|
|
}
|
|
|
|
if (excepts & FE_UNDERFLOW) {
|
|
|
|
std::fputs(" UNDERFLOW", stderr);
|
|
|
|
}
|
2020-07-18 02:17:39 +08:00
|
|
|
std::fputc('\n', stderr);
|
2020-01-09 05:27:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 03:19:49 +08:00
|
|
|
static void CloseAllExternalUnits(const char *why) {
|
|
|
|
Fortran::runtime::io::IoErrorHandler handler{why};
|
|
|
|
Fortran::runtime::io::ExternalFileUnit::CloseAll(handler);
|
|
|
|
}
|
|
|
|
|
2020-01-09 05:27:32 +08:00
|
|
|
[[noreturn]] void RTNAME(StopStatement)(
|
|
|
|
int code, bool isErrorStop, bool quiet) {
|
2020-06-19 03:19:49 +08:00
|
|
|
CloseAllExternalUnits("STOP statement");
|
2021-11-11 03:55:46 +08:00
|
|
|
if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) {
|
|
|
|
quiet = true;
|
|
|
|
}
|
2020-01-09 05:27:32 +08:00
|
|
|
if (!quiet) {
|
2020-07-18 02:17:39 +08:00
|
|
|
std::fprintf(stderr, "Fortran %s", isErrorStop ? "ERROR STOP" : "STOP");
|
2020-01-09 05:27:32 +08:00
|
|
|
if (code != EXIT_SUCCESS) {
|
2020-07-18 02:17:39 +08:00
|
|
|
std::fprintf(stderr, ": code %d\n", code);
|
2020-01-09 05:27:32 +08:00
|
|
|
}
|
2020-07-18 02:17:39 +08:00
|
|
|
std::fputc('\n', stderr);
|
2020-01-09 05:27:32 +08:00
|
|
|
DescribeIEEESignaledExceptions();
|
|
|
|
}
|
|
|
|
std::exit(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] void RTNAME(StopStatementText)(
|
2020-10-02 03:12:46 +08:00
|
|
|
const char *code, std::size_t length, bool isErrorStop, bool quiet) {
|
2020-06-19 03:19:49 +08:00
|
|
|
CloseAllExternalUnits("STOP statement");
|
2020-01-09 05:27:32 +08:00
|
|
|
if (!quiet) {
|
2021-11-19 01:06:19 +08:00
|
|
|
if (Fortran::runtime::executionEnvironment.noStopMessage && !isErrorStop) {
|
|
|
|
std::fprintf(stderr, "%.*s\n", static_cast<int>(length), code);
|
|
|
|
} else {
|
|
|
|
std::fprintf(stderr, "Fortran %s: %.*s\n",
|
|
|
|
isErrorStop ? "ERROR STOP" : "STOP", static_cast<int>(length), code);
|
|
|
|
}
|
2020-01-09 05:27:32 +08:00
|
|
|
DescribeIEEESignaledExceptions();
|
|
|
|
}
|
2021-11-19 01:06:19 +08:00
|
|
|
if (isErrorStop) {
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2020-01-09 05:27:32 +08:00
|
|
|
}
|
|
|
|
|
2020-10-02 03:12:46 +08:00
|
|
|
static bool StartPause() {
|
2020-07-05 02:06:28 +08:00
|
|
|
if (Fortran::runtime::io::IsATerminal(0)) {
|
|
|
|
Fortran::runtime::io::IoErrorHandler handler{"PAUSE statement"};
|
|
|
|
Fortran::runtime::io::ExternalFileUnit::FlushAll(handler);
|
2020-10-02 03:12:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EndPause() {
|
|
|
|
std::fflush(nullptr);
|
|
|
|
if (std::fgetc(stdin) == EOF) {
|
|
|
|
CloseAllExternalUnits("PAUSE statement");
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTNAME(PauseStatement)() {
|
|
|
|
if (StartPause()) {
|
2020-07-05 02:06:28 +08:00
|
|
|
std::fputs("Fortran PAUSE: hit RETURN to continue:", stderr);
|
2020-10-02 03:12:46 +08:00
|
|
|
EndPause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTNAME(PauseStatementInt)(int code) {
|
|
|
|
if (StartPause()) {
|
|
|
|
std::fprintf(stderr, "Fortran PAUSE %d: hit RETURN to continue:", code);
|
|
|
|
EndPause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
|
|
|
|
if (StartPause()) {
|
|
|
|
std::fprintf(stderr,
|
|
|
|
"Fortran PAUSE %.*s: hit RETURN to continue:", static_cast<int>(length),
|
|
|
|
code);
|
|
|
|
EndPause();
|
2020-07-05 02:06:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 05:27:32 +08:00
|
|
|
[[noreturn]] void RTNAME(FailImageStatement)() {
|
|
|
|
Fortran::runtime::NotifyOtherImagesOfFailImageStatement();
|
2020-06-19 03:19:49 +08:00
|
|
|
CloseAllExternalUnits("FAIL IMAGE statement");
|
2020-01-09 05:27:32 +08:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
|
|
|
[[noreturn]] void RTNAME(ProgramEndStatement)() {
|
2020-06-19 03:19:49 +08:00
|
|
|
CloseAllExternalUnits("END statement");
|
2020-01-24 08:59:27 +08:00
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2021-09-24 07:36:04 +08:00
|
|
|
|
|
|
|
[[noreturn]] void RTNAME(Exit)(int status) {
|
|
|
|
CloseAllExternalUnits("CALL EXIT()");
|
|
|
|
std::exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] void RTNAME(Abort)() { std::abort(); }
|
2022-01-29 05:21:29 +08:00
|
|
|
|
2022-02-01 23:38:10 +08:00
|
|
|
[[noreturn]] void RTNAME(ReportFatalUserError)(
|
2022-01-29 05:21:29 +08:00
|
|
|
const char *message, const char *source, int line) {
|
|
|
|
Fortran::runtime::Terminator{source, line}.Crash(message);
|
|
|
|
}
|
2020-01-09 05:27:32 +08:00
|
|
|
}
|