summaryrefslogtreecommitdiff
path: root/compiler/GHC
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2021-03-14 03:31:21 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-03-20 07:52:47 -0400
commit8e054ff3c79be5d33a00449bec5e7d4814ff2e54 (patch)
tree4e703c7b84d50469e0ef2e86f57101d5bf148276 /compiler/GHC
parent3fa3fb79e02858c29797a904655985628513cc01 (diff)
downloadhaskell-8e054ff3c79be5d33a00449bec5e7d4814ff2e54.tar.gz
Fix literals for unregisterized backend of small types
All credit to @hsyl20, who in https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4717#note_338560 figured out this was a problem. To fix this, we use casts in addition to the shrinking and suffixing that is already done. It might make for more verbose code, I don't think that matters too much. In the future, perhaps some of the shrinking and suffixing can be removed for being redundant. That proved less trivial than it sounds, so this wasn't done at this time. Progress towards #19026 Metric Increase: T12707 T13379 Co-authored-by: Sylvain Henry <hsyl20@gmail.com>
Diffstat (limited to 'compiler/GHC')
-rw-r--r--compiler/GHC/CmmToC.hs21
1 files changed, 16 insertions, 5 deletions
diff --git a/compiler/GHC/CmmToC.hs b/compiler/GHC/CmmToC.hs
index d7596f0aac..49f0567a81 100644
--- a/compiler/GHC/CmmToC.hs
+++ b/compiler/GHC/CmmToC.hs
@@ -1304,13 +1304,24 @@ wordShift platform = widthInLog (wordWidth platform)
commafy :: [SDoc] -> SDoc
commafy xs = hsep $ punctuate comma xs
--- Print in C hex format: 0x13fa
+-- | Print in C hex format
+--
+-- Examples:
+--
+-- 5114 :: W32 ===> ((StgWord32)0x13faU)
+-- (-5114) :: W32 ===> ((StgWord32)(-0x13faU))
+--
+-- We use casts to support types smaller than `unsigned int`; C literal
+-- suffixes support longer but not shorter types.
pprHexVal :: Platform -> Integer -> Width -> SDoc
-pprHexVal platform w rep
- | w < 0 = parens (char '-' <>
- text "0x" <> intToDoc (-w) <> repsuffix rep)
- | otherwise = text "0x" <> intToDoc w <> repsuffix rep
+pprHexVal platform w rep = parens ctype <> rawlit
where
+ rawlit
+ | w < 0 = parens (char '-' <>
+ text "0x" <> intToDoc (-w) <> repsuffix rep)
+ | otherwise = text "0x" <> intToDoc w <> repsuffix rep
+ ctype = machRep_U_CType platform rep
+
-- type suffix for literals:
-- Integer literals are unsigned in Cmm/C. We explicitly cast to
-- signed values for doing signed operations, but at all other