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

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

[avr-gcc-list] Strange Code generated when using Global Register Variabl


From: Christian Steer
Subject: [avr-gcc-list] Strange Code generated when using Global Register Variables
Date: Sat, 30 Apr 2011 07:26:54 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8

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




reply via email to

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