attribs: Namespace-aware lookup_attribute_spec
[official-gcc.git] / libgfortran / runtime / stop.c
blob19a0e5e9bc59ea863d39d5b9aa2f8ef4e8f492c6
1 /* Implementation of the STOP statement.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
32 #include <string.h>
34 /* Fortran 2008 demands: If any exception (14) is signaling on that image, the
35 processor shall issue a warning indicating which exceptions are signaling;
36 this warning shall be on the unit identified by the named constant
37 ERROR_UNIT (13.8.2.8). In line with other compilers, we do not report
38 inexact - and we optionally ignore underflow, cf. thread starting at
39 http://mailman.j3-fortran.org/pipermail/j3/2013-June/006452.html. */
41 static void
42 report_exception (void)
44 struct iovec iov[8];
45 int set_excepts, iovcnt = 1;
47 if (!compile_options.fpe_summary)
48 return;
50 set_excepts = get_fpu_except_flags ();
51 if ((set_excepts & compile_options.fpe_summary) == 0)
52 return;
54 iov[0].iov_base = (char*) "Note: The following floating-point exceptions are signalling:";
55 iov[0].iov_len = strlen (iov[0].iov_base);
57 if ((compile_options.fpe_summary & GFC_FPE_INVALID)
58 && (set_excepts & GFC_FPE_INVALID))
60 iov[iovcnt].iov_base = (char*) " IEEE_INVALID_FLAG";
61 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
62 iovcnt++;
65 if ((compile_options.fpe_summary & GFC_FPE_ZERO)
66 && (set_excepts & GFC_FPE_ZERO))
68 iov[iovcnt].iov_base = (char*) " IEEE_DIVIDE_BY_ZERO";
69 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
70 iovcnt++;
73 if ((compile_options.fpe_summary & GFC_FPE_OVERFLOW)
74 && (set_excepts & GFC_FPE_OVERFLOW))
76 iov[iovcnt].iov_base = (char*) " IEEE_OVERFLOW_FLAG";
77 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
78 iovcnt++;
81 if ((compile_options.fpe_summary & GFC_FPE_UNDERFLOW)
82 && (set_excepts & GFC_FPE_UNDERFLOW))
84 iov[iovcnt].iov_base = (char*) " IEEE_UNDERFLOW_FLAG";
85 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
86 iovcnt++;
89 if ((compile_options.fpe_summary & GFC_FPE_DENORMAL)
90 && (set_excepts & GFC_FPE_DENORMAL))
92 iov[iovcnt].iov_base = (char*) " IEEE_DENORMAL";
93 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
94 iovcnt++;
97 if ((compile_options.fpe_summary & GFC_FPE_INEXACT)
98 && (set_excepts & GFC_FPE_INEXACT))
100 iov[iovcnt].iov_base = (char*) " IEEE_INEXACT_FLAG";
101 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
102 iovcnt++;
105 iov[iovcnt].iov_base = (char*) "\n";
106 iov[iovcnt].iov_len = 1;
107 iovcnt++;
109 estr_writev (iov, iovcnt);
113 /* A numeric STOP statement. */
115 extern _Noreturn void stop_numeric (int, bool);
116 export_proto(stop_numeric);
118 void
119 stop_numeric (int code, bool quiet)
121 if (!quiet)
123 report_exception ();
124 st_printf ("STOP %d\n", code);
126 exit (code);
130 /* A character string or blank STOP statement. */
132 void
133 stop_string (const char *string, size_t len, bool quiet)
135 if (!quiet)
137 report_exception ();
138 if (string)
140 struct iovec iov[3];
141 iov[0].iov_base = (char*) "STOP ";
142 iov[0].iov_len = strlen (iov[0].iov_base);
143 iov[1].iov_base = (char*) string;
144 iov[1].iov_len = len;
145 iov[2].iov_base = (char*) "\n";
146 iov[2].iov_len = 1;
147 estr_writev (iov, 3);
150 exit (0);
154 /* Per Fortran 2008, section 8.4: "Execution of a STOP statement initiates
155 normal termination of execution. Execution of an ERROR STOP statement
156 initiates error termination of execution." Thus, error_stop_string returns
157 a nonzero exit status code. */
159 extern _Noreturn void error_stop_string (const char *, size_t, bool);
160 export_proto(error_stop_string);
162 void
163 error_stop_string (const char *string, size_t len, bool quiet)
165 if (!quiet)
167 struct iovec iov[3];
168 report_exception ();
169 iov[0].iov_base = (char*) "ERROR STOP ";
170 iov[0].iov_len = strlen (iov[0].iov_base);
171 iov[1].iov_base = (char*) string;
172 iov[1].iov_len = len;
173 iov[2].iov_base = (char*) "\n";
174 iov[2].iov_len = 1;
175 estr_writev (iov, 3);
177 exit_error (1);
181 /* A numeric ERROR STOP statement. */
183 extern _Noreturn void error_stop_numeric (int, bool);
184 export_proto(error_stop_numeric);
186 void
187 error_stop_numeric (int code, bool quiet)
189 if (!quiet)
191 report_exception ();
192 st_printf ("ERROR STOP %d\n", code);
194 exit_error (code);