avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Strange Code generated when using Global Register Var


From: Weddington, Eric
Subject: Re: [avr-gcc-list] Strange Code generated when using Global Register Variables
Date: Sat, 30 Apr 2011 05:37:29 -0600

What are you really trying to do? Are you trying to have a do-nothing loop for 
delay purposes?

Eric Weddington

> -----Original Message-----
> From: address@hidden
> [mailto:address@hidden On
> Behalf Of Christian Steer
> Sent: Friday, April 29, 2011 11:27 PM
> To: address@hidden
> Subject: [avr-gcc-list] Strange Code generated when using Global Register
> Variables
> 
> Hello list,
> 
> I tried to use a global register variable to save space and increase
> speed.
> But the generated code is very strange, not the expected register usage.
> 
> Here is the code of an simple example:
> 
> #include  <stdint.h>
> 
> volatile  register  uint8_t state __asm__("r24");
> 
> // just to avoid optimizig:
> static  volatile  uint8_t tmp;
> 
> void  main() {
> 
>     state=100;
>     tmp=state;
> 
>     do  {
>          tmp=state;
>     }while  (--state);
> }
> 
> 
> and the Assembler output using -O0:
> 
> 
>          ldir24,lo8(100);  state,
> .LM2:
>          movr25,r24       ;  state.0, state
>          sts tmp,r25       ;  tmp, state.0
> .L2:
> .LM3:
>          movr25,r24       ;  state.1, state
>          sts tmp,r25       ;  tmp, state.1
> .LM4:
>          movr25,r24       ;  state.2, state
>          subir25,lo8(-(-1));  state.3,
>          movr24,r25       ;  state, state.3
>          movr25,r24       ;  state.4, state
>          tstr25           ;  state.4
>          brne.L2          ; ,
> 
> and the Assembler output using -O3:
> 
>          ldir24,lo8(100);  state,
> .LM2:
>          ldir25,lo8(100);  state.0,
>          sts tmp,r25       ;  tmp, state.0
> .L2:
> .LM3:
>          movr18,r24       ;  state.1, state
>          sts tmp,r24       ;  tmp, state.1
> .LM4:
> 
> 
>          subir18,lo8(-(-1));  state.3,
>          movr24,r18       ;  state, state.3
>          brne.L2          ; ,
> 
> 
> ( The use of volatile with register is essential to get the results)
> 
> I expect when using a register variable that the compiler will just
> use the register as source as well as for calculation.
> 
> My Conclusion:
> The avr-gcc uses the register equivalent to a storage location.
> The value is 'loaded' to an other register for calculation and than
> 'stored'.
> Even the volatile keyword make sense: if it is missing, the value will be
> hold in other processor register.
> ( compiled with GCC 4.3.4 and -ffixed-r24 and I dont use a library)
> 
> Can I improve the generated code?
> 
> Christian
> 
> 
> _______________________________________________
> AVR-GCC-list mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/avr-gcc-list



reply via email to

[Prev in Thread] Current Thread [Next in Thread]