Skip to content

Commit 44ec979

Browse files
author
H. Peter Anvin (Intel)
committed
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) <[email protected]>
1 parent 9ba21c1 commit 44ec979

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

asm/error.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ unsigned int debug_nasm; /* Debugging messages? */
1313
unsigned int opt_verbose_info; /* Informational messages? */
1414

1515
/* Common function body */
16-
#define nasm_do_error(_sev,_flags) \
16+
#define nasm_do_error(_sev,_flags) \
1717
do { \
18+
const errflags nde_severity = (_sev); \
19+
const errflags nde_flags = nde_severity | (_flags); \
1820
va_list ap; \
1921
va_start(ap, fmt); \
20-
if ((_sev) >= ERR_CRITICAL) \
21-
nasm_verror_critical((_sev)|(_flags), fmt, ap); \
22-
else \
23-
nasm_verror((_sev)|(_flags), fmt, ap); \
22+
if (nde_severity >= ERR_CRITICAL) { \
23+
nasm_verror_critical(nde_flags, fmt, ap); \
24+
unreachable(); \
25+
} else { \
26+
nasm_verror(nde_flags, fmt, ap); \
27+
if (nde_severity >= ERR_FATAL) \
28+
unreachable(); \
29+
} \
2430
va_end(ap); \
25-
if ((_sev) >= ERR_FATAL) \
26-
abort(); \
2731
} while (0)
2832

2933
/*

asm/nasm.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,23 +1874,25 @@ static const char no_file_name[] = "nasm"; /* What to print if no file name */
18741874
*/
18751875
static_fatal_func die_hard(errflags true_type, errflags severity)
18761876
{
1877-
fflush(NULL);
1877+
if (true_type < ERR_PANIC || !abort_on_panic) {
1878+
fflush(NULL);
18781879

1879-
if (true_type == ERR_PANIC && abort_on_panic)
1880-
abort();
1880+
if (ofile) {
1881+
fclose(ofile);
1882+
if (!keep_all)
1883+
remove(outname);
1884+
ofile = NULL;
1885+
}
18811886

1882-
if (ofile) {
1883-
fclose(ofile);
1884-
if (!keep_all)
1885-
remove(outname);
1886-
ofile = NULL;
1887-
}
1887+
if (severity & ERR_USAGE)
1888+
usage();
18881889

1889-
if (severity & ERR_USAGE)
1890-
usage();
1890+
/* Terminate immediately */
1891+
exit(true_type - ERR_FATAL + 1);
1892+
}
18911893

1892-
/* Terminate immediately */
1893-
exit(true_type - ERR_FATAL + 1);
1894+
while (1)
1895+
abort();
18941896
}
18951897

18961898
/*
@@ -1950,6 +1952,7 @@ fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args
19501952
fputc('\n', error_file);
19511953

19521954
die_hard(true_type, severity);
1955+
unreachable();
19531956
}
19541957

19551958
/**

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ AC_CHECK_FUNCS(sysconf)
224224
AC_CHECK_FUNCS([access _access faccessat])
225225

226226
PA_HAVE_FUNC(__builtin_expect,(1,1))
227+
PA_HAVE_FUNC(__builtin_unreachable,())
227228

228229
PA_FUNC_SNPRINTF
229230
PA_FUNC_VSNPRINTF

include/compiler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,12 @@ static inline unsigned int watcom_switch_hack(uint64_t x)
469469
# define default case BOGUS_CASE: default
470470
#endif
471471

472+
#ifndef unreachable /* C23 defines as a macro in <stddef.h> */
473+
# ifdef HAVE___BUILTIN_UNREACHABLE
474+
# define unreachable() __builtin_unreachable()
475+
# else
476+
# define unreachable() do { abort(); } while(1)
477+
# endif
478+
#endif
479+
472480
#endif /* NASM_COMPILER_H */

0 commit comments

Comments
 (0)