From 44ec97993a28ba109f98c4514e27cef813720399 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin (Intel)" Date: Mon, 13 Oct 2025 17:53:42 -0700 Subject: [PATCH] compiler: add and use unreachable() macro C23 defines unreachable() as a macro in . For earlier versions of gcc, __builtin_unreachable() is possible. Signed-off-by: H. Peter Anvin (Intel) --- asm/error.c | 18 +++++++++++------- asm/nasm.c | 29 ++++++++++++++++------------- configure.ac | 1 + include/compiler.h | 8 ++++++++ 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/asm/error.c b/asm/error.c index a9671bc8c..0e17ffef1 100644 --- a/asm/error.c +++ b/asm/error.c @@ -13,17 +13,21 @@ unsigned int debug_nasm; /* Debugging messages? */ unsigned int opt_verbose_info; /* Informational messages? */ /* Common function body */ -#define nasm_do_error(_sev,_flags) \ +#define nasm_do_error(_sev,_flags) \ do { \ + const errflags nde_severity = (_sev); \ + const errflags nde_flags = nde_severity | (_flags); \ va_list ap; \ va_start(ap, fmt); \ - if ((_sev) >= ERR_CRITICAL) \ - nasm_verror_critical((_sev)|(_flags), fmt, ap); \ - else \ - nasm_verror((_sev)|(_flags), fmt, ap); \ + if (nde_severity >= ERR_CRITICAL) { \ + nasm_verror_critical(nde_flags, fmt, ap); \ + unreachable(); \ + } else { \ + nasm_verror(nde_flags, fmt, ap); \ + if (nde_severity >= ERR_FATAL) \ + unreachable(); \ + } \ va_end(ap); \ - if ((_sev) >= ERR_FATAL) \ - abort(); \ } while (0) /* diff --git a/asm/nasm.c b/asm/nasm.c index 49fa00eb7..2a209154c 100644 --- a/asm/nasm.c +++ b/asm/nasm.c @@ -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) { - fflush(NULL); + if (true_type < ERR_PANIC || !abort_on_panic) { + fflush(NULL); - if (true_type == ERR_PANIC && abort_on_panic) - abort(); + if (ofile) { + fclose(ofile); + if (!keep_all) + remove(outname); + ofile = NULL; + } - if (ofile) { - fclose(ofile); - if (!keep_all) - remove(outname); - ofile = NULL; + if (severity & ERR_USAGE) + usage(); + + /* Terminate immediately */ + exit(true_type - ERR_FATAL + 1); } - if (severity & ERR_USAGE) - usage(); - - /* Terminate immediately */ - exit(true_type - ERR_FATAL + 1); + while (1) + abort(); } /* @@ -1950,6 +1952,7 @@ fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args fputc('\n', error_file); die_hard(true_type, severity); + unreachable(); } /** diff --git a/configure.ac b/configure.ac index 448eeb291..acd7461bf 100644 --- a/configure.ac +++ b/configure.ac @@ -224,6 +224,7 @@ AC_CHECK_FUNCS(sysconf) AC_CHECK_FUNCS([access _access faccessat]) PA_HAVE_FUNC(__builtin_expect,(1,1)) +PA_HAVE_FUNC(__builtin_unreachable,()) PA_FUNC_SNPRINTF PA_FUNC_VSNPRINTF diff --git a/include/compiler.h b/include/compiler.h index d4cb71617..21025d7fa 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -469,4 +469,12 @@ static inline unsigned int watcom_switch_hack(uint64_t x) # define default case BOGUS_CASE: default #endif +#ifndef unreachable /* C23 defines as a macro in */ +# ifdef HAVE___BUILTIN_UNREACHABLE +# define unreachable() __builtin_unreachable() +# else +# define unreachable() do { abort(); } while(1) +# endif +#endif + #endif /* NASM_COMPILER_H */