Do not spill variables/registers on the stack

David Brown david@westcontrol.com
Mon Feb 7 10:48:00 GMT 2011


On 04/02/2011 23:13, Ian Lance Taylor wrote:
> Enrico Weigelt<weigelt@metux.de>  writes:
>
>> What does the ANSI C spec say about the "register" keyword ?
>
> 6.7.1.  "A declaration of an identifier for an object with storage-class
> specifier register suggests that access to the object be as fast as
> possible.  The extent to which such suggestions are effective is
> implementation-defined."
>
> Then a footnote to that text says "The implementation may treat any
> register declaration simply as an auto declaration.  However, whether or
> not addressable storage is actually used, the address of any part of an
> object declared with storage-class specifier register cannot be
> computed, either explicitly (by use of the unary&  operator as discussed
> in 6.5.3.2) or implicitly (by converting an array name to a pointer as
> discussed in 6.3.2.1).  Thus, the only operator that can be applied to
> an array declared with storage-class specifier register is sizeof."
>
>

Technically, that means gcc can ignore the "register" qualifier except 
to give a warning or error if you try to take the address of the data.

>> Does GCC issue some warning when it fails to put such a variable
>> directly into a register ?
>
> No.
>
>> Is it that keyword still useful at all on todays compilers ?
>
> No, except for things like gcc's extension to specify the hardware
> register where a variable should live.
>

I suppose the OP's situation where he specifically wants to use -O0 but 
have some data in registers is another case, but it's certainly quite 
unusual.

>> I'm not really experienced at that low level, but i can imagine
>> scenarios where it's essential to keep certain things in registers
>> (eg. when there's no valid stack), so the compiler should issue
>> a warning (which can be made fatal w/ -Werror) in those cases.
>
> Such code can not be written in standard C.  It always requires
> extensions.
>

Fortunately, gcc has such extensions - the "register asm" extension you 
mentioned does exactly that.

Actually, it is not necessary to use such extensions to write code that 
runs before the stack exists.  You just have to be very careful, compile 
with optimisations (so that data does go in registers), and obviously 
limit the features you use.  You also want to check the generated 
assembly carefully.

I work with embedded systems on a variety of processors, and sometimes 
need to write pre-main startup code.  I invariably write it in straight 
C with only a minimum of inline assembly.

I also once had a project for a microcontroller with no RAM at all - 
only 32 8-bit registers.  I wrote that with gcc without too many 
problems (obviously it was a small program).

The real use of the "register asm" extension is not for pre-main code, 
but for things like RTOS's where you might have some sort of "current 
task" pointer or variable in a fixed register.  It's not that common, 
however, and very expensive unless the cpu has lots of registers.  It is 
also occasionally used locally for things like system calls.




More information about the Gcc-help mailing list