public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* register variable is clobbered by passing parameter to a function
@ 2004-03-15 10:54 Wolfgang Kuehn
  2004-03-15 22:51 ` Richard Henderson
  2004-03-16 10:20 ` Dave Korn
  0 siblings, 2 replies; 11+ messages in thread
From: Wolfgang Kuehn @ 2004-03-15 10:54 UTC (permalink / raw)
  To: gcc

target-mcu: AVR
gcc-version: 3.3.1  (latest WINAVR-distribution)

When I declare an often used variable as register, the variable is modified
after calling a function.
Example-code:

register  char often_used_variable asm ("r16");
char foo;
void function1 (char p1, char p2, char p3, char p4, char p5) {   };

int main (void)
{
often_used_variable = 5;
function1 (6,7,8,9,10);
foo = often_used_variable;
}

is compiled to (opt. level=s):

int main (void)
{
.
.
often_used_variable = 5;
ldi r16, 0x05 ; 5       <-- 
function1 (6,7,8,9,10);
ldi r16, 0x0A ; 10   <--- here it happens
ldi r18, 0x09 ; 9
ldi r20, 0x08 ; 8
ldi r22, 0x07 ; 7
ldi r24, 0x06 ; 6
rcall .-24 ; 0x4a

foo = often_used_variable;
sts 0x0060, r16

} // main
.
and foo is now 10 instead of 5.

but without any warning.
If I use the register r18, I get a compiler-warning: call clobbered register
... (and the code is inoperative too).
I could not found anything about this behaviour in the gcc-online-docs.
It only happens with parameters passed to a function, that the register
variable is wasted.
If I use r14 and I have 6 parameters, the result is the same! (r14 is
wasted).
In a bigger application I use an often used variable in such a matter. It
saves a lot of space and runtime. And it works. But once a day it wouldn't
work and I debugged. It was when I added a function with some more
parameters. I know, that the register-keyword is not obligatory for the
compiler, but then he shall give me at least a warning.
Now I am not shure, how safe is my code, are there more side effects?

if I make in my example-code the variable local, the same happens.

K&R says, the compiler can ignore the keyword. But in this case he doesn't
ignores it, he uses the register but then he uses it for himself (for the
parameter passing).
If it is a local var, too.

When I debugged my application, I noticed, that in case of register decl.,
the compiler doesn't use the r16, in case of standard decl. he used the r16.
So I think, it is a bug, that only in the parameter passing he "forgot" to
use another register for passing.

Bug or feature?
Each comment is welcome.

Best regards
Wolfgang

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: register variable is clobbered by passing parameter to a function
@ 2004-03-16 14:18 Wolfgang Kuehn
  2004-03-16 14:47 ` Ian Lance Taylor
  0 siblings, 1 reply; 11+ messages in thread
From: Wolfgang Kuehn @ 2004-03-16 14:18 UTC (permalink / raw)
  To: gcc

> > Wolfgang, your best bet is probably to just add -fixed-r16 to your
> >compile-time flags.  And don't call any library functions that were
compiled
> >without it.

-ffixed-r16 has no effect at passing.

>> This is AVR, and he is trying to use r16.  According to
>> function_arg_regno_p() in avr.c, that register can be used to
> >pass parameters to functions.  gcc can't handle declaring a
>> register used for parameter passing as fixed.

>Ah.  Then my advice should have been "Change r16 to some safer register."
>Hopefully that should then suffice to make his global-register-var work
>correctly and there isn't actually a bug in gcc.

  Some safer register are r4,r5 and r6. And there is no effect by passing
parameters. But the register r16 (and all other above this) are more
efficient, because you can load only these immediately (ldi r16,0x10) ,
set/reset bits (sbr r17,1) and other very effective instructions.
Its a pity not to exhaust the complete power of this little processor.

In moment the best workaround for my application is not to pass more than
four (char) parameters to a function, so I am free to use the powerful
r16-register.

>The best bet might be to add a bit more
>documentation to the register-vars pages, indicating that registers chosen
>should not be call-used or fixed-for-other-purposes-by-the-abi.

Thats very welcome.

Last question: what means ABI ?

best regards
wolfgang

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-03-16 14:47 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-15 10:54 register variable is clobbered by passing parameter to a function Wolfgang Kuehn
2004-03-15 22:51 ` Richard Henderson
2004-03-16 11:50   ` Wolfgang Kuehn
2004-03-16 12:30     ` Michael Matz
2004-03-16 12:39       ` Michael Matz
2004-03-16 12:43       ` Dave Korn
2004-03-16 13:00         ` Ian Lance Taylor
2004-03-16 13:21           ` Dave Korn
2004-03-16 10:20 ` Dave Korn
2004-03-16 14:18 Wolfgang Kuehn
2004-03-16 14:47 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).