This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Who is responsible for ensuring a char is in range?


When I compile the following on MIPS with the latest egcs, a
char parameter gets masked with 0xff both at the calling point
and at function entry.  For example:

int testfn(int b, char c)
{
   if(c < 80)
     return b;
   else
     return b + 1;
}

extern int h;

int testfn2()
{
  int g = h;
  return testfn(0, g);
}

with -O2 I get the following code:

testfn:
...
        move    $2,$4
        andi    $5,$5,0x00ff
        sltu    $5,$5,80
        bne     $5,$0,.L5
        addu    $2,$2,1
.L5:

...

testfn2:
        lw      $5,h
        move    $4,$0
        andi    $5,$5,0x00ff
        la      $25,testfn
        jal     $31,$25
        ld      $31,24($sp)


I.e. the called function is masking the char with 0xff and
so is the calling function.  This seems like needless double
work, but I can't work out from the C standard (or is this a
calling convention thing) who should really be doing this.
Is it necessary to do it both places?  Can't testfn assume
that the value being passed in is within the correct range?

I took a look at what SGI's cc does, but I couldn't work
out at all where the mask was taking place.  It was working,
though.

-- 
Erik Corry erik@arbat.com     Ceterum censeo, Microsoftem esse delendam!


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]