-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mario Palomo schrieb:
> m...@horizon.com wrote:
>> 2) Mark it as inline.  E.g. in a header file, include
>>
>> static inline uint16_t __attribute__((const))
>> convert_endian(uint16_t dt)
>> {
>>      asm("swpb %0" : "+g" (dt));
>>      return dt;
>> }
>>
>> Since it's only one instruction anyway, that will let mspgcc just
>> drop that one instruction in wherever it's needed.
>>
>> (Unfortunately, while it does a good job with register variables,
>> I haven't managed to convince it to generate "swpb @r15+" even when
>> spoon-fed code that should generate it.  Oh, well.)
>>
> 
> This does not work if I use this code:
> ------------------------------------------------------------------------------
> /*
>   endian.c
> */
> #include <stdint.h>
> 
> static inline uint16_t endian16(uint16_t v)
> {
>   asm("swpb %0" : "+g" (v));
>   return v;
> }
> 
> volatile uint16_t value;
> int main()
> {
>   value = 0x1122;
>   endian16(value);
> 
>   //At this point: value = 0x1122 !!!!
>   return (int)value;
> }//main
> ------------------------------------------------------------------------------
> 
> And I compile using:
> 
> msp430-gcc -g3 -mmcu=msp430x149 -o endian.msp endian.c -Wall -O2
> 
> The function 'endian16' (with or without __attribute__((const))) compile to
> nothing!! Maybe a problem of mspgcc with inline assembler in static inline
> functions? I work with msp430-gcc 3.2.3 compiled from CVS (2006-04-10).
> 
> 
> If I use a macro to inline the assembler, it works fine (and very optimized):
> ------------------------------------------------------------------------------
> /*
>   endian.c
> */
> #include <stdint.h>
> 
> #define endian16(v)  asm("swpb %0" : "+g" (v))
> 
> volatile uint16_t value;
> int main()
> {
>   value = 0x1122;
>   endian16(value);
> 
>   //At this point: value = 0x2211
>   return (int)value;
> }//main
> ------------------------------------------------------------------------------
> 
> Any comments? Maybe a bug? Greetings,

Yes, totally clear. This is a result of variable
visibility. The first version would have worked
if you had written

value = endian16(value);

or (with modifications to the inline assembler)

endian16(&value);

The other case: you call a function with the
parameter "value" which gets copied for the
lifetime of the function endian16 and discarded
after finishing that funciton. So the modified
parameter "value" gets discarded with the return
to the caller and the old "value" is visible again.

The function itself compiles to nothing because
there has nothing happened... the result is
discarded, there are no sideeffects so it is
unnecessary code.

The macro version works of course because it is
placed in the same visibility context as the
variable "value" is...


Best regards


        Robert Dominicus-Schleutermann

- --
Robert Dominicus-Schleutermann      Telefon : +49 36203 96-314
Software+Systeme Erfurt GmbH        Telefon : +49 36203 96-0
Fichtenweg 8                        Fax     : +49 36203 96-333
D-99198 Erfurt-Kerspleben
eMail: mailto:robert.dominicus-schleuterm...@sse-erfurt.de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE5E5s7kPn7znKZuYRAmQwAJ9n06KDbkO4CMxW75U3nz4mMSJNRwCdGQQ+
1rqXeNh9hg2fyFiUXLouMv4=
=DEk1
-----END PGP SIGNATURE-----

Reply via email to