0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-11-08 23:27:15 -05:00

compiler: add and use unreachable() macro

C23 defines unreachable() as a macro in <stddef.h>. For earlier
versions of gcc, __builtin_unreachable() is possible.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel)
2025-10-13 17:53:42 -07:00
parent 9ba21c1e50
commit 44ec97993a
4 changed files with 36 additions and 20 deletions

View File

@@ -13,17 +13,21 @@ unsigned int debug_nasm; /* Debugging messages? */
unsigned int opt_verbose_info; /* Informational messages? */ unsigned int opt_verbose_info; /* Informational messages? */
/* Common function body */ /* Common function body */
#define nasm_do_error(_sev,_flags) \ #define nasm_do_error(_sev,_flags) \
do { \ do { \
const errflags nde_severity = (_sev); \
const errflags nde_flags = nde_severity | (_flags); \
va_list ap; \ va_list ap; \
va_start(ap, fmt); \ va_start(ap, fmt); \
if ((_sev) >= ERR_CRITICAL) \ if (nde_severity >= ERR_CRITICAL) { \
nasm_verror_critical((_sev)|(_flags), fmt, ap); \ nasm_verror_critical(nde_flags, fmt, ap); \
else \ unreachable(); \
nasm_verror((_sev)|(_flags), fmt, ap); \ } else { \
nasm_verror(nde_flags, fmt, ap); \
if (nde_severity >= ERR_FATAL) \
unreachable(); \
} \
va_end(ap); \ va_end(ap); \
if ((_sev) >= ERR_FATAL) \
abort(); \
} while (0) } while (0)
/* /*

View File

@@ -1874,23 +1874,25 @@ static const char no_file_name[] = "nasm"; /* What to print if no file name */
*/ */
static_fatal_func die_hard(errflags true_type, errflags severity) static_fatal_func die_hard(errflags true_type, errflags severity)
{ {
fflush(NULL); if (true_type < ERR_PANIC || !abort_on_panic) {
fflush(NULL);
if (true_type == ERR_PANIC && abort_on_panic) if (ofile) {
abort(); fclose(ofile);
if (!keep_all)
remove(outname);
ofile = NULL;
}
if (ofile) { if (severity & ERR_USAGE)
fclose(ofile); usage();
if (!keep_all)
remove(outname); /* Terminate immediately */
ofile = NULL; exit(true_type - ERR_FATAL + 1);
} }
if (severity & ERR_USAGE) while (1)
usage(); abort();
/* Terminate immediately */
exit(true_type - ERR_FATAL + 1);
} }
/* /*
@@ -1950,6 +1952,7 @@ fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args
fputc('\n', error_file); fputc('\n', error_file);
die_hard(true_type, severity); die_hard(true_type, severity);
unreachable();
} }
/** /**

View File

@@ -224,6 +224,7 @@ AC_CHECK_FUNCS(sysconf)
AC_CHECK_FUNCS([access _access faccessat]) AC_CHECK_FUNCS([access _access faccessat])
PA_HAVE_FUNC(__builtin_expect,(1,1)) PA_HAVE_FUNC(__builtin_expect,(1,1))
PA_HAVE_FUNC(__builtin_unreachable,())
PA_FUNC_SNPRINTF PA_FUNC_SNPRINTF
PA_FUNC_VSNPRINTF PA_FUNC_VSNPRINTF

View File

@@ -469,4 +469,12 @@ static inline unsigned int watcom_switch_hack(uint64_t x)
# define default case BOGUS_CASE: default # define default case BOGUS_CASE: default
#endif #endif
#ifndef unreachable /* C23 defines as a macro in <stddef.h> */
# ifdef HAVE___BUILTIN_UNREACHABLE
# define unreachable() __builtin_unreachable()
# else
# define unreachable() do { abort(); } while(1)
# endif
#endif
#endif /* NASM_COMPILER_H */ #endif /* NASM_COMPILER_H */