Skip to content

Commit f69bd2f

Browse files
committed
Mark Terminate() as noreturn
This helps the compiler identify which code paths are taken rarely, and move them out of the way from hot paths.
1 parent 2073865 commit f69bd2f

7 files changed

Lines changed: 30 additions & 14 deletions

File tree

configure.ac

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ AS_IF([test $ok != yes],
267267
[ok=yes;
268268
AC_DEFINE([HAVE_POPCNT], [1], [Define to 1 if you have __popcnt function.])])
269269
AC_MSG_RESULT($ok)])
270+
AC_MSG_CHECKING([__attribute__((noreturn))])
271+
AC_LINK_IFELSE(
272+
[AC_LANG_PROGRAM([extern void fatal() __attribute__((noreturn));], [return 0;])],
273+
[
274+
AC_DEFINE(NORETURN, [__attribute__((noreturn))], [The "noreturn" function attribute.])
275+
AC_MSG_RESULT([yes])
276+
],
277+
[
278+
AC_DEFINE(NORETURN, [], [The "noreturn" function attribute.])
279+
AC_MSG_RESULT([no])
280+
])
270281

271282
# Check for inline
272283
AC_C_INLINE

sources/declare.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ static inline ULONG LongAbs(LONG x)
410410
*/
411411
static inline int UnsignedToInt(unsigned int x)
412412
{
413-
extern void TerminateImpl(int, const char*, int, const char*);
413+
extern void TerminateImpl(int, const char*, int, const char*) NORETURN;
414414
if ( x <= INT_MAX ) return(x);
415415
if ( x >= (unsigned int)INT_MIN )
416416
return((int)(x - (unsigned int)INT_MIN) + INT_MIN);
@@ -420,7 +420,7 @@ static inline int UnsignedToInt(unsigned int x)
420420

421421
static inline WORD UWordToWord(UWORD x)
422422
{
423-
extern void TerminateImpl(int, const char*, int, const char*);
423+
extern void TerminateImpl(int, const char*, int, const char*) NORETURN;
424424
if ( x <= WORD_MAX_VALUE ) return(x);
425425
if ( x >= (UWORD)WORD_MIN_VALUE )
426426
return((WORD)(x - (UWORD)WORD_MIN_VALUE) + WORD_MIN_VALUE);
@@ -430,7 +430,7 @@ static inline WORD UWordToWord(UWORD x)
430430

431431
static inline LONG ULongToLong(ULONG x)
432432
{
433-
extern void TerminateImpl(int, const char*, int, const char*);
433+
extern void TerminateImpl(int, const char*, int, const char*) NORETURN;
434434
if ( x <= LONG_MAX_VALUE ) return(x);
435435
if ( x >= (ULONG)LONG_MIN_VALUE )
436436
return((LONG)(x - (ULONG)LONG_MIN_VALUE) + LONG_MIN_VALUE);
@@ -791,7 +791,7 @@ extern VOID PositionStream(STREAM *,LONG);
791791
extern int ReverseStatements(STREAM *);
792792
extern int ProcessOption(UBYTE *,UBYTE *,int);
793793
extern int DoSetups(VOID);
794-
extern VOID TerminateImpl(int, const char *,int, const char *);
794+
extern VOID TerminateImpl(int, const char *,int, const char *) NORETURN;
795795
extern NAMENODE *GetNode(NAMETREE *,UBYTE *);
796796
extern int AddName(NAMETREE *,UBYTE *,WORD,WORD,int *);
797797
extern int GetName(NAMETREE *,UBYTE *,WORD *,int);
@@ -990,7 +990,7 @@ extern int DoPolyratfun(UBYTE *);
990990
extern int CompileStatement(UBYTE *);
991991
extern UBYTE *ToToken(UBYTE *);
992992
extern int GetDollar(UBYTE *);
993-
extern int MesWork(VOID);
993+
extern VOID MesWork(VOID) NORETURN;
994994
extern int MesPrint(const char *,...);
995995
extern int MesCall(char *);
996996
extern UBYTE *NumCopy(WORD,UBYTE *);

sources/execute.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,9 @@ WORD PutBracket(PHEAD WORD *termin)
11181118
WORD *bbb = 0, *bind, *binst = 0, bwild = 0, *bss = 0, *bns = 0, bset = 0;
11191119
term1 = AT.WorkPointer+1;
11201120
term2 = (WORD *)(((UBYTE *)(term1)) + AM.MaxTer);
1121-
if ( ( (WORD *)(((UBYTE *)(term2)) + AM.MaxTer) ) > AT.WorkTop ) return(MesWork());
1121+
if ( ( (WORD *)(((UBYTE *)(term2)) + AM.MaxTer) ) > AT.WorkTop ) {
1122+
MesWork();
1123+
}
11221124
if ( AR.BracketOn < 0 ) {
11231125
t2 = term1; t1 = term2; /* AntiBracket */
11241126
}

sources/message.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static char hex[] = {'0','1','2','3','4','5','6','7','8','9',
5353
#[ Error0 :
5454
*/
5555

56-
VOID Error0(char *s)
56+
NORETURN VOID Error0(char *s)
5757
{
5858
MesPrint("=== %s",s);
5959
Terminate(-1);
@@ -64,7 +64,7 @@ VOID Error0(char *s)
6464
#[ Error1 :
6565
*/
6666

67-
VOID Error1(char *s, UBYTE *t)
67+
NORETURN VOID Error1(char *s, UBYTE *t)
6868
{
6969
MesPrint("@%s %s",s,t);
7070
Terminate(-1);
@@ -75,7 +75,7 @@ VOID Error1(char *s, UBYTE *t)
7575
#[ Error2 :
7676
*/
7777

78-
VOID Error2(char *s1, char *s2, UBYTE *t)
78+
NORETURN VOID Error2(char *s1, char *s2, UBYTE *t)
7979
{
8080
MesPrint("@%s%s %s",s1,s2,t);
8181
Terminate(-1);
@@ -86,12 +86,11 @@ VOID Error2(char *s1, char *s2, UBYTE *t)
8686
#[ MesWork :
8787
*/
8888

89-
int MesWork(VOID)
89+
NORETURN VOID MesWork(VOID)
9090
{
9191
MesPrint("=== Workspace overflow. %l bytes is not enough.",AM.WorkSize);
9292
MesPrint("=== Change parameter WorkSpace in %s",setupfilename);
9393
Terminate(-1);
94-
return(-1);
9594
}
9695

9796
/*

sources/parallel.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,9 @@ int PF_Processor(EXPRESSIONS e, WORD i, WORD LastExpression)
15581558
}
15591559
#endif
15601560

1561-
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer ) ) > AT.WorkTop ) return(MesWork());
1561+
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer ) ) > AT.WorkTop ) {
1562+
MesWork();
1563+
}
15621564

15631565
/* For redefine statements. */
15641566
if ( AC.numpfirstnum > 0 ) {

sources/proces.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ WORD Processor(VOID)
9999
AR.CompressPointer = AR.CompressBuffer;
100100
AR.NoCompress = AC.NoCompress;
101101
term = AT.WorkPointer;
102-
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer) ) > AT.WorkTop ) return(MesWork());
102+
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer) ) > AT.WorkTop ) {
103+
MesWork();
104+
}
103105
UpdatePositions();
104106
C->rhs[C->numrhs+1] = C->Pointer;
105107
AR.KeptInHold = 0;

sources/reshuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1875,7 +1875,7 @@ WORD DoPartitions(PHEAD WORD *term, WORD level)
18751875
t2 = twhere+twhere[1];
18761876
to = termout = AT.WorkPointer;
18771877
if ( termout + *term + part.numpart*FUNHEAD + AM.MaxTal >= AT.WorkTop ) {
1878-
return(MesWork());
1878+
MesWork();
18791879
}
18801880
for ( i = 0; i < ncoeffnum; i++ ) coeff[i] = coeffnum[i];
18811881
ncoeff = ncoeffnum;

0 commit comments

Comments
 (0)