Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ghellquist

macrumors regular
Aug 21, 2011
146
5
Stockholm Sweden
It has no obligation to, as the register keyword is "just a hint" (which is actually commonly ignored). In fact, the other way around is also true: The compiler may well decide to keep something in a register when the optimizer thinks that's a good idea, even if you didn't mention the register keyword.
Quite true.

A compiler might even select to not implement a variable at all, depending on optimization. The only thing the compiler promises is to make the code behave as if there was a variable. One exemple is loop optimization, where for example

for (i =1; i++; i<5) { ... do something .. }
might be unfolded inte a number of ... do something ...

as long as the code gives exactly the same result.

// Gunnar
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
In fact, I recall some years ago reading that arguments were being passed in subroutines via registers instead of the stack, as much as possible, because the PPC had a large register file, why waste it?
 

jon3543

macrumors 6502a
Sep 13, 2010
608
265
The "register" keyword has been effectively deprecated in decent mainstream compilers since the late 80s or thereabouts. By that time, compiler optimization had been implemented such that the compiler could do a better job of register allocation than the user could guide with the register keyword, which has been nothing more than an historical oddity for a very, very long time. It is typical for "register" to be completely ignored, and seeing it in code written in the last couple of decades should cause both eyebrows to raise, because it indicates either (a) something strange is going on and register is not being ignored, or (b) the programmer doesn't know what he's doing.

As for passing function arguments in registers, that is implementation-defined behavior. Usually, there are a number of "calling conventions" that define how arguments are passed, and when passed on the stack, the order in which they're pushed. In general, there can be more arguments than registers, and the stack will be a fallback mechanism even in register-based calling conventions. When the stack is used, there's also the question of who cleans it up, the caller or callee, and that too is defined by the calling convention.

The register keyword is genuine trivia, and calling conventions are something the application programmer can usually ignore. However, the latter can still be important mainly when interfacing with code written in different languages or more generally, code that uses different calling conventions than is the default for the current compiler.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.