stop.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Implementation of the STOP statement.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. /* Fortran 2008 demands: If any exception (14) is signaling on that image, the
  27. processor shall issue a warning indicating which exceptions are signaling;
  28. this warning shall be on the unit identified by the named constant
  29. ERROR_UNIT (13.8.2.8). In line with other compilers, we do not report
  30. inexact - and we optionally ignore underflow, cf. thread starting at
  31. http://mailman.j3-fortran.org/pipermail/j3/2013-June/006452.html. */
  32. static void
  33. report_exception (void)
  34. {
  35. int set_excepts;
  36. if (!compile_options.fpe_summary)
  37. return;
  38. set_excepts = get_fpu_except_flags ();
  39. if ((set_excepts & compile_options.fpe_summary) == 0)
  40. return;
  41. estr_write ("Note: The following floating-point exceptions are signalling:");
  42. if ((compile_options.fpe_summary & GFC_FPE_INVALID)
  43. && (set_excepts & GFC_FPE_INVALID))
  44. estr_write (" IEEE_INVALID_FLAG");
  45. if ((compile_options.fpe_summary & GFC_FPE_ZERO)
  46. && (set_excepts & GFC_FPE_ZERO))
  47. estr_write (" IEEE_DIVIDE_BY_ZERO");
  48. if ((compile_options.fpe_summary & GFC_FPE_OVERFLOW)
  49. && (set_excepts & GFC_FPE_OVERFLOW))
  50. estr_write (" IEEE_OVERFLOW_FLAG");
  51. if ((compile_options.fpe_summary & GFC_FPE_UNDERFLOW)
  52. && (set_excepts & GFC_FPE_UNDERFLOW))
  53. estr_write (" IEEE_UNDERFLOW_FLAG");
  54. if ((compile_options.fpe_summary & GFC_FPE_DENORMAL)
  55. && (set_excepts & GFC_FPE_DENORMAL))
  56. estr_write (" IEEE_DENORMAL");
  57. if ((compile_options.fpe_summary & GFC_FPE_INEXACT)
  58. && (set_excepts & GFC_FPE_INEXACT))
  59. estr_write (" IEEE_INEXACT_FLAG");
  60. estr_write ("\n");
  61. }
  62. /* A numeric STOP statement. */
  63. extern _Noreturn void stop_numeric (GFC_INTEGER_4);
  64. export_proto(stop_numeric);
  65. void
  66. stop_numeric (GFC_INTEGER_4 code)
  67. {
  68. report_exception ();
  69. if (code == -1)
  70. code = 0;
  71. else
  72. st_printf ("STOP %d\n", (int)code);
  73. exit (code);
  74. }
  75. /* A Fortran 2008 numeric STOP statement. */
  76. extern _Noreturn void stop_numeric_f08 (GFC_INTEGER_4);
  77. export_proto(stop_numeric_f08);
  78. void
  79. stop_numeric_f08 (GFC_INTEGER_4 code)
  80. {
  81. report_exception ();
  82. st_printf ("STOP %d\n", (int)code);
  83. exit (code);
  84. }
  85. /* A character string or blank STOP statement. */
  86. void
  87. stop_string (const char *string, GFC_INTEGER_4 len)
  88. {
  89. report_exception ();
  90. if (string)
  91. {
  92. estr_write ("STOP ");
  93. (void) write (STDERR_FILENO, string, len);
  94. estr_write ("\n");
  95. }
  96. exit (0);
  97. }
  98. /* Per Fortran 2008, section 8.4: "Execution of a STOP statement initiates
  99. normal termination of execution. Execution of an ERROR STOP statement
  100. initiates error termination of execution." Thus, error_stop_string returns
  101. a nonzero exit status code. */
  102. extern _Noreturn void error_stop_string (const char *, GFC_INTEGER_4);
  103. export_proto(error_stop_string);
  104. void
  105. error_stop_string (const char *string, GFC_INTEGER_4 len)
  106. {
  107. report_exception ();
  108. estr_write ("ERROR STOP ");
  109. (void) write (STDERR_FILENO, string, len);
  110. estr_write ("\n");
  111. exit (1);
  112. }
  113. /* A numeric ERROR STOP statement. */
  114. extern _Noreturn void error_stop_numeric (GFC_INTEGER_4);
  115. export_proto(error_stop_numeric);
  116. void
  117. error_stop_numeric (GFC_INTEGER_4 code)
  118. {
  119. report_exception ();
  120. st_printf ("ERROR STOP %d\n", (int) code);
  121. exit (code);
  122. }