a68: Add exit function to POSIX prelude

Add the procedure `posixexit'.

gcc/algol68/ChangeLog:

	* a68-low-posix.cc (a68_posix_setexitstatus): Delete function.
	(a68_posix_exit): New function.
	* a68-low-prelude.cc (a68_lower_setexitstatus): Delete function.
	(a68_lower_posixexit): New function.
	* a68-low-runtime.def (SET_EXIT_STATUS): Delete definition.
	(POSIX_EXIT): Add definition for posixexit.
	* a68-parser-prelude.cc (posix_prelude): Remove setexitstatus
	identifier from and add posixexit identifier to standenv.
	* a68.h (a68_posix_setexitstatus): Delete prototype.
	(a68_lower_setexitstatus): Likewise.
	(a68_posix_exit): New prototype.
	(a68_lower_posixexit): Likewise.
	* ga68.texi:

libga68/ChangeLog:

	* ga68-posix.c (_libga68_posixexit): New function.
	* ga68.h (_libga68_posixexit): New prototype.
	(_libga68_set_exit_status): Delete prototype.
	* ga68.map: Remove _libga68_set_exit_status from and add
	_libga68_posixexit to the global map.
	* libga68.c: include <stdlib.h>.
	(_libga68_set_exit_status): Delete function.
	(main): Return EXIT_SUCCESS.

gcc/testsuite/ChangeLog:

	* algol68/execute/posix-exit-1.a68: New test.

Signed-off-by: Pietro Monteiro <pietro@sociotechnical.xyz>
This commit is contained in:
Pietro Monteiro
2026-01-09 20:52:13 -05:00
parent 1f7e64d56b
commit f2cdddef40
11 changed files with 46 additions and 49 deletions

View File

@@ -42,15 +42,6 @@
#include "a68.h"
/* Set the exit status of the running process, to be returned to the OS upon
exit. */
tree
a68_posix_setexitstatus (void)
{
return a68_get_libcall (A68_LIBCALL_SET_EXIT_STATUS);
}
/* Number of command line arguments passed to the program. */
tree
@@ -341,6 +332,12 @@ a68_posix_errno (void)
return a68_get_libcall (A68_LIBCALL_POSIX_ERRNO);
}
tree
a68_posix_exit (void)
{
return a68_get_libcall (A68_LIBCALL_POSIX_EXIT);
}
tree
a68_posix_perror (void)
{

View File

@@ -1902,15 +1902,6 @@ a68_lower_longlongrandom (NODE_T *p ATTRIBUTE_UNUSED, LOW_CTX_T ctx ATTRIBUTE_UN
/********* POSIX prelude. ***************/
tree
a68_lower_setexitstatus (NODE_T *p, LOW_CTX_T ctx ATTRIBUTE_UNUSED)
{
tree t = a68_posix_setexitstatus ();
if (CAN_HAVE_LOCATION_P (t))
SET_EXPR_LOCATION (t, a68_get_node_location (p));
return t;
}
tree
a68_lower_posixargc (NODE_T *p ATTRIBUTE_UNUSED, LOW_CTX_T ctx ATTRIBUTE_UNUSED)
{
@@ -2112,6 +2103,16 @@ a68_lower_posixerrno (NODE_T *p ATTRIBUTE_UNUSED,
return t;
}
tree
a68_lower_posixexit (NODE_T *p ATTRIBUTE_UNUSED,
LOW_CTX_T ctx ATTRIBUTE_UNUSED)
{
tree t = a68_posix_exit ();
if (CAN_HAVE_LOCATION_P (t))
SET_EXPR_LOCATION (t, a68_get_node_location (p));
return t;
}
tree
a68_lower_posixperror (NODE_T *p ATTRIBUTE_UNUSED,
LOW_CTX_T ctx ATTRIBUTE_UNUSED)

View File

@@ -42,7 +42,6 @@ along with GCC; see the file COPYING3. If not see
Used for declaring functions that are called by generated code. */
DEF_A68_RUNTIME (ASSERT, "_libga68_assert", RT(VOID), P2(CONSTCHARPTR, UINT), ECF_NORETURN)
DEF_A68_RUNTIME (SET_EXIT_STATUS, "_libga68_set_exit_status", RT(VOID), P1(INT), 0)
DEF_A68_RUNTIME (MALLOC, "_libga68_malloc", RT(VOIDPTR), P1(SIZE), ECF_NOTHROW | ECF_LEAF | ECF_MALLOC)
DEF_A68_RUNTIME (DEREFNIL, "_libga68_derefnil", RT(VOID), P2(CONSTCHARPTR, UINT), ECF_NORETURN)
DEF_A68_RUNTIME (UNREACHABLE, "_libga68_unreachable", RT(VOID), P2(CONSTCHARPTR, UINT), ECF_NORETURN)
@@ -78,6 +77,7 @@ DEF_A68_RUNTIME (POSIX_GETS, "_libga68_posixgets", RT(UNISTRPTR), P2(INT,SIZEPTR
DEF_A68_RUNTIME (POSIX_FGETS, "_libga68_posixfgets", RT(UNISTRPTR), P3(INT,INT,SIZEPTR), 0)
DEF_A68_RUNTIME (POSIX_GETENV, "_libga68_posixgetenv", RT(VOID), P5(UNISTR,SIZE,SIZE,UNISTRPTR,SIZEPTR), 0)
DEF_A68_RUNTIME (POSIX_ERRNO, "_libga68_posixerrno", RT(INT), P0(), 0)
DEF_A68_RUNTIME (POSIX_EXIT, "_libga68_posixexit", RT(VOID), P1(INT), 0)
DEF_A68_RUNTIME (POSIX_PERROR, "_libga68_posixperror", RT(VOID), P3(UNISTR,SIZE,SIZE), 0)
DEF_A68_RUNTIME (POSIX_STRERROR, "_libga68_posixstrerror", RT(UNISTRPTR), P2(INT, SIZEPTR), 0)
DEF_A68_RUNTIME (POSIX_LSEEK, "_libga68_posixlseek", RT(LONGLONGINT), P3(INT,LONGLONGINT,INT), 0)

View File

@@ -1378,7 +1378,7 @@ posix_prelude (void)
a68_idf (A68_EXT, "getenv", m, a68_lower_posixgetenv);
/* Exit status handling. */
m = a68_proc (M_VOID, M_INT, NO_MOID);
a68_idf (A68_EXT, "setexitstatus", m, a68_lower_setexitstatus);
a68_idf (A68_EXT, "posixexit", m, a68_lower_posixexit);
/* Argument handling. */
m = A68_MCACHE (proc_int);
a68_idf (A68_EXT, "argc", m, a68_lower_posixargc);

View File

@@ -578,7 +578,6 @@ tree a68_complex_widen_from_real (MOID_T *mode, tree r);
/* a68-low-posix.cc */
tree a68_posix_setexitstatus (void);
tree a68_posix_argc (void);
tree a68_posix_argv (void);
tree a68_posix_getenv (void);
@@ -591,6 +590,7 @@ tree a68_posix_fclose (void);
tree a68_posix_fsize (void);
tree a68_posix_lseek (void);
tree a68_posix_errno (void);
tree a68_posix_exit (void);
tree a68_posix_perror (void);
tree a68_posix_strerror (void);
tree a68_posix_getchar (void);
@@ -1067,7 +1067,6 @@ tree a68_lower_shortenreal2 (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_random (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_longrandom (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_longlongrandom (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_setexitstatus (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixargc (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixargv (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixputchar (NODE_T *p, LOW_CTX_T ctx);
@@ -1093,6 +1092,7 @@ tree a68_lower_posixfileordonly (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixfileowronly (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixfileotrunc (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixerrno (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixexit (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixperror (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixstrerror (NODE_T *p, LOW_CTX_T ctx);
tree a68_lower_posixgetchar (NODE_T *p, LOW_CTX_T ctx);

View File

@@ -1032,19 +1032,19 @@ Some operating systems have the notion of @dfn{exit status} of a
process. In such systems, by default the execution of the particular
program results in an exit status of success. It is possible for the
program to specify an explicit exit status by using the standard
procedure @code{set exit status}, like:
procedure @code{posix exit}, like:
@example
@b{begin} # ... program code ... #
@B{if} error found;
@B{then} set exit status (1) @B{fi}
@B{then} posix exit (1) @B{fi}
@b{end}
@end example
In POSIX systems the status is an integer, and the system interprets a
value other than zero as a run-time error. In other systems the
status may be of some other type. To support this, the @code{set
error status} procedure accepts as an argument an united value that
status may be of some other type. To support this, the @code{posix
exit} procedure accepts as an argument an united value that
accommodates all the supported systems.
The following example shows a very simple program that prints ``Hello
@@ -2894,17 +2894,15 @@ which is the default.
@node POSIX process
@section POSIX process
The Algol 68 program can report an exit status to the operating system
once they stop running. The exit status reported by default is zero,
which corresponds to success.
The Algol 68 program reports an exit status to the operating system once
it stops running. The exit status reported by default is zero, which
corresponds to success.
@deftypefn Procedure {} {set exit status} {= (@B{int} status)}
@deftypefn Procedure {} {posix exit} {= (@B{int} status)}
Procedure that sets the exit status to report to the operating system
once the program stop executing. The default exit status is 0 which,
by convention, is interpreted by POSIX systems as success. A value
different to zero is interpreted as an error status. This procedure
can be invoked more than one, the previous exit status being
overwritten.
and immediately stops executing the program. The default exit status is
0 which, by convention, is interpreted by POSIX systems as success. A
value different to zero is interpreted as an error status.
@end deftypefn
@node POSIX command line

View File

@@ -0,0 +1,3 @@
begin posix_exit (0);
assert (false) { if this assert triggers then posix_exit is broken }
end

View File

@@ -461,3 +461,11 @@ _libga68_posixlseek (int fd, long long int offset, int whence)
_libga68_errno = errno;
return ret;
}
/* Implementation of the posix prelude `exit'. */
void
_libga68_posixexit (int status)
{
exit (status);
}

View File

@@ -84,6 +84,7 @@ long double _libga68_longlongrandom (void);
/* ga68-posix.c */
int _libga68_posixerrno (void);
void _libga68_posixexit (int) __attribute__ ((__noreturn__));
void _libga68_posixperror (uint32_t *s, size_t len, size_t stride);
uint32_t *_libga68_posixstrerror (int errnum, size_t *len);
long long int _libga68_posixfsize (int fd);
@@ -125,6 +126,4 @@ uint32_t *_libga68_u8_to_u32 (const uint8_t *s, size_t n,
extern int _libga68_argc;
extern char **_libga68_argv;
void _libga68_set_exit_status (int status);
#endif /* ! GA68_H */

View File

@@ -16,6 +16,7 @@ LIBGA68_2.0 {
_libga68_posixclose;
_libga68_posixcreat;
_libga68_posixerrno;
_libga68_posixexit;
_libga68_posixfconnect;
_libga68_posixfgetc;
_libga68_posixfgets;
@@ -32,7 +33,6 @@ LIBGA68_2.0 {
_libga68_posixputs;
_libga68_posixstrerror;
_libga68_random;
_libga68_set_exit_status;
_libga68_u32_cmp2;
_libga68_unreachable;
_libga68_upper_bound;

View File

@@ -20,22 +20,13 @@
<http://www.gnu.org/licenses/>. */
#include "ga68.h"
#include <stdlib.h> /* for EXIT_SUCCESS */
/* argc and argv are preserved in the following objects. */
int _libga68_argc;
char **_libga68_argv;
/* Exit status of the program reported to the OS upon exit. */
static int exit_status;
void
_libga68_set_exit_status (int status)
{
exit_status = status;
}
/* Entry point for Algol 68 programs. */
void __algol68_main (void);
@@ -48,5 +39,5 @@ main (int argc, char **argv)
_libga68_init_heap ();
__algol68_main ();
return exit_status;
return EXIT_SUCCESS;
}