Debian Codegraph @

libgfortran/runtime/stop.c

raw
1 /* Implementation of the STOP statement.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook <[email protected]>
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
6
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.
11
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.
16
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.
20
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/>. */
25
26 #include "libgfortran.h"
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #include <string.h>
33
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. */
40
41 extern void report_exception (void);
42 iexport_proto (report_exception);
43
44 void
45 report_exception (void)
46 {
47 struct iovec iov[8];
48 int set_excepts, iovcnt = 1;
49
50 if (!compile_options.fpe_summary)
51 return;
52
53 set_excepts = get_fpu_except_flags ();
54 if ((set_excepts & compile_options.fpe_summary) == 0)
55 return;
56
57 iov[0].iov_base = (char*) "Note: The following floating-point exceptions are signalling:";
58 iov[0].iov_len = strlen (iov[0].iov_base);
59
60 if ((compile_options.fpe_summary & GFC_FPE_INVALID)
61 && (set_excepts & GFC_FPE_INVALID))
62 {
63 iov[iovcnt].iov_base = (char*) " IEEE_INVALID_FLAG";
64 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
65 iovcnt++;
66 }
67
68 if ((compile_options.fpe_summary & GFC_FPE_ZERO)
69 && (set_excepts & GFC_FPE_ZERO))
70 {
71 iov[iovcnt].iov_base = (char*) " IEEE_DIVIDE_BY_ZERO";
72 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
73 iovcnt++;
74 }
75
76 if ((compile_options.fpe_summary & GFC_FPE_OVERFLOW)
77 && (set_excepts & GFC_FPE_OVERFLOW))
78 {
79 iov[iovcnt].iov_base = (char*) " IEEE_OVERFLOW_FLAG";
80 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
81 iovcnt++;
82 }
83
84 if ((compile_options.fpe_summary & GFC_FPE_UNDERFLOW)
85 && (set_excepts & GFC_FPE_UNDERFLOW))
86 {
87 iov[iovcnt].iov_base = (char*) " IEEE_UNDERFLOW_FLAG";
88 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
89 iovcnt++;
90 }
91
92 if ((compile_options.fpe_summary & GFC_FPE_DENORMAL)
93 && (set_excepts & GFC_FPE_DENORMAL))
94 {
95 iov[iovcnt].iov_base = (char*) " IEEE_DENORMAL";
96 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
97 iovcnt++;
98 }
99
100 if ((compile_options.fpe_summary & GFC_FPE_INEXACT)
101 && (set_excepts & GFC_FPE_INEXACT))
102 {
103 iov[iovcnt].iov_base = (char*) " IEEE_INEXACT_FLAG";
104 iov[iovcnt].iov_len = strlen (iov[iovcnt].iov_base);
105 iovcnt++;
106 }
107
108 iov[iovcnt].iov_base = (char*) "\n";
109 iov[iovcnt].iov_len = 1;
110 iovcnt++;
111
112 estr_writev (iov, iovcnt);
113 }
114 iexport (report_exception);
115
116 /* A numeric STOP statement. */
117
118 extern _Noreturn void stop_numeric (int, bool);
119 export_proto(stop_numeric);
120
121 void
122 stop_numeric (int code, bool quiet)
123 {
124 if (!quiet)
125 {
126 report_exception ();
127 st_printf ("STOP %d\n", code);
128 }
129 exit (code);
130 }
131
132
133 /* A character string or blank STOP statement. */
134
135 void
136 stop_string (const char *string, size_t len, bool quiet)
137 {
138 if (!quiet)
139 {
140 report_exception ();
141 if (string)
142 {
143 struct iovec iov[3];
144 iov[0].iov_base = (char*) "STOP ";
145 iov[0].iov_len = strlen (iov[0].iov_base);
146 iov[1].iov_base = (char*) string;
147 iov[1].iov_len = len;
148 iov[2].iov_base = (char*) "\n";
149 iov[2].iov_len = 1;
150 estr_writev (iov, 3);
151 }
152 }
153 exit (0);
154 }
155
156
157 /* Per Fortran 2008, section 8.4: "Execution of a STOP statement initiates
158 normal termination of execution. Execution of an ERROR STOP statement
159 initiates error termination of execution." Thus, error_stop_string returns
160 a nonzero exit status code. */
161
162 extern _Noreturn void error_stop_string (const char *, size_t, bool);
163 export_proto(error_stop_string);
164
165 void
166 error_stop_string (const char *string, size_t len, bool quiet)
167 {
168 if (!quiet)
169 {
170 struct iovec iov[3];
171 report_exception ();
172 iov[0].iov_base = (char*) "ERROR STOP ";
173 iov[0].iov_len = strlen (iov[0].iov_base);
174 iov[1].iov_base = (char*) string;
175 iov[1].iov_len = len;
176 iov[2].iov_base = (char*) "\n";
177 iov[2].iov_len = 1;
178 estr_writev (iov, 3);
179 }
180 exit_error (1);
181 }
182
183
184 /* A numeric ERROR STOP statement. */
185
186 extern _Noreturn void error_stop_numeric (int, bool);
187 export_proto(error_stop_numeric);
188
189 void
190 error_stop_numeric (int code, bool quiet)
191 {
192 if (!quiet)
193 {
194 report_exception ();
195 st_printf ("ERROR STOP %d\n", code);
196 }
197 exit_error (code);
198 }