Skip to content
  • Sergei Trofimovich's avatar
    UNREG: fix CmmRegOff large offset handling on W64 platforms · b8e34992
    Sergei Trofimovich authored and Ben Gamari's avatar Ben Gamari committed
    Gabor noticed C warning when building unregisterised
    64-bit compiler on GHC.Integer.Types (from integer-simple).
    
    Minimised example with a warning:
    
    ```haskell
    {-# LANGUAGE MagicHash #-}
    {-# LANGUAGE NoImplicitPrelude #-}
    {-# OPTIONS_GHC -Wall #-}
    
    module M (bug) where
    
    import GHC.Prim (Word#, minusWord#, ltWord#)
    import GHC.Types (isTrue#)
    
    -- assume Word = Word64
    bug :: Word# -> Word#
    bug x = if isTrue# (x `ltWord#` 0x8000000000000000##) then 0##
            else x `minusWord#` 0x8000000000000000##
    ```
    
    ```
    $ LANG=C x86_64-UNREG-linux-gnu-ghc -O1 -c M.hs -fforce-recomp
    /tmp/ghc30219_0/ghc_1.hc: In function 'M_bug_entry':
    
    /tmp/ghc30219_0/ghc_1.hc:20:14: error:
         warning: integer constant is so large that it is unsigned
    ```
    
    It's caused by limited handling of integer literals in CmmRegOff.
    This change switches to use standard integer literal pretty-printer.
    
    C code before the change:
    
    ```c
    FN_(M_bug_entry) {
    W_ _sAg;
    _cAr:
    _sAg = *Sp;
    switch ((W_)(_sAg < 0x8000000000000000UL)) {
    case 0x1UL: goto _cAq;
    default: goto _cAp;
    }
    _cAp:
    R1.w = _sAg+-9223372036854775808;
    // ...
    ```
    
    C code after the change:
    
    ```c
    FN_(M_bug_entry) {
    W_ _sAg;
    _cAr:
    _sAg = *Sp;
    switch ((W_)(_sAg < 0x8000000000000000UL)) {
    case 0x1UL: goto _cAq;
    default: goto _cAp;
    }
    _cAp:
    R1.w = _sAg+(-0x8000000000000000UL);
    ```
    
    URL: https://mail.haskell.org/pipermail/ghc-devs/2018-June/015875.html
    
    
    Reported-by: Gabor Greif
    Signed-off-by: default avatarSergei Trofimovich <slyfox@gentoo.org>
    
    Test Plan: test generated code on unregisterised mips64 and amd64
    
    Reviewers: simonmar, ggreif, bgamari
    
    Reviewed By: ggreif, bgamari
    
    Subscribers: rwbarton, thomie, carter
    
    Differential Revision: https://phabricator.haskell.org/D4856
    b8e34992