weird code with MSPGCC

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

weird code with MSPGCC

Kees Schoenmakers
Hello,

I experienced some odd effect when msp430-gcc tries to inline  memcpy
and/or memset.

the code

#include <stdint.h>

void test_memcpy(void)
{
        uint8_t mybuffer[10];
        memset((uint8_t*)&mybuffer, 0, 10);
}

when compiled with
msp430-gcc -c -O2 -mmcu=msp430f449 mspgcc_memcpy.c
results in

Disassembly of section .text:

00000000 <test_memcpy>:
   0:   31 50 f6 ff     add     #-10,   r1      ;#0xfff6
   4:   81 43 06 00     mov     #0,     6(r1)   ;r3 As==00, 0x0006(r1)
   8:   81 43 08 00     mov     #0,     8(r1)   ;r3 As==00, 0x0008(r1)
   c:   31 50 0a 00     add     #10,    r1      ;#0x000a
  10:   30 41           ret

In my opion thats not clearing 10 bytes....
Without optimation the code appears sound.

msp430-gcc (GCC) 4.7.0 20120322 (mspgcc dev 20120911)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Same size problem I noticed with memcpy if inlined....

best regards

Kees

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: weird code with MSPGCC

David Brown-4
Hi,

Your problem is that "mybuffer" is an unused local variable - the
compiler can therefore eliminate it entirely when optimising.

If you write code that uses mybuffer in some way (that can't be
optimised away), then it should work fine.

mvh.,

David


On 26/06/14 09:48, Kees Schoenmakers wrote:

>> Hello,
>>
>> I experienced some odd effect when msp430-gcc tries to inline
>> memcpy and/or memset.
>>
>> the code
>>
>> #include <stdint.h>
>>
>> void test_memcpy(void) { uint8_t mybuffer[10];
>> memset((uint8_t*)&mybuffer, 0, 10); }
>>
>> when compiled with msp430-gcc -c -O2 -mmcu=msp430f449
>> mspgcc_memcpy.c results in
>>
>> Disassembly of section .text:
>>
>> 00000000 <test_memcpy>: 0:   31 50 f6 ff     add     #-10,   r1
>> ;#0xfff6 4:   81 43 06 00     mov     #0,     6(r1)   ;r3 As==00,
>> 0x0006(r1) 8:   81 43 08 00     mov     #0,     8(r1)   ;r3 As==00,
>> 0x0008(r1) c:   31 50 0a 00     add     #10,    r1      ;#0x000a
>> 10:   30 41           ret
>>
>> In my opion thats not clearing 10 bytes.... Without optimation the
>> code appears sound.
>>
>> msp430-gcc (GCC) 4.7.0 20120322 (mspgcc dev 20120911) Copyright (C)
>> 2012 Free Software Foundation, Inc. This is free software; see the
>> source for copying conditions.  There is NO warranty; not even for
>> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>>
>> Same size problem I noticed with memcpy if inlined....
>>
>> best regards
>>
>> Kees
>>
>> ------------------------------------------------------------------------------
>>
>>
Open source business process management suite built on Java and Eclipse
>> Turn processes into business applications with Bonita BPM Community
>> Edition Quickly connect people, data, and systems into organized
>> workflows Winner of BOSSIE, CODIE, OW2 and Gartner awards
>> http://p.sf.net/sfu/Bonitasoft
>>
>

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: weird code with MSPGCC

Orlando Arias
In reply to this post by Kees Schoenmakers
Hello,
You have a bug in your code, you are taking the address of the pointer
and zeroing it. This is a dangerous thing to do, the proper way is to do:
        memset(mybuffer, 0, 10);
or
        memset(&mybuffer[0], 0, 10);
The compiler should have given you a warning about that without the
cast. Listen to it and do not try to force things.

As for why the elimination of memset()/memcpy(), the compiler is doing
that because you are telling it to optimize. Since you do not use the
buffer at all in the function, it does not need to be zeroed. In fact,
the two mov instructions can also be optimized away. The first add
instruction allocates the buffer in the stack and the last one removes it.

When you do not tell the compiler to optimize the code (-O0), it will
not make the optimizations and you see your memset()/memcpy() work
properly. You will be able to see it at work with optimizations enabled
if you were to declare your buffer as a global, that is

uint8_t mybuffer[10];

void test_memcpy(void) {
        memset(mybuffer, 1, 10); /* globals are initialized */
                                        /* to 0 by default */
}

Again, not using the buffer may imply optimizing it away...
Cheers.

On 06/26/2014 03:48 AM, Kees Schoenmakers wrote:

> Hello,
>
> I experienced some odd effect when msp430-gcc tries to inline  memcpy
> and/or memset.
>
> the code
>
> #include <stdint.h>
>
> void test_memcpy(void)
> {
>         uint8_t mybuffer[10];
>         memset((uint8_t*)&mybuffer, 0, 10);
> }
>
> when compiled with
> msp430-gcc -c -O2 -mmcu=msp430f449 mspgcc_memcpy.c
> results in
>
> Disassembly of section .text:
>
> 00000000 <test_memcpy>:
>    0:   31 50 f6 ff     add     #-10,   r1      ;#0xfff6
>    4:   81 43 06 00     mov     #0,     6(r1)   ;r3 As==00, 0x0006(r1)
>    8:   81 43 08 00     mov     #0,     8(r1)   ;r3 As==00, 0x0008(r1)
>    c:   31 50 0a 00     add     #10,    r1      ;#0x000a
>   10:   30 41           ret
>
> In my opion thats not clearing 10 bytes....
> Without optimation the code appears sound.
>
> msp430-gcc (GCC) 4.7.0 20120322 (mspgcc dev 20120911)
> Copyright (C) 2012 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> Same size problem I noticed with memcpy if inlined....
>
> best regards
>
> Kees
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awards
> http://p.sf.net/sfu/Bonitasoft
> _______________________________________________
> Mspgcc-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

signature.asc (254 bytes) Download Attachment