Opened 11 years ago

Closed 11 years ago

#2388 closed bug (fixed)

GHC creates invalid test instruction for 64-bit code

Reported by: MartinGrabmueller Owned by: simonmar
Priority: normal Milestone: 6.10.1
Component: Compiler Version: 6.8.2
Keywords: Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

When compiling the cpuid package from Hackage (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/cpuid), Don Stewart noticed that compilation aborts on a 64-bit Linux box with the following error message:

Error: suffix or operands invalid for `test'

Examining the assembler code, we see that the instruction

testq $2147483648,%rax

is generated, but test is only allowed to take a 32-bit immediate operand.

Don's compilation info follows:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.8.2

$ gcc --version
gcc (GCC) 4.3.1
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -msr
Linux 2.6.25-ARCH x86_64

    $ cabal install cpuid
    Resolving dependencies...
    Downloading cpuid-0.2...
    Configuring cpuid-0.2...
    Preprocessing library cpuid-0.2...
    Building cpuid-0.2...
    [1 of 1] Compiling System.Cpuid     ( System/Cpuid.hs, dist/build/System/Cpuid.o )
    /tmp/ghc23771_0/ghc23771_0.s: Assembler messages:

    /tmp/ghc23771_0/ghc23771_0.s:3254:0:
         Error: suffix or operands invalid for `test'
    cabal: Error: some packages failed to install:
    cpuid-0.2 failed during the building phase. The exception was:
    exit: ExitFailure 1

Checking the temp files, this is generated:

    $ runhaskell Setup.lhs configure --ghc-options=-keep-tmp-files
    $ runhaskell Setup.lhs build                                  
    Preprocessing library cpuid-0.2...
    Building cpuid-0.2...
    [1 of 1] Compiling System.Cpuid     ( System/Cpuid.hs, dist/build/System/Cpuid.o )
    /tmp/ghc9500_0/ghc9500_0.s: Assembler messages:

    /tmp/ghc9500_0/ghc9500_0.s:3254:0:
         Error: suffix or operands invalid for `test'
    $ vim /tmp/ghc9500_0/ghc9500_0.s +3254


    s3TR_info:
        addq $208,%r12
        cmpq %r15,%r12
        ja .Lc3W2
        movq 7(%rbx),%rax
        testq $2147483648,%rax
        je .Lc3W4
        movq 8(%rbp),%rsi
        addq $16,%rbp
        addq $-208,%r12
        jmp r2By_info

Change History (4)

comment:1 Changed 11 years ago by igloo

difficulty: Unknown
Milestone: 6.10.1

Thanks for the report, we'll take a look at what's going on.

comment:2 Changed 11 years ago by igloo

Small testcase:

module Q where

import Data.Bits
import Data.Word

splitBytes :: Word32 -> Char
splitBytes w | w .&. 0x80000000 /= 0 = 'a'
splitBytes _ = 'b'
ghc-inplace --make -O q 
[1 of 1] Compiling Q                ( q.hs, q.o )
/tmp/ghc10020_0/ghc10020_0.s: Assembler messages:

/tmp/ghc10020_0/ghc10020_0.s:28:0:
     Error: suffix or operands invalid for `test'

comment:3 Changed 11 years ago by simonmar

Owner: set to simonmar

comment:4 Changed 11 years ago by simonmar

Resolution: fixed
Status: newclosed

Fixed:

Wed Jul 30 11:52:31 BST 2008  Simon Marlow <marlowsd@gmail.com>
  * FIX #2388: check that the operand fits before using the 'test' opcode
Note: See TracTickets for help on using tickets.