[comp.sys.amiga.programmer] Manx 5.0d bug

barrett@jhunix.HCF.JHU.EDU (Dan Barrett) (02/11/91)

	I have found a bug in Manx 5.0d.  The optimizer can produce
incorrect code with the -sr option ("compiler picks which local variables
get assigned to registers").  Enclosed is a minimal code example that
illustrates the bug.  Please someone verify this for me.  I have an
Amiga 1000 running 1.3.2.

This prints the right answer:

	Compile:	cc bug.c
	Link:		ln bug.o -lc
	Run:		1> bug
			Did you know that 3276476 + 8 - 1 == 3276483?

This prints the wrong answer:

	Compile:	cc -sr bug.c
	Link:		ln bug.o -lc
	Run:		1> bug
			Did you know that 3276480 + 8 - 1 == 3276488?

The problem is in the address arithmetic:

	p = buf + strlen(buf) - 1;

The "- 1" is ignored by the compiler.  It doesn't matter what the constant
is, as long as it is NOT stored in a variable.

	Code is below my signature.  I hope I saved somebody some 
frustration by hunting this down!!

                                                        Dan

 //////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
| Dan Barrett, Department of Computer Science      Johns Hopkins University |
| INTERNET:   barrett@cs.jhu.edu           |                                |
| COMPUSERVE: >internet:barrett@cs.jhu.edu | UUCP:   barrett@jhunix.UUCP    |
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////

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

main()
{
	char *p;
	char buf[BUFSIZ];

	strcpy(buf, "Anything");		/* Any string. */
	p = buf + strlen(buf) - 1;

	printf("Did you know that %ld + %d - 1 == %ld?\n",
		buf, strlen(buf), p);
}

umueller@iiic.ethz.ch (Urban Dominik Mueller) (02/16/91)

Recently,  a  Manx  5.0d  bug  report  was  posted on this net.  Before I
explain what happened, a short summary:
 
==> Make sure your Manx 5.0d compiler 'cc' is dated Sep 28 <==
    ( The version of 5.0d dated Sep 20 has a *nasty* bug )
 
The  rest  is  only  for  the interested.  What happens is the following:
Whenever  the destination an addition with a constant in it is an address
register  (this is why the mentioned bug only occured when -sr was turned
on),  Manx  tries  the  following  trick (-sr always set, but it can also
happen when it's not set):
 
Source code              Correct assembler code 5.0d (both versions)
-----------------------+--------------------------------------------
main()                 | move.l  d2,a0    ; i moved to a0
{                      | add.l d2,a0      ; i added to a0
    int i;             | pea   99(a0)     ; a0 + 99 pushed to stack
                       | jsr   _func      ; function call
    func( i+i+99 );    | add.w 4,sp       ; argument popping
}                      |
 
So  far,  so  good.  But when when one part of the addition is a pointer,
this happens with Manx 5.0d Sep 20:
 
Source code              Buggy assembler code by 5.0d Sep 20
-----------------------+------------------------------------
main()                 | move.l  d2,a0    ; i moved to a0
{                      | add.l a2,a0      ; p added to a0
    char *p;           | move.l  a0,-(sp) ; result pushed, but 99 forgotten
    int i;             | jsr   _func
                       | add.w 4,sp
    func( p+i+99 );    |
}
 
The 5.0d version dated Sep 28 does a nice job again (by just forgetting
about that 'pea' trick in that special case):
 
Same source code         Correct assembler code by 5.0d Sep 28
-----------------------+--------------------------------------
main()                 | move.l  d2,a0    ; i moved to a0
{                      | add.l a2,a0      ; p added to a0
    char *p;           | add.l 99,a0      ; 99 added to a0
    int i;             | move.l  a0,-(sp) ; a0 pushed
                       | jsr   _func
    func( p+i+99 );    | add.w 4,sp
}                      |
 
Hope this helped some people. IMHO it's up to Manx to tell you bugs like
this one. There are many people who can't call their BBS. At least I must
say that their update service is good, I got that special update for free
(hadn't even got to pay any postage).
                                          __
 |          Urban Mueller         |      / / |    Urban Mueller    |
 | USENET:  umueller@iiic.ethz.ch | __  / /  |    Schulhausstr. 83 |
 | FIDONET: 2:302/906 (AUGL)      | \ \/ /   | CH-6312 Steinhausen |
 | "Don't tell my employer"       |  \__/    |    SWITZERLAND      |

Ata@system-m.phx.bull.com (John G. Ata) (02/23/91)

I tried your test case with my version of Manx 5.0d and could not get it
to fail as you describe.  It always gave arithmetically correct answers
regardless of whether -sr was used.  Which version of 5.0d are you
using?  The correct version should be dated September 28, 1990 (printed
when cc is called) as there was a preliminary version of 5.0d that was
distributed over the BB with errors.

                                        John

Anthon_Pang@mindlink.UUCP (Anthon Pang) (02/24/91)

Ata@system-m.phx.bull.com (John G. Ata) writes:
> I tried your test case with my version of Manx 5.0d and could not get it
> to fail as you describe.  It always gave arithmetically correct answers
> regardless of whether -sr was used.  Which version of 5.0d are you
> using?  The correct version should be dated September 28, 1990 (printed
> when cc is called) as there was a preliminary version of 5.0d that was
> distributed over the BB with errors.

*ick*  My version is dated September 20th and I got my version mailed to me
through the update department.  This is ridiculous...I would expect better
quality control, before announcing and releasing an update.

One now worries whether one's version was the latest version where they added
one more feature, or fixed one more bug.  *sigh*