Issue Details (XML | Word | Printable)

Key: PCC-409
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Anders Magnusson
Reporter: Iain Hibbert
Votes: 0
Watchers: 0

If you were logged in you would be able to see more operations.

'invalid' bitshift produces possibly incorrect code

Created: 10/Sep/12 09:21 PM   Updated: 15/Sep/12 04:05 PM
Component/s: Common code, i386 target
Affects Version/s: None
Fix Version/s: None

Environment: NetBSD/i386

 Description  « Hide
The following code snippet (which construct I found in some GNU code)

unsigned int foo(unsigned int a)
      return a << 31 << 1;

when compiled with pcc, produces a warning

foo.c, line 3: warning: shift larger than type

which of course is true (though gcc does not warn about it, as I guess that
it was intentionally written that way). However, while considering what to do
about it, I noticed that the output code pcc produces is not strictly correct..

        .align 4
        .globl foo
        .type foo,@function
        pushl %ebp
        movl %esp,%ebp
        subl $4,%esp
        movl 8(%ebp),%eax
        movl %eax,-4(%ebp)
        jmp .L86
        movl -4(%ebp),%eax
        .size foo,.-foo
        .ident "PCC: pcc 1.1.0.DEVEL 20120910 for netbsd-i386"

it seems that the function merely returns its input; the equivalent output from gcc
(and what I would expect) is to return 0 since all the bits have been shifted out

in fact, pcc is shifting "count % maxbits" rather than "count" here, which also produces
incorrect result for larger shifts..

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Iain Hibbert added a comment - 10/Sep/12 09:54 PM
I should note, that in C99 6.5.7 "Bitwise shift operators" note 3, it does say

  "If the value of the right operand is negative or is greater than or equal to the width of
  the promoted left operand, the behavior is undefined."

which means that pcc is not unconforming here, just that the result is not necessarily what I would expect..

Anders Magnusson added a comment - 10/Sep/12 10:18 PM
The code for clearing types is commented out in optim.c due to lack of side effects check. It should probably be fixed.

Anders Magnusson added a comment - 15/Sep/12 04:05 PM
The mod for correct size is now removed, to give least surprises.