2020-01-27 19:18:45 -07:00
|
|
|
//===-- runtime/stop.cpp ----------------------------------------*- C++ -*-===//
|
2020-01-08 13: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-10 12:12:03 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-01-08 13:27:32 -08:00
|
|
|
|
|
|
|
|
#include "stop.h"
|
2020-07-04 11:06:28 -07:00
|
|
|
#include "file.h"
|
2020-01-23 16:59:27 -08:00
|
|
|
#include "io-error.h"
|
2020-01-08 13:27:32 -08:00
|
|
|
#include "terminator.h"
|
2020-01-23 16:59:27 -08:00
|
|
|
#include "unit.h"
|
2020-01-08 13:27:32 -08:00
|
|
|
#include <cfenv>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
static void DescribeIEEESignaledExceptions() {
|
2020-03-28 21:00:16 -07:00
|
|
|
#ifdef fetestexcept // a macro in some environments; omit std::
|
2020-01-09 14:01:40 -08:00
|
|
|
auto excepts{fetestexcept(FE_ALL_EXCEPT)};
|
|
|
|
|
#else
|
|
|
|
|
auto excepts{std::fetestexcept(FE_ALL_EXCEPT)};
|
|
|
|
|
#endif
|
|
|
|
|
if (excepts) {
|
2020-01-08 13: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-17 11:17:39 -07:00
|
|
|
std::fputc('\n', stderr);
|
2020-01-08 13:27:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 12:19:49 -07:00
|
|
|
static void CloseAllExternalUnits(const char *why) {
|
|
|
|
|
Fortran::runtime::io::IoErrorHandler handler{why};
|
|
|
|
|
Fortran::runtime::io::ExternalFileUnit::CloseAll(handler);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 13:27:32 -08:00
|
|
|
[[noreturn]] void RTNAME(StopStatement)(
|
|
|
|
|
int code, bool isErrorStop, bool quiet) {
|
2020-06-18 12:19:49 -07:00
|
|
|
CloseAllExternalUnits("STOP statement");
|
2020-01-08 13:27:32 -08:00
|
|
|
if (!quiet) {
|
2020-07-17 11:17:39 -07:00
|
|
|
std::fprintf(stderr, "Fortran %s", isErrorStop ? "ERROR STOP" : "STOP");
|
2020-01-08 13:27:32 -08:00
|
|
|
if (code != EXIT_SUCCESS) {
|
2020-07-17 11:17:39 -07:00
|
|
|
std::fprintf(stderr, ": code %d\n", code);
|
2020-01-08 13:27:32 -08:00
|
|
|
}
|
2020-07-17 11:17:39 -07:00
|
|
|
std::fputc('\n', stderr);
|
2020-01-08 13:27:32 -08:00
|
|
|
DescribeIEEESignaledExceptions();
|
|
|
|
|
}
|
|
|
|
|
std::exit(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void RTNAME(StopStatementText)(
|
|
|
|
|
const char *code, bool isErrorStop, bool quiet) {
|
2020-06-18 12:19:49 -07:00
|
|
|
CloseAllExternalUnits("STOP statement");
|
2020-01-08 13:27:32 -08:00
|
|
|
if (!quiet) {
|
2020-01-09 08:10:57 -08:00
|
|
|
std::fprintf(
|
|
|
|
|
stderr, "Fortran %s: %s\n", isErrorStop ? "ERROR STOP" : "STOP", code);
|
2020-01-08 13:27:32 -08:00
|
|
|
DescribeIEEESignaledExceptions();
|
|
|
|
|
}
|
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 11:06:28 -07:00
|
|
|
void RTNAME(PauseStatement)() {
|
|
|
|
|
if (Fortran::runtime::io::IsATerminal(0)) {
|
|
|
|
|
Fortran::runtime::io::IoErrorHandler handler{"PAUSE statement"};
|
|
|
|
|
Fortran::runtime::io::ExternalFileUnit::FlushAll(handler);
|
|
|
|
|
std::fputs("Fortran PAUSE: hit RETURN to continue:", stderr);
|
|
|
|
|
std::fflush(nullptr);
|
|
|
|
|
if (std::fgetc(stdin) == EOF) {
|
|
|
|
|
CloseAllExternalUnits("PAUSE statement");
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 13:27:32 -08:00
|
|
|
[[noreturn]] void RTNAME(FailImageStatement)() {
|
|
|
|
|
Fortran::runtime::NotifyOtherImagesOfFailImageStatement();
|
2020-06-18 12:19:49 -07:00
|
|
|
CloseAllExternalUnits("FAIL IMAGE statement");
|
2020-01-08 13:27:32 -08:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2020-01-23 16:59:27 -08:00
|
|
|
|
|
|
|
|
[[noreturn]] void RTNAME(ProgramEndStatement)() {
|
2020-06-18 12:19:49 -07:00
|
|
|
CloseAllExternalUnits("END statement");
|
2020-01-23 16:59:27 -08:00
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
2020-01-08 13:27:32 -08:00
|
|
|
}
|