Word and Int literals not correctly truncated when cross compiling 64 -> 32 bit
GHC does not take the target word size into account for some of the rules in PrelRules
:
rule_convert "integerToWord" integerToWordName mkWordLitWord,
rule_convert "integerToInt" integerToIntName mkIntLitInt,
The relevant code from CoreSyn
:
-- | Create a machine word literal expression of type @Word#@ from a @Word@.
-- If you want an expression of type @Word@ use 'MkCore.mkWordExpr'
mkWordLitWord :: DynFlags -> Word -> Expr b
mkWordLit dflags w = Lit (mkMachWord dflags w)
mkWordLitWord dflags w = Lit (mkMachWord dflags (toInteger w))
-- | Create a machine integer literal expression of type @Int#@ from an @Int@.
-- If you want an expression of type @Int@ use 'MkCore.mkIntExpr'
mkIntLitInt :: DynFlags -> Int -> Expr b
mkIntLitInt dflags n = Lit (mkMachInt dflags (toInteger n))
If a literal is bigger than the target word size, these rules can lead to loss of truncation when optimizing a fromInteger
/ toInteger
pair. This makes testcase codeGen/should_run/T5785
fail when compiled with optimization with GHCJS on 64 bit GHC.
It probably also affects targeting 64 platforms with 32 bit compilers, where literals would be truncated too much, but I don't have a way of testing that.