c/8221: return value of a function is forgotten

Zack Weinberg zack@codesourcery.com
Mon Oct 14 08:46:00 GMT 2002


The following reply was made to PR c/8221; it has been noted by GNATS.

From: Zack Weinberg <zack@codesourcery.com>
To: c.kaliszyk@zodiac.mimuw.edu.pl
Cc: gcc-gnats@gcc.gnu.org, cek@wp.pl
Subject: Re: c/8221: return value of a function is forgotten
Date: Mon, 14 Oct 2002 08:43:14 -0700

 On Mon, Oct 14, 2002 at 08:44:09AM -0000, c.kaliszyk@zodiac.mimuw.edu.pl wrote:
 > inline int getcputime(void) {
 >   int i;
 >   __asm__(
 >     "rdtsc
 >     movl %%eax, %0"
 >     : :"g" (i) :"ax", "dx");
 >   return i;
 > }
 
 This asm statement claims that I is an input to the instruction, and
 that there aren't any outputs.  Thus, GCC assumes that I is not
 modified by the asm.
 
 The correct way to write getcputime() would be
 
   static inline unsigned int
   getcputime(void)
   {
     unsigned int i;
     asm volatile ("rdtsc" : "=a" (i) : : "edx");
     return i;
   }
 
 The key change is to put the I parameter in the _first_ block of asm
 operands, and add a leading = to its constraint.  That tells the
 compiler that I contains an output.
 
 Changing "g" to "a" in the constraint tells GCC that the output
 appears in %eax, which obviates the need for the move instruction you
 wrote.  This also means you need not (must not) clobber edx.
 
 All the other changes are also necessary.  I leave as an exercise for
 you to figure out why I made them.
 
 zw



More information about the Gcc-prs mailing list