Watcom Compiler error ?

Can it be that there is an error in the watcom 10.6 compiler?

The following code fragment:

for( cat = 0; cat <= StartMaxCatC; cat++ )
{
found = FALSE;
for( i = 0; i < procs; i++ )
{
if( killProc( procs - i - 1, cat ) ) // <============
{
found = TRUE;
} // if
} // for

if( found && ( startDelay > 0 ) )
{
delay( startDelay );
} // if
} // for

If I compile with optimising active:
cc -O -w3 -5 -DNDEBUG -I/MMV/h -c Start.c
cc -O -w3 -5 -DNDEBUG -I/MMV/h Start.o /MMV/lib/*.lib -o Start
the function killProc is never called.

If I compile with debug information (so reduced optimising)
cc -O -w3 -g -5 -I/MMV/h -c Start.c
cc -O -w3 -g -5 -I/MMV/h Start.o /MMV/lib/*.lib -o Start
all is working as expected.

If I add a printf("%d", i) somewhere in the inner loop,
all is working also independant of the compiler options.

Thanks for any help
Werner

Werner Schweizer <nospamWrnr.Schwzr@ch.mullermartini.com> wrote:

Can it be that there is an error in the watcom 10.6 compiler?

Sure it can; but it’s also possible that there’s a problem in your code
that you haven’t noticed. For instance, maybe you forgot to initialize
StartMaxCatC or procs and one of them happens to be negative when you
compile with optimizations? It would be much easier to tell if you
posted a small but complete C file that could be compiled and
disassembled.

The following code fragment:

for( cat = 0; cat <= StartMaxCatC; cat++ )
{
found = FALSE;
for( i = 0; i < procs; i++ )
{
if( killProc( procs - i - 1, cat ) ) // <============
{
found = TRUE;
} // if
} // for

if( found && ( startDelay > 0 ) )
{
delay( startDelay );
} // if
} // for

If I compile with optimising active:
cc -O -w3 -5 -DNDEBUG -I/MMV/h -c Start.c
cc -O -w3 -5 -DNDEBUG -I/MMV/h Start.o /MMV/lib/*.lib -o Start
the function killProc is never called.

If I compile with debug information (so reduced optimising)
cc -O -w3 -g -5 -I/MMV/h -c Start.c
cc -O -w3 -g -5 -I/MMV/h Start.o /MMV/lib/*.lib -o Start
all is working as expected.

If I add a printf("%d", i) somewhere in the inner loop,
all is working also independant of the compiler options.

Thanks for any help
Werner


Wojtek Lerch QNX Software Systems Ltd.

Sorry, I just cut out the part of code in question.
Here now a stripped down runnable programm showing the problem.

======================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <i86.h>

static void
killProc( const ushort_t proc,
const ushort_t cat )
{
printf(“killProc proc=%d cat=%d\n”,
proc, cat );
} // killProc()

static void
killProcs( const ushort_t procs )
{
ushort_t i;
ushort_t cat;

for( cat=0; cat < 3; cat++ )
{
for ( i = 0; i < procs; i++ )
{
// printf("Call killProc %d: ", i );
killProc( procs - i - 1, cat );
} // for
} // for
} // killProcs()

main( int argc, char **argv )
{
killProcs( 5 );
} // main()

If I compile with:
cc -O -w3 -5 -DNDEBUG -c Start.c
cc -O -w3 -5 -DNDEBUG Start.o -o Start
then nothing is displayed (printf in function killProc).

If I compile with debug information
cc -O -w3 -g -5 -c Start.c
cc -O -w3 -g -5 Start.o -o Start
or uncomment the printf in the inner loop (on line 26)
everything works as expected.

TIA
Werner

“Wojtek Lerch” <wojtek_l@yahoo.ca> schrieb im Newsbeitrag
news:bdhoi0$qdq$1@inn.qnx.com

Werner Schweizer <> nospamWrnr.Schwzr@ch.mullermartini.com> > wrote:
Can it be that there is an error in the watcom 10.6 compiler?

Sure it can; but it’s also possible that there’s a problem in your code
that you haven’t noticed. For instance, maybe you forgot to initialize
StartMaxCatC or procs and one of them happens to be negative when you
compile with optimizations? It would be much easier to tell if you
posted a small but complete C file that could be compiled and
disassembled.

---- SNIP ----

Werner Schweizer <nospamWrnr.Schwzr@ch.mullermartini.com> wrote:

Sorry, I just cut out the part of code in question.
Here now a stripped down runnable programm showing the problem.

Congratulations – indeed it looks like a genuine compiler bug…

It seems to only happen when the -Ol optimization is enabled, and only
when both i and proc are unsigned short (I tried short, int, and
unsigned int). I guess the workaround could be “don’t use unsigned
short for loop control variables”…

======================
#include <stdio.h
#include <stdlib.h
#include <string.h

#include <sys/types.h
#include <i86.h

static void
killProc( const ushort_t proc,
const ushort_t cat )
{
printf(“killProc proc=%d cat=%d\n”,
proc, cat );
} // killProc()

static void
killProcs( const ushort_t procs )
{
ushort_t i;
ushort_t cat;

for( cat=0; cat < 3; cat++ )
{
for ( i = 0; i < procs; i++ )
{
// printf("Call killProc %d: ", i );
killProc( procs - i - 1, cat );
} // for
} // for
} // killProcs()

main( int argc, char **argv )
{
killProcs( 5 );
} // main()

If I compile with:
cc -O -w3 -5 -DNDEBUG -c Start.c
cc -O -w3 -5 -DNDEBUG Start.o -o Start
then nothing is displayed (printf in function killProc).

If I compile with debug information
cc -O -w3 -g -5 -c Start.c
cc -O -w3 -g -5 Start.o -o Start
or uncomment the printf in the inner loop (on line 26)
everything works as expected.

Thank you for loocking into it and for the workaround.
Werner

“Wojtek Lerch” <wojtek_l@yahoo.ca> schrieb im Newsbeitrag
news:bdpje7$g6m$1@inn.qnx.com

Werner Schweizer <> nospamWrnr.Schwzr@ch.mullermartini.com> > wrote:
Sorry, I just cut out the part of code in question.
Here now a stripped down runnable programm showing the problem.

Congratulations – indeed it looks like a genuine compiler bug…

It seems to only happen when the -Ol optimization is enabled, and only
when both i and proc are unsigned short (I tried short, int, and
unsigned int). I guess the workaround could be “don’t use unsigned
short for loop control variables”…

======================
#include <stdio.h
#include <stdlib.h
#include <string.h

#include <sys/types.h
#include <i86.h

static void
killProc( const ushort_t proc,
const ushort_t cat )
{
printf(“killProc proc=%d cat=%d\n”,
proc, cat );
} // killProc()

static void
killProcs( const ushort_t procs )
{
ushort_t i;
ushort_t cat;

for( cat=0; cat < 3; cat++ )
{
for ( i = 0; i < procs; i++ )
{
// printf("Call killProc %d: ", i );
killProc( procs - i - 1, cat );
} // for
} // for
} // killProcs()

main( int argc, char **argv )
{
killProcs( 5 );
} // main()

If I compile with:
cc -O -w3 -5 -DNDEBUG -c Start.c
cc -O -w3 -5 -DNDEBUG Start.o -o Start
then nothing is displayed (printf in function killProc).

If I compile with debug information
cc -O -w3 -g -5 -c Start.c
cc -O -w3 -g -5 Start.o -o Start
or uncomment the printf in the inner loop (on line 26)
everything works as expected.