From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22076 invoked by alias); 15 Mar 2004 10:54:51 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 22066 invoked from network); 15 Mar 2004 10:54:49 -0000 Received: from unknown (HELO securemail.more-it.net) (62.214.5.132) by sources.redhat.com with SMTP; 15 Mar 2004 10:54:49 -0000 Received: from securemail.more-it.net (securemail.more-it.net [127.0.0.1]) by securemail.more-it.net (8.11.6p3/8.11.6) with ESMTP id i2FAsmZ26590 for ; Mon, 15 Mar 2004 11:54:48 +0100 Received: from mail.kristensen.de (mail.kristensen.de [217.5.183.3]) by securemail.more-it.net (8.11.6p3/8.11.6) with ESMTP id i2FAslI26584 for ; Mon, 15 Mar 2004 11:54:47 +0100 Received: from B1WS064 ([172.16.101.64]) by mail.kristensen.de (8.11.6/8.11.6) with SMTP id i2FAsla24302 for ; Mon, 15 Mar 2004 11:54:47 +0100 Message-ID: <002301c40a7b$ee521b30$406510ac@B1WS064> From: "Wolfgang Kuehn" To: Subject: register variable is clobbered by passing parameter to a function Date: Mon, 15 Mar 2004 10:54:00 -0000 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-SW-Source: 2004-03/txt/msg00669.txt.bz2 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