This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Is registers for arguments pluasable?
Feed Title: Michael Lucas-Smith
Feed URL: http://www.michaellucassmith.com/site.atom
Feed Description: Smalltalk and my misinterpretations of life
Here's the kicker with respect to calling methods using registers as the arguments. What if we don't have enough registers free for all the arguments? In this scenario, what happens is called 'register spill', where we spill over the available registers for use.
When this occurs, we must create an object to hold the arguments and pass that object as the only argument to the method. Such a method must already know it will take a spill object instead of register parameters.
So is this a practical method? How many registers do we have. So far I've talked about the 'continuation address', the 'stack pointer' and the 'stack allocation pointer'. That's three out of the x86's 8 registers used. If we want to go further, we could have the heap pointer (not an allocation pointer for the heap, this would sit at the start of the heap area). That leaves us with only 4 general purpose registers on the x86 architecture.
There will be other ways to handle the heap registers by putting them at known memory locations. And you can get 'fake' registers by reserving some known memory addresses to be used as registers too, but lets play it out as it is for now and see where it goes.
So to see if three general purpose registers is okay, I ran a quick check over all the method names in the VisualWorks Smalltalk image which also had WithStyle loaded in to it.
Here's the results:
No Arguments: 27,896 methods
One Argument: 4,057 methods
Two Arguments: 1,365 methods
Three Arguments: 486 methods
Four Arguments: 153 methods
Five Arguments: 66 methods
Six or more Arguments: 59 methods
I put in italics the methods that would require a spill object. Only 0.37% of methods in the system will spill!. Clearly this indicates that the technique will work even on a system with 8 registers like an x86.
And lets not forget that the alternative to putting arguments in to registers is to put arguments on to the stack like a C program. So we're not too bad off if we have to resort to that technique.
Actually, the sooner we know we have to use that technique in a method, the sooner we know that we will now have 4 registers free for 'whatever' instead of holding arguments to the next call. (There is surprisingly little 'whatever' left in a system like this)