Re: [cc65] Register variables are back

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2002-11-25 19:15:50


On Mon, Nov 25, 2002 at 06:50:57PM +0100, MagerValp wrote:
> So for me and other clueless people, how is this implemented? How many
> register variables makes sense?

The runtime has 6 bytes of zero page space available for register variables
(this could be increased, but I think it's a good value). So you can declare
register variables up to a total size of 6. The compiler will allocate
register space on a "first come, first served" base and convert any register
declarations that exceed the available register space silently to auto.
Parameters can also be declared as "register", this will in fact give slightly
shorter code than using a register variable.

Since a function must save the current values of the registers on entry and
restore them on exit, there is an overhead associated with register variables,
and this overhead is quite high (~ 20 bytes per variable). This means that
just declaring anything as register is not a good idea.

The best use for register variables are pointers, especially those that point
to structures. The magic number here is about 3 uses of a struct field: If the
function contains this number or even more, the generated code will be usually
shorter and faster when using a register variable for the struct pointer. The
reason for this is that the register variable can in many cases be used as a
pointer directly. Having a pointer in an auto variable means that this pointer
must first be copied into a zero page location, before it can be dereferenced.

Second best use for register variables are counters. However, there is not
much difference in the code generated for counters, so you will need at least
100 operations on this variable (for example in a loop) to make it worth the
trouble.

Register variables must be explicitly enabled by using -Or or -r on the
command line. Register variables are only accepted on function top level,
register variables declared in interior blocks are silently converted to auto.
With register variables disabled, all variables declared as register are
actually auto variables.

BTW: There is a new option "--dump-segsize" for the od65 utility. This prints
all the segments together with the sizes. This can be used to determine if a
change led to shorter code.

Please note: This was a big change, so there may be errors. If you experience
any trouble that goes away when disabling register variables, please drop me a
mail.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-11-25 19:15:56 CET