oooops... compiler bug

Discussion and support of the compiler from ImageCraft for the M8C.

Moderator: richard@imagecraft.com

oooops... compiler bug

Postby zorglub » Sun Aug 30, 2009 10:50 pm

Hi all,

The compiler doesn't generate a proper assembly in the following scenario:
Code: Select all
typedef unsigned long U32;
typedef unsigned char U8;

U8 sum_of_bytes(U32 a) {
   U8 s = ((U8*)&a)[0]; // MSB
   s += ((U8*)&a)[1];
   s += ((U8*)&a)[2];
   s += ((U8*)&a)[3]; // LSB
   return s;
}

Code: Select all
_sum_of_bytes:
  s                    --> X+0
  a                    --> X-7
0383: 10       PUSH  X             (0184)     ret
0384: 4F       MOV   X,SP
0385: 38 01    ADD   SP,1

0387: 52 F9    MOV   A,[X-7]
0389: 54 00    MOV   [X+0],A
038B: 52 06    MOV   A,[X+6]   <<<< supposed to be [X-6]
038D: 05 00    ADD   [X+0],A
038F: 52 05    MOV   A,[X+5]   <<<< supposed to be [X-5]
0391: 05 00    ADD   [X+0],A
0393: 52 04    MOV   A,[X+4]   <<<< supposed to be [X-4]
0395: 05 00    ADD   [X+0],A
0397: 52 00    MOV   A,[X+0]
0399: 62 D0 00 MOV   REG[208],0
039C: 38 FF    ADD   SP,255
039E: 20       POP   X
039F: 7F       RET   

The assembly code to get the most significant byte is correct but not for the other bytes.

Cheers,
Zorglub
zorglub
Bite-Size Cheese
Bite-Size Cheese
 
Posts: 18
Joined: Wed Aug 27, 2008 9:55 am

Re: oooops... compiler bug

Postby Jerson » Sun Aug 30, 2009 11:51 pm

It should be like this
Code: Select all
typedef unsigned long U32;
typedef unsigned char U8;

U8 sum_of_bytes(U32 a) {
   U8 s = (U8*)&a[0]; // MSB
   s += (U8*)&a[1];
   s += (U8*)&a[2];
   s += (U8*)&a[3]; // LSB
   return s;
}
PSoC/8051/PIC/ARM/MSP430 - Embedded Systems Design
http://jerson.co.in
User avatar
Jerson
The Big Cheese
The Big Cheese
 
Posts: 367
Joined: Fri Jan 28, 2005 10:11 pm
Website: http://jerson.co.in
Yahoo Messenger: embedded.design@yahoo.com
Location: Bombay, India

Re: oooops... compiler bug

Postby nan358 » Mon Aug 31, 2009 12:16 am

Jerson,

Since the operator [] has precedence over the operator &, the first sub-expression which will be evaluated in the expression (U8*)&a[0] is a[0], which yields a syntax error.

--
Nobu
nan358
The Big Cheese
The Big Cheese
 
Posts: 728
Joined: Mon Jul 02, 2007 8:12 am

Re: oooops... compiler bug

Postby nan358 » Tue Sep 01, 2009 8:11 pm

Somewhat similar bug appears when compiling these codes:

Code: Select all
void f(int i)
{
   int* p;
   p = &i;
   p = 1 + &i;
   p = 2 + &i;
}


Code: Select all
 0004           ; void f(int i)
 0004           ; {
 0004           ;       int* p;
 0004           ;       p = &i;
 0004 5A00              mov [__r0],X
 0006 160005            sub [__r0],5
 0009 5100              mov A,[__r0]
 000B 5401              mov [X+1],A
 000D           ;       p = 1 + &i;
 000D 5A00              mov [__r0],X
 000F 1600FD            sub [__r0],-3   <<<< supposed to be 3, not -3
 0012 5100              mov A,[__r0]
 0014 5401              mov [X+1],A
 0016           ;       p = 2 + &i;
 0016 5A00              mov [__r0],X
 0018 1600FF            sub [__r0],-1   <<<< supposed to be 1, not -1
 001B 5100              mov A,[__r0]
 001D 5401              mov [X+1],A
 001F           ;
 001F 38FE              add SP,-2
 0021 20                pop X
 0022 7F                ret

It seems that the compiler has a problem calculating address offset from address of argument variable.

--
Nobu
nan358
The Big Cheese
The Big Cheese
 
Posts: 728
Joined: Mon Jul 02, 2007 8:12 am

Re: oooops... compiler bug

Postby zorglub » Mon Nov 02, 2009 10:51 pm

I tried SP6 and the problem is still there. Richard, any news?
Cheers
zorglub
Bite-Size Cheese
Bite-Size Cheese
 
Posts: 18
Joined: Wed Aug 27, 2008 9:55 am

Re: oooops... compiler bug

Postby richard@imagecraft.com » Tue Nov 03, 2009 1:07 am

zorglub wrote:I tried SP6 and the problem is still there. Richard, any news?
Cheers


We didn't know about this bug. Please send it to richard@imagecraft.com and I will track it for the next release. Sorry about that.

BTW, you probably meant

... *(unsigned char *)&a[...]

with the indirection * in the front.

// richard
richard@imagecraft.com
Cheese Wheel
Cheese Wheel
 
Posts: 167
Joined: Mon Apr 05, 2004 10:33 pm

Re: oooops... compiler bug

Postby zorglub » Mon Oct 04, 2010 4:55 pm

I tried the new PD5.1 and this bug is still there.
Any news when this bug will be fixed?

Cheers,
Zorglub
zorglub
Bite-Size Cheese
Bite-Size Cheese
 
Posts: 18
Joined: Wed Aug 27, 2008 9:55 am


Return to “%s” ImageCraft ICCv7 for M8C Compiler

Who is online

Users browsing this forum: No registered users and 1 guest

cron