On Tue, Dec 18, 2012 at 10:16 AM, Radu Rendec <radu.ren...@cloudbit.ro>wrote:

> Hello,
>
> I noticed that mspgcc-20120406 produces strange assembly for some very
> simple pointer arithmetic. I've managed to isolate this in a tiny test
> case:
>
> ext.c:
>         int foo, bar;
>
> main.c:
>         extern int foo, bar;
>
>         void main(void) {
>                 char *ptr = (char *)&foo;
>                 char *top = (char *)&bar;
>
>                 *(int *)ptr = top - ptr;
>         }
>
> I'm compiling with this command:
> msp430-gcc -Os -mmcu=msp430f47173 main.c ext.c
>
> The only reason for putting foo and bar in another .c file than main()
> is to avoid "top - ptr" getting optimized out into a constant value
> determined at compile time.
>
> The assembly for the above C code looks like this:
>     313e:       3a 40 02 11     mov     #4354,  r10     ;#0x1102
>     3142:       0f 4a           mov     r10,    r15
>     3144:       8f 10           swpb    r15
>     3146:       8f 11           sxt     r15
>     3148:       8f 10           swpb    r15
>     314a:       8f 11           sxt     r15
>     314c:       0b 4f           mov     r15,    r11
>     314e:       3c 40 00 11     mov     #4352,  r12     ;#0x1100
>     3152:       0f 4c           mov     r12,    r15
>     3154:       8f 10           swpb    r15
>     3156:       8f 11           sxt     r15
>     3158:       8f 10           swpb    r15
>     315a:       8f 11           sxt     r15
>     315c:       0d 4f           mov     r15,    r13
>     315e:       0a 8c           sub     r12,    r10
>     3160:       0b 7d           subc    r13,    r11
>     3162:       82 4a 00 11     mov     r10,    &0x1100
>
> Given the above assembly, 0x1102 is the address of bar and 0x1100 the
> address of foo.
>
> Any idea why "top - ptr" is not compiled as a simple "sub" instruction?
> I'm guessing that it has something to do with operator signedness, but I
> can't figure it out...
>

Yes, it's obviously sign-extending the pointers and doing a 32-bit
subtraction.  I have a recollection that how mspgcc treated pointers
(signed/unsigned) changed sometime during the 4.7 development cycle.

mspgcc-20120911 appears to do the right thing and generates a simple 16-bit
subtract.

Peter



> Thanks,
>
> Radu Rendec
>
>
>
>
> ------------------------------------------------------------------------------
> LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
> Remotely access PCs and mobile devices and provide instant support
> Improve your efficiency, and focus on delivering more value-add services
> Discover what IT Professionals Know. Rescue delivers
> http://p.sf.net/sfu/logmein_12329d2d
> _______________________________________________
> Mspgcc-users mailing list
> Mspgcc-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>
------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to